1 var jsdom = require("jsdom");
2 var $ = require('jquery')(new jsdom.JSDOM().window);
3 var fs = require('fs');
4 var exec = require('child_process').exec;
5 var pd = require('pretty-data').pd;
6 var ansidiff = require('ansidiff');
9 eval(fs.readFileSync(__dirname + '/../../../../redakcja/static/js/wiki/xslt.js') + '');
12 function assertNodesEqual(lhs, rhs, areHTMLNodes) {
14 function throwError() {
15 throw new Error(ansidiff.chars(pd.xml(lhs), pd.xml(rhs)));
18 var lhsNode = $(lhs)[0],
22 var getMagicAttrsInfo = function(node) {
23 var valuePrefix = 'x-attr-value-';
24 var namePrefix = 'x-attr-name-';
25 var startsWith = function(str, prefix) { return str.substr(0, prefix.length) === prefix; }
26 var isNameAttribute = function(attr) { return startsWith(attr.name, namePrefix); }
27 var isValueAttribute = function(attr) { return startsWith(attr.name, valuePrefix); }
28 var extractId = function(attr) { return attr.name.split('-').pop(); }
30 var toret = {map: {}, magicAttrsList: []};
32 for(var i = 0; i < node.attributes.length; i++) {
33 var attr = node.attributes[i];
34 if(isNameAttribute(attr) || isValueAttribute(attr)) {
35 var id = extractId(attr);
36 temp[id] = typeof temp[id] === 'undefined' ? {} : temp[id];
37 if(isNameAttribute(attr))
38 temp[id]['name'] = attr.value;
40 temp[id]['value'] = attr.value;
41 toret.magicAttrsList.push(attr.name);
45 Object.keys(temp).forEach(function(id) {
47 toret.map[pair.name] = typeof toret.map[pair.name] === 'undefined' ? toret.map[pair.name] = [] : toret.map[pair.name];
48 toret.map[pair.name].push(pair.value);
54 var removeAttrs = function(node, attrsNames) {
55 attrsNames.forEach(function(name) {
56 node.removeAttribute(name);
60 var mapsEqual = function(map1, map2) {
61 if(Object.keys(map1).length != Object.keys(map2).length)
63 var arraysEqual = function(a1, a2) {return a1.slice().sort().join('') === a2.slice().sort().join('');}
64 Object.keys(map1).forEach(function(key) {
65 if(Object.keys(map2).indexOf(key) === -1 || !arraysEqual(map2[key], map1[key]))
71 var lhsMagicAttrsInfo = getMagicAttrsInfo(lhsNode),
72 rhsMagicAttrsInfo = getMagicAttrsInfo(rhsNode);
74 removeAttrs(lhsNode, lhsMagicAttrsInfo.magicAttrsList);
75 removeAttrs(rhsNode, rhsMagicAttrsInfo.magicAttrsList);
77 if(!mapsEqual(lhsMagicAttrsInfo.map, rhsMagicAttrsInfo.map))
81 if(!lhsNode.isEqualNode(rhsNode))
85 suite('wiki.tests.xslt.auto', function() {
87 var tempFileName = '.temp.xml';
88 var xsltStyleSheetPath = __dirname + '/../../../../redakcja/static/xsl/wl2html_client.xsl';
90 fs.readdirSync(__dirname + '/data/').forEach(function(fileName) {
92 if(fileName === tempFileName)
95 var ext = fileName.split('.').pop();
96 if(ext !== 'html' && ext !== 'xml')
99 var inputData = fs.readFileSync(__dirname + '/data/' + fileName) + '';
102 test('[HTML->XML->HTML] ' + fileName, function(done) {
103 var result = html2text({
104 element: $(inputData)[0],
106 success: function(generatedXML) {
107 fs.writeFileSync(tempFileName, generatedXML);
108 exec(['xsltproc', xsltStyleSheetPath, tempFileName].join(' ') , {},
109 function(error, stdout, stderr) {
110 fs.unlinkSync(tempFileName);
111 assertNodesEqual(inputData, stdout, true);
115 error: function(msg){throw msg;}
118 } else if(ext === 'xml') {
119 test('[XML->HTML->XML] ' + fileName, function(done) {
120 var originalXML = $(inputData);
121 exec(['xsltproc', xsltStyleSheetPath, __dirname + '/data/' + fileName].join(' ') , {},
122 function(error, stdout, stderr) {
123 var generatedHTML = $(stdout);
124 var result = html2text({
125 element: generatedHTML[0],
127 success: function(xmltext) {
128 assertNodesEqual(inputData, xmltext);
131 error: function(msg){throw msg;}