1 var $ = require('jquery');
2 var fs = require('fs');
3 var exec = require('child_process').exec;
4 var pd = require('pretty-data').pd;
5 var ansidiff = require('ansidiff');
8 eval(fs.readFileSync(__dirname + '/../../../../../redakcja/static/js/wiki/xslt.js') + '');
11 function assertNodesEqual(lhs, rhs, areHTMLNodes) {
13 function throwError() {
14 throw new Error(ansidiff.chars(pd.xml(lhs), pd.xml(rhs)));
17 var lhsNode = $(lhs)[0],
21 var getMagicAttrsInfo = function(node) {
22 var valuePrefix = 'x-attr-value-';
23 var namePrefix = 'x-attr-name-';
24 var startsWith = function(str, prefix) { return str.substr(0, prefix.length) === prefix; }
25 var isNameAttribute = function(attr) { return startsWith(attr.name, namePrefix); }
26 var isValueAttribute = function(attr) { return startsWith(attr.name, valuePrefix); }
27 var extractId = function(attr) { return attr.name.split('-').pop(); }
29 var toret = {map: {}, magicAttrsList: []};
31 for(var i = 0; i < node.attributes.length; i++) {
32 var attr = node.attributes[i];
33 if(isNameAttribute(attr) || isValueAttribute(attr)) {
34 var id = extractId(attr);
35 temp[id] = typeof temp[id] === 'undefined' ? {} : temp[id];
36 if(isNameAttribute(attr))
37 temp[id]['name'] = attr.value;
39 temp[id]['value'] = attr.value;
40 toret.magicAttrsList.push(attr.name);
44 Object.keys(temp).forEach(function(id) {
46 toret.map[pair.name] = typeof toret.map[pair.name] === 'undefined' ? toret.map[pair.name] = [] : toret.map[pair.name];
47 toret.map[pair.name].push(pair.value);
53 var removeAttrs = function(node, attrsNames) {
54 attrsNames.forEach(function(name) {
55 node.removeAttribute(name);
59 var mapsEqual = function(map1, map2) {
60 if(Object.keys(map1).length != Object.keys(map2).length)
62 var arraysEqual = function(a1, a2) {return a1.slice().sort().join('') === a2.slice().sort().join('');}
63 Object.keys(map1).forEach(function(key) {
64 if(Object.keys(map2).indexOf(key) === -1 || !arraysEqual(map2[key], map1[key]))
70 var lhsMagicAttrsInfo = getMagicAttrsInfo(lhsNode),
71 rhsMagicAttrsInfo = getMagicAttrsInfo(rhsNode);
73 removeAttrs(lhsNode, lhsMagicAttrsInfo.magicAttrsList);
74 removeAttrs(rhsNode, rhsMagicAttrsInfo.magicAttrsList);
76 if(!mapsEqual(lhsMagicAttrsInfo.map, rhsMagicAttrsInfo.map))
80 if(!lhsNode.isEqualNode(rhsNode))
84 suite('wiki.tests.xslt.auto', function() {
86 var tempFileName = '.temp.xml';
87 var xsltStyleSheetPath = __dirname + '/../../../../../redakcja/static/xsl/wl2html_client.xsl';
89 fs.readdirSync(__dirname + '/data/').forEach(function(fileName) {
91 if(fileName === tempFileName)
94 var ext = fileName.split('.').pop();
95 if(ext !== 'html' && ext !== 'xml')
98 var inputData = fs.readFileSync(__dirname + '/data/' + fileName) + '';
101 test('[HTML->XML->HTML] ' + fileName, function(done) {
102 var result = html2text({
103 element: $(inputData)[0],
105 success: function(generatedXML) {
106 fs.writeFileSync(tempFileName, generatedXML);
107 exec(['xsltproc', xsltStyleSheetPath, tempFileName].join(' ') , {},
108 function(error, stdout, stderr) {
109 fs.unlinkSync(tempFileName);
110 assertNodesEqual(inputData, stdout, true);
114 error: function(msg){throw msg;}
117 } else if(ext === 'xml') {
118 test('[XML->HTML->XML] ' + fileName, function(done) {
119 var originalXML = $(inputData);
120 exec(['xsltproc', xsltStyleSheetPath, __dirname + '/data/' + fileName].join(' ') , {},
121 function(error, stdout, stderr) {
122 var generatedHTML = $(stdout);
123 var result = html2text({
124 element: generatedHTML[0],
126 success: function(xmltext) {
127 assertNodesEqual(inputData, xmltext);
130 error: function(msg){throw msg;}