6 function createXSLT(xsl) {
7 var p = new XSLTProcessor();
8 p.importStylesheet(xsl);
12 var xml2htmlStylesheet = null;
14 // Wykonuje block z załadowanymi arkuszami stylów
15 function withStylesheets(code_block, onError)
17 if (!xml2htmlStylesheet) {
18 $.blockUI({message: 'Ładowanie arkuszy stylów...'});
20 url: STATIC_URL + 'xsl/wl2html_client.xsl?20170727',
23 success: function(data) {
24 xml2htmlStylesheet = createXSLT(data);
38 // Wykonuje block z załadowanymi kanonicznymi motywami
39 function withThemes(code_block, onError)
41 if (typeof withThemes.canon == 'undefined') {
43 url: '/editor/themes',
45 success: function(data) {
46 withThemes.canon = data.split('\n');
47 code_block(withThemes.canon);
50 withThemes.canon = null;
51 code_block(withThemes.canon);
56 code_block(withThemes.canon);
61 function xml2html(options) {
62 withStylesheets(function() {
63 var xml = options.xml.replace(/\/(\s+)/g, '<br />$1');
64 xml = xml.replace(/([^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+)/g, '<alien>$1</alien>');
65 var parser = new DOMParser();
66 var serializer = new XMLSerializer();
67 var doc = parser.parseFromString(xml, 'text/xml');
68 var error = $('parsererror', doc);
70 if (error.length == 0) {
71 doc = xml2htmlStylesheet.transformToFragment(doc, document);
72 console.log(doc.firstChild);
74 if(doc.firstChild === null) {
75 options.error("Błąd w przetwarzaniu XML.");
79 error = $('parsererror', doc);
82 if (error.length > 0 && options.error) {
83 source = $('sourcetext', doc);
84 source_text = source.text();
86 options.error(error.text(), source_text);
88 options.success(doc.childNodes);
90 withThemes(function(canonThemes) {
91 if (canonThemes != null) {
92 $('.theme-text-list').addClass('canon').each(function(){
93 var themes = $(this).html().split(',');
95 themes[i] = $.trim(themes[i]);
96 if (canonThemes.indexOf(themes[i]) == -1)
97 themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
99 $(this).html(themes.join(', '));
104 }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
107 /* USEFULL CONSTANTS */
108 const ELEMENT_NODE = 1;
109 const ATTRIBUTE_NODE = 2;
111 const CDATA_SECTION_NODE = 4;
112 const ENTITY_REFERENCE_NODE = 5;
113 const ENTITY_NODE = 6;
114 const PROCESSING_INSTRUCTION_NODE = 7;
115 const COMMENT_NODE = 8;
116 const DOCUMENT_NODE = 9;
117 const DOCUMENT_TYPE_NODE = 10;
118 const DOCUMENT_FRAGMENT_NODE = 11;
119 const NOTATION_NODE = 12;
120 const XATTR_RE = /^x-attr-name-(.*)$/;
122 const ELEM_START = 1;
127 // namespaces not listed here will be assigned random names
128 "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
129 "http://purl.org/dc/elements/1.1/": "dc",
130 "http://www.w3.org/XML/1998/namespace": "xml"
133 function HTMLSerializer() {
139 HTMLSerializer.prototype._prepare = function() {
142 // XML namespace is implicit
143 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
149 HTMLSerializer.prototype._pushElement = function(element) {
156 HTMLSerializer.prototype._pushChildren = function(element) {
157 for(var i = element.childNodes.length-1; i >= 0; i--)
158 this._pushElement(element.childNodes.item(i));
161 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
168 HTMLSerializer.prototype._verseBefore = function(node) {
169 /* true if previous element is a previous verse of a stanza */
170 var parent = node.parentNode;
171 if (!parent || !parent.hasAttribute('x-node') || parent.getAttribute('x-node') != 'strofa')
174 var prev = node.previousSibling;
176 while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
177 prev = prev.previousSibling;
180 return (prev !== null) && prev.hasAttribute('x-verse');
183 HTMLSerializer.prototype._nodeIgnored = function(node) {
184 return node.getAttribute('x-node') == 'wers';
187 HTMLSerializer.prototype._ignoredWithWhitespace = function(node) {
188 while (node.nodeType == ELEMENT_NODE && this._nodeIgnored(node) && node.childNodes.length > 0)
189 node = node.childNodes[0];
190 if (node.nodeType == TEXT_NODE)
191 return node.nodeValue.match(/^\s/)
196 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
202 self._pushElement(rootElement);
204 self._pushChildren(rootElement);
206 var text_buffer = '';
208 while(self.stack.length > 0) {
209 var token = self.stack.pop();
211 if(token.type === ELEM_END) {
212 self.result += text_buffer;
214 if (token.tagName != '')
215 self.result += "</" + token.tagName + ">";
219 if(token.type === NS_END) {
220 self._unassignNamespace(token.namespace);
225 switch(token.node.nodeType) {
227 if(token.node.hasAttribute('x-pass-thru')
228 || token.node.hasAttribute('data-pass-thru')) {
229 self._pushChildren(token.node);
233 if(!token.node.hasAttribute('x-node'))
236 var xnode = token.node.getAttribute('x-node');
238 if(xnode === 'out-of-flow-text') {
239 self._pushChildren(token.node);
243 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
245 // add whitespace if there's none
246 if (!(text_buffer.match(/^\s/) || self._ignoredWithWhitespace(token.node)))
250 self.result += text_buffer;
252 self._serializeElement(token.node);
255 self.result += text_buffer;
256 text_buffer = token.node.nodeValue.replace(/&/g, '&').replace(/</g, '<');
259 self.result += text_buffer;
261 self.result += '<!--' + token.node.nodeValue + '-->';
265 self.result += text_buffer;
271 * TODO: this doesn't support prefix redefinitions
273 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
274 this.nsMap[nsData.uri] = undefined;
277 HTMLSerializer.prototype._assignNamespace = function(uri) {
280 return ({"prefix": "", "uri": "", "fresh": false});
283 if(this.nsMap[uri] === undefined) {
284 // this prefix hasn't been defined yet in current context
285 var prefix = NAMESPACES[uri];
287 if (prefix === undefined) { // not predefined
288 prefix = "ns" + this.nsCounter;
292 this.nsMap[uri] = prefix;
300 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
303 HTMLSerializer.prototype._join = function(prefix, name) {
305 return prefix + ":" + name;
309 HTMLSerializer.prototype._rjoin = function(prefix, name) {
311 return prefix + ":" + name;
315 HTMLSerializer.prototype._serializeElement = function(node) {
318 if (self._nodeIgnored(node)) {
319 self._pushTagEnd('');
320 self._pushChildren(node);
323 var ns = node.getAttribute('x-ns');
325 var newNamespaces = [];
327 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
330 newNamespaces.push(nsData);
337 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
339 /* retrieve attributes */
340 var attributeIDs = [];
341 for (var i = 0; i < node.attributes.length; i++) {
342 var attr = node.attributes.item(i);
344 // check if name starts with "x-attr-name"
345 var m = attr.name.match(XATTR_RE);
347 attributeIDs.push(m[1]);
352 self.result += '<' + tagName;
354 $.each(attributeIDs, function() {
355 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
358 newNamespaces.push(nsData);
365 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
366 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
369 /* print new namespace declarations */
370 $.each(newNamespaces, function() {
371 self.result += " " + self._rjoin("xmlns", this.prefix);
372 self.result += '="' + this.uri + '"';
375 if (node.childNodes.length > 0) {
377 self._pushTagEnd(tagName);
378 self._pushChildren(node);
386 function html2text(params) {
388 var s = new HTMLSerializer();
389 params.success( s.serialize(params.element, params.stripOuter) );
391 params.error("Nie udało się zserializować tekstu:" + e)