#959: going through whole repository was a huge performance hog
[redakcja.git] / redakcja / static / js / wiki / xslt.js
1 /*
2  *
3  * XSLT STUFF
4  *
5  */
6 function createXSLT(xsl) {
7     var p = new XSLTProcessor();
8     p.importStylesheet(xsl);
9     return p;
10 }
11
12 var xml2htmlStylesheet = null;
13
14 // Wykonuje block z załadowanymi arkuszami stylów
15 function withStylesheets(code_block, onError)
16 {
17     if (!xml2htmlStylesheet) {
18         $.blockUI({message: 'Ładowanie arkuszy stylów...'});
19         $.ajax({
20                 url: STATIC_URL + 'xsl/wl2html_client.xsl',
21                 dataType: 'xml',
22                 timeout: 10000,
23                 success: function(data) {
24                 xml2htmlStylesheet = createXSLT(data);
25                 $.unblockUI();
26                                 code_block();
27
28             },
29                         error: onError
30         })
31     }
32         else {
33                 code_block();
34         }
35 }
36
37
38 // Wykonuje block z załadowanymi kanonicznymi motywami
39 function withThemes(code_block, onError)
40 {
41     if (typeof withThemes.canon == 'undefined') {
42         $.ajax({
43             url: '/themes',
44             dataType: 'text',
45             success: function(data) {
46                 withThemes.canon = data.split('\n');
47                 code_block(withThemes.canon);
48             },
49             error: function() {
50                 withThemes.canon = null;
51                 code_block(withThemes.canon);
52             }
53         })
54     }
55     else {
56         code_block(withThemes.canon);
57     }
58 }
59
60
61 function xml2html(options) {
62     withStylesheets(function() {
63         var xml = options.xml.replace(/\/(\s+)/g, '<br />$1');
64         var parser = new DOMParser();
65         var serializer = new XMLSerializer();
66         var doc = parser.parseFromString(xml, 'text/xml');
67         var error = $('parsererror', doc);
68
69         if (error.length == 0) {
70             doc = xml2htmlStylesheet.transformToFragment(doc, document);
71             console.log(doc.firstChild);
72
73         if(doc.firstChild === null) {
74             options.error("Błąd w przetwarzaniu XML.");
75                 return;
76             }
77
78             error = $('parsererror', doc);
79         }
80
81         if (error.length > 0 && options.error) {
82             source = $('sourcetext', doc);
83             source_text = source.text();
84             source.text('');
85             options.error(error.text(), source_text);
86         } else {
87             options.success(doc.firstChild);
88
89             withThemes(function(canonThemes) {
90                 if (canonThemes != null) {
91                     $('.theme-text-list').addClass('canon').each(function(){
92                         var themes = $(this).html().split(',');
93                         for (i in themes) {
94                             themes[i] = $.trim(themes[i]);
95                             if (canonThemes.indexOf(themes[i]) == -1)
96                                 themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
97                         }
98                         $(this).html(themes.join(', '));
99                     });
100                 }
101             });
102         }
103     }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
104 }
105
106 /* USEFULL CONSTANTS */
107 const ELEMENT_NODE                                       = 1;
108 const ATTRIBUTE_NODE                 = 2;
109 const TEXT_NODE                      = 3;
110 const CDATA_SECTION_NODE             = 4;
111 const ENTITY_REFERENCE_NODE          = 5;
112 const ENTITY_NODE                    = 6;
113 const PROCESSING_INSTRUCTION_NODE    = 7;
114 const COMMENT_NODE                   = 8;
115 const DOCUMENT_NODE                  = 9;
116 const DOCUMENT_TYPE_NODE             = 10;
117 const DOCUMENT_FRAGMENT_NODE         = 11;
118 const NOTATION_NODE                  = 12;
119 const XATTR_RE = /^x-attr-name-(.*)$/;
120
121 const ELEM_START = 1;
122 const ELEM_END = 2;
123 const NS_END = 3;
124
125 const NAMESPACES = {
126         // namespaces not listed here will be assigned random names
127         "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
128         "http://purl.org/dc/elements/1.1/": "dc",
129         "http://www.w3.org/XML/1998/namespace": "xml"
130 };
131
132 function HTMLSerializer() {
133         // empty constructor
134 }
135
136
137
138 HTMLSerializer.prototype._prepare = function() {
139         this.stack = [];
140
141         // XML namespace is implicit
142         this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
143
144         this.result = "";
145         this.nsCounter = 1;
146 }
147
148 HTMLSerializer.prototype._pushElement = function(element) {
149         this.stack.push({
150                 "type": ELEM_START,
151                 "node": element
152         });
153 }
154
155 HTMLSerializer.prototype._pushChildren = function(element) {
156         for(var i = element.childNodes.length-1; i >= 0; i--)
157                 this._pushElement(element.childNodes.item(i));
158 }
159
160 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
161         this.stack.push({
162                 "type": ELEM_END,
163                 "tagName": tagName
164         });
165 }
166
167 HTMLSerializer.prototype._verseBefore = function(node) {
168     /* true if previous element is a previous verse of a stanza */
169     var parent = node.parentNode;
170     if (!parent || !parent.hasAttribute('x-node') || parent.getAttribute('x-node') != 'strofa')
171         return false;
172
173         var prev = node.previousSibling;
174
175         while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
176                 prev = prev.previousSibling;
177         }
178
179         return (prev !== null) && prev.hasAttribute('x-verse');
180 }
181
182 HTMLSerializer.prototype._nodeIgnored = function(node) {
183     return node.getAttribute('x-node') == 'wers';
184 }
185
186 HTMLSerializer.prototype._ignoredWithWhitespace = function(node) {
187     while (node.nodeType == ELEMENT_NODE && this._nodeIgnored(node) && node.childNodes.length > 0)
188         node = node.childNodes[0];
189     if (node.nodeType == TEXT_NODE)
190         return node.nodeValue.match(/^\s/)
191     else return false;
192 }
193
194
195 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
196 {
197         var self = this;
198         self._prepare();
199
200         if(!stripOuter)
201                 self._pushElement(rootElement);
202         else
203                 self._pushChildren(rootElement);
204
205     var text_buffer = '';
206
207         while(self.stack.length > 0) {
208                 var token = self.stack.pop();
209
210         if(token.type === ELEM_END) {
211             self.result += text_buffer;
212             text_buffer = '';
213             if (token.tagName != '')
214                 self.result += "</" + token.tagName + ">";
215             continue;
216         };
217
218                 if(token.type === NS_END) {
219                         self._unassignNamespace(token.namespace);
220                         continue;
221                 }
222
223
224                 switch(token.node.nodeType) {
225                         case ELEMENT_NODE:
226                                 if(token.node.hasAttribute('x-pass-thru')
227                                  || token.node.hasAttribute('data-pass-thru')) {
228                                         self._pushChildren(token.node);
229                                         break;
230                                 }
231
232                                 if(!token.node.hasAttribute('x-node'))
233                                         break;
234
235                                 var xnode = token.node.getAttribute('x-node');
236
237                                 if(xnode === 'out-of-flow-text') {
238                                         self._pushChildren(token.node);
239                                         break;
240                                 }
241
242                 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
243                     self.result += '/';
244                     // add whitespace if there's none
245                     if (!(text_buffer.match(/^\s/) || self._ignoredWithWhitespace(token.node)))
246                         self.result += ' ';
247                 }
248
249                 self.result += text_buffer;
250                 text_buffer = '';
251                                 self._serializeElement(token.node);
252                                 break;
253                         case TEXT_NODE:
254                                 self.result += text_buffer;
255                                 text_buffer = token.node.nodeValue;
256                                 break;
257                 };
258         };
259     self.result += text_buffer;
260
261         return this.result;
262 }
263
264 /*
265  * TODO: this doesn't support prefix redefinitions
266  */
267 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
268         this.nsMap[nsData.uri] = undefined;
269 };
270
271 HTMLSerializer.prototype._assignNamespace = function(uri) {
272         if(uri === null) {
273                 // default namespace
274                 return ({"prefix": "", "uri": "", "fresh": false});
275         }
276
277         if(this.nsMap[uri] === undefined) {
278                 // this prefix hasn't been defined yet in current context
279                 var prefix = NAMESPACES[uri];
280
281                 if (prefix === undefined) { // not predefined
282                         prefix = "ns" + this.nsCounter;
283                         this.nsCounter += 1;
284                 }
285
286                 this.nsMap[uri] = prefix;
287                 return ({
288                         "prefix": prefix,
289                         "uri": uri,
290                         "fresh": true
291                 });
292         }
293
294         return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
295 };
296
297 HTMLSerializer.prototype._join = function(prefix, name) {
298         if(!!prefix)
299                 return prefix + ":" + name;
300         return name;
301 };
302
303 HTMLSerializer.prototype._rjoin = function(prefix, name) {
304         if(!!name)
305                 return prefix + ":" + name;
306         return prefix;
307 };
308
309 HTMLSerializer.prototype._serializeElement = function(node) {
310     var self = this;
311
312     if (self._nodeIgnored(node)) {
313         self._pushTagEnd('');
314         self._pushChildren(node);
315     }
316     else {
317         var ns = node.getAttribute('x-ns');
318         var nsPrefix = null;
319         var newNamespaces = [];
320
321         var nsData = self._assignNamespace(node.getAttribute('x-ns'));
322
323         if(nsData.fresh) {
324                 newNamespaces.push(nsData);
325                 self.stack.push({
326                         "type": NS_END,
327                         "namespace": nsData
328                 });
329         }
330
331         var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
332
333         /* retrieve attributes */
334         var attributeIDs = [];
335         for (var i = 0; i < node.attributes.length; i++) {
336                 var attr = node.attributes.item(i);
337
338                 // check if name starts with "x-attr-name"
339                 var m = attr.name.match(XATTR_RE);
340                 if (m !== null)
341                         attributeIDs.push(m[1]);
342         };
343
344         /* print out */
345
346         self.result += '<' + tagName;
347
348         $.each(attributeIDs, function() {
349                 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
350
351                 if(nsData.fresh) {
352                         newNamespaces.push(nsData);
353                         self.stack.push({
354                                 "type": NS_END,
355                                 "namespace": nsData
356                         });
357                 };
358
359                 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
360                 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
361         });
362
363         /* print new namespace declarations */
364         $.each(newNamespaces, function() {
365                 self.result += " " + self._rjoin("xmlns", this.prefix);
366                 self.result += '="' + this.uri + '"';
367         });
368
369         if (node.childNodes.length > 0) {
370                 self.result += ">";
371                 self._pushTagEnd(tagName);
372                 self._pushChildren(node);
373         }
374         else {
375                 self.result += "/>";
376         };
377     }
378 };
379
380 function html2text(params) {
381         try {
382                 var s = new HTMLSerializer();
383                 params.success( s.serialize(params.element, params.stripOuter) );
384         } catch(e) {
385                 params.error("Nie udało się zserializować tekstu:" + e)
386         }
387 }