mobile PDF option
[redakcja.git] / apps / wiki / tests / xslt / auto / auto_test.js
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');
6
7
8 eval(fs.readFileSync(__dirname + '/../../../../../redakcja/static/js/wiki/xslt.js') + '');
9
10
11 function assertNodesEqual(lhs, rhs, areHTMLNodes) {
12
13     function throwError() {
14         throw new Error(ansidiff.chars(pd.xml(lhs), pd.xml(rhs)));
15     }
16
17     var lhsNode = $(lhs)[0],
18         rhsNode = $(rhs)[0];
19
20     if(areHTMLNodes) {
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(); }
28             var temp = {};
29             var toret = {map: {}, magicAttrsList: []};
30             
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;
38                     else
39                         temp[id]['value'] = attr.value;
40                     toret.magicAttrsList.push(attr.name);
41                 }
42             }
43
44             Object.keys(temp).forEach(function(id) {
45                 var pair = temp[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);
48             });
49             
50             return toret;
51         }
52     
53         var removeAttrs = function(node, attrsNames) {
54             attrsNames.forEach(function(name) {
55                 node.removeAttribute(name);
56             });
57         }
58         
59         var mapsEqual = function(map1, map2) {
60             if(Object.keys(map1).length != Object.keys(map2).length)
61                 return false;        
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]))
65                     return false;
66             });
67             return true;
68         }
69     
70         var lhsMagicAttrsInfo = getMagicAttrsInfo(lhsNode),
71             rhsMagicAttrsInfo = getMagicAttrsInfo(rhsNode);
72         
73         removeAttrs(lhsNode, lhsMagicAttrsInfo.magicAttrsList);
74         removeAttrs(rhsNode, rhsMagicAttrsInfo.magicAttrsList);
75         
76         if(!mapsEqual(lhsMagicAttrsInfo.map, rhsMagicAttrsInfo.map))
77             throwError();
78     }
79     
80     if(!lhsNode.isEqualNode(rhsNode))
81         throwError();
82 }
83
84 suite('wiki.tests.xslt.auto', function() {
85         
86         var tempFileName = '.temp.xml';
87         var xsltStyleSheetPath = __dirname + '/../../../../../redakcja/static/xsl/wl2html_client.xsl';
88         
89         fs.readdirSync(__dirname + '/data/').forEach(function(fileName) {
90             
91             if(fileName === tempFileName)
92                 return;
93             
94             var ext = fileName.split('.').pop();
95             if(ext !== 'html' && ext !== 'xml')
96                 return;
97
98             var inputData = fs.readFileSync(__dirname + '/data/' + fileName) + '';
99             
100             if(ext === 'html') {
101                 test('[HTML->XML->HTML] ' + fileName, function(done) {
102                     var result = html2text({
103                         element: $(inputData)[0],
104                         stripOuter: false,
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);
111                                     done();
112                             });
113                         },
114                         error: function(msg){throw msg;}
115                     });
116                 });
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],
125                                 stripOuter: false,
126                                 success: function(xmltext) {
127                                     assertNodesEqual(inputData, xmltext);
128                                     done();
129                                 },
130                                 error: function(msg){throw msg;}
131                             });
132                     });
133                 });
134             }
135         });
136 });
137
138