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