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?20201110',
23 success: function(data) {
24 xml2htmlStylesheet = createXSLT(data);
38 function xml2html(options) {
39 withStylesheets(function() {
40 var xml = options.xml.replace(/\/(\s+)/g, '<br />$1');
41 xml = xml.replace(/([^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+)/g, '<alien>$1</alien>');
42 var parser = new DOMParser();
43 var serializer = new XMLSerializer();
44 var doc = parser.parseFromString(xml, 'text/xml');
45 var error = $('parsererror', doc);
47 if (error.length == 0) {
48 doc = xml2htmlStylesheet.transformToFragment(doc, document);
50 if(doc.firstChild === null) {
51 options.error("Błąd w przetwarzaniu XML.");
55 error = $('parsererror', doc);
58 if (error.length > 0 && options.error) {
59 source = $('sourcetext', doc);
60 source_text = source.text();
62 options.error(error.text(), source_text);
64 options.success(doc.childNodes);
66 $.themes.withCanon(function(canonThemes) {
67 if (canonThemes != null) {
68 $('.theme-text-list').addClass('canon').each(function(){
69 var themes = $(this).html().split(',');
71 themes[i] = $.trim(themes[i]);
72 if (canonThemes.indexOf(themes[i]) == -1)
73 themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
75 $(this).html(themes.join(', '));
80 }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
83 /* USEFULL CONSTANTS */
84 const ELEMENT_NODE = 1;
85 const ATTRIBUTE_NODE = 2;
87 const CDATA_SECTION_NODE = 4;
88 const ENTITY_REFERENCE_NODE = 5;
89 const ENTITY_NODE = 6;
90 const PROCESSING_INSTRUCTION_NODE = 7;
91 const COMMENT_NODE = 8;
92 const DOCUMENT_NODE = 9;
93 const DOCUMENT_TYPE_NODE = 10;
94 const DOCUMENT_FRAGMENT_NODE = 11;
95 const NOTATION_NODE = 12;
96 const XATTR_RE = /^x-attr-name-(.*)$/;
103 // namespaces not listed here will be assigned random names
104 "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
105 "http://purl.org/dc/elements/1.1/": "dc",
106 "http://www.w3.org/XML/1998/namespace": "xml"
109 function HTMLSerializer() {
115 HTMLSerializer.prototype._prepare = function() {
118 // XML namespace is implicit
119 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
125 HTMLSerializer.prototype._pushElement = function(element) {
132 HTMLSerializer.prototype._pushChildren = function(element) {
133 for(var i = element.childNodes.length-1; i >= 0; i--)
134 this._pushElement(element.childNodes.item(i));
137 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
144 HTMLSerializer.prototype._verseBefore = function(node) {
145 /* true if previous element is a previous verse of a stanza */
146 var parent = node.parentNode;
147 if (!parent || !parent.hasAttribute('x-node') || parent.getAttribute('x-node') != 'strofa')
150 var prev = node.previousSibling;
152 while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
153 prev = prev.previousSibling;
156 return (prev !== null) && prev.hasAttribute('x-verse');
159 HTMLSerializer.prototype._nodeIgnored = function(node) {
160 return node.getAttribute('x-auto-node') == 'true';
163 HTMLSerializer.prototype._ignoredWithWhitespace = function(node) {
164 while (node.nodeType == ELEMENT_NODE && this._nodeIgnored(node) && node.childNodes.length > 0)
165 node = node.childNodes[0];
166 if (node.nodeType == TEXT_NODE)
167 return node.nodeValue.match(/^\s/)
172 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
178 self._pushElement(rootElement);
180 self._pushChildren(rootElement);
182 var text_buffer = '';
184 while(self.stack.length > 0) {
185 var token = self.stack.pop();
187 if(token.type === ELEM_END) {
188 self.result += text_buffer;
190 if (token.tagName != '')
191 self.result += "</" + token.tagName + ">";
195 if(token.type === NS_END) {
196 self._unassignNamespace(token.namespace);
201 switch(token.node.nodeType) {
203 if(token.node.hasAttribute('x-pass-thru')
204 || token.node.hasAttribute('data-pass-thru')) {
205 self._pushChildren(token.node);
209 if(!token.node.hasAttribute('x-node'))
212 var xnode = token.node.getAttribute('x-node');
214 if(xnode === 'out-of-flow-text') {
215 self._pushChildren(token.node);
219 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
221 // add whitespace if there's none
222 if (!(text_buffer.match(/^\s/) || self._ignoredWithWhitespace(token.node)))
226 self.result += text_buffer;
228 self._serializeElement(token.node);
231 self.result += text_buffer;
232 text_buffer = token.node.nodeValue.replace(/&/g, '&').replace(/</g, '<');
235 self.result += text_buffer;
237 self.result += '<!--' + token.node.nodeValue + '-->';
241 self.result += text_buffer;
247 * TODO: this doesn't support prefix redefinitions
249 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
250 this.nsMap[nsData.uri] = undefined;
253 HTMLSerializer.prototype._assignNamespace = function(uri) {
256 return ({"prefix": "", "uri": "", "fresh": false});
259 if(this.nsMap[uri] === undefined) {
260 // this prefix hasn't been defined yet in current context
261 var prefix = NAMESPACES[uri];
263 if (prefix === undefined) { // not predefined
264 prefix = "ns" + this.nsCounter;
268 this.nsMap[uri] = prefix;
276 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
279 HTMLSerializer.prototype._join = function(prefix, name) {
281 return prefix + ":" + name;
285 HTMLSerializer.prototype._rjoin = function(prefix, name) {
287 return prefix + ":" + name;
291 HTMLSerializer.prototype._serializeElement = function(node) {
294 if (self._nodeIgnored(node)) {
295 self._pushTagEnd('');
296 self._pushChildren(node);
299 var ns = node.getAttribute('x-ns');
301 var newNamespaces = [];
303 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
306 newNamespaces.push(nsData);
313 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
315 /* retrieve attributes */
316 var attributeIDs = [];
317 for (var i = 0; i < node.attributes.length; i++) {
318 var attr = node.attributes.item(i);
320 // check if name starts with "x-attr-name"
321 var m = attr.name.match(XATTR_RE);
323 attributeIDs.push(m[1]);
328 self.result += '<' + tagName;
330 $.each(attributeIDs, function() {
331 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
334 newNamespaces.push(nsData);
341 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
342 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
345 /* print new namespace declarations */
346 $.each(newNamespaces, function() {
347 self.result += " " + self._rjoin("xmlns", this.prefix);
348 self.result += '="' + this.uri + '"';
351 if (node.childNodes.length > 0) {
353 self._pushTagEnd(tagName);
354 self._pushChildren(node);
362 function html2text(params) {
364 var s = new HTMLSerializer();
365 params.success( s.serialize(params.element, params.stripOuter) );
367 params.error("Nie udało się zserializować tekstu:" + e)