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