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',
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') {
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 var parser = new DOMParser();
65 var serializer = new XMLSerializer();
66 var doc = parser.parseFromString(xml, 'text/xml');
67 var error = $('parsererror', doc);
69 if (error.length == 0) {
70 doc = xml2htmlStylesheet.transformToFragment(doc, document);
71 console.log(doc.firstChild);
73 if(doc.firstChild === null) {
74 options.error("Błąd w przetwarzaniu XML.");
78 error = $('parsererror', doc);
81 if (error.length > 0 && options.error) {
82 source = $('sourcetext', doc);
83 source_text = source.text();
85 options.error(error.text(), source_text);
87 options.success(doc.firstChild);
89 withThemes(function(canonThemes) {
90 if (canonThemes != null) {
91 $('.theme-text-list').addClass('canon').each(function(){
92 var themes = $(this).html().split(',');
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>"
98 $(this).html(themes.join(', '));
103 }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
106 /* USEFULL CONSTANTS */
107 const ELEMENT_NODE = 1;
108 const ATTRIBUTE_NODE = 2;
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-(.*)$/;
121 const ELEM_START = 1;
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"
132 function HTMLSerializer() {
138 HTMLSerializer.prototype._prepare = function() {
141 // XML namespace is implicit
142 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
148 HTMLSerializer.prototype._pushElement = function(element) {
155 HTMLSerializer.prototype._pushChildren = function(element) {
156 for(var i = element.childNodes.length-1; i >= 0; i--)
157 this._pushElement(element.childNodes.item(i));
160 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
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')
173 var prev = node.previousSibling;
175 while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
176 prev = prev.previousSibling;
179 return (prev !== null) && prev.hasAttribute('x-verse');
182 HTMLSerializer.prototype._nodeIgnored = function(node) {
183 return node.getAttribute('x-node') == 'wers';
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/)
195 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
201 self._pushElement(rootElement);
203 self._pushChildren(rootElement);
205 var text_buffer = '';
207 while(self.stack.length > 0) {
208 var token = self.stack.pop();
210 if(token.type === ELEM_END) {
211 self.result += text_buffer;
213 if (token.tagName != '')
214 self.result += "</" + token.tagName + ">";
218 if(token.type === NS_END) {
219 self._unassignNamespace(token.namespace);
224 switch(token.node.nodeType) {
226 if(token.node.hasAttribute('x-pass-thru')
227 || token.node.hasAttribute('data-pass-thru')) {
228 self._pushChildren(token.node);
232 if(!token.node.hasAttribute('x-node'))
235 var xnode = token.node.getAttribute('x-node');
237 if(xnode === 'out-of-flow-text') {
238 self._pushChildren(token.node);
242 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
244 // add whitespace if there's none
245 if (!(text_buffer.match(/^\s/) || self._ignoredWithWhitespace(token.node)))
249 self.result += text_buffer;
251 self._serializeElement(token.node);
254 self.result += text_buffer;
255 text_buffer = token.node.nodeValue;
264 * TODO: this doesn't support prefix redefinitions
266 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
267 this.nsMap[nsData.uri] = undefined;
270 HTMLSerializer.prototype._assignNamespace = function(uri) {
273 return ({"prefix": "", "uri": "", "fresh": false});
276 if(this.nsMap[uri] === undefined) {
277 // this prefix hasn't been defined yet in current context
278 var prefix = NAMESPACES[uri];
280 if (prefix === undefined) { // not predefined
281 prefix = "ns" + this.nsCounter;
285 this.nsMap[uri] = prefix;
293 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
296 HTMLSerializer.prototype._join = function(prefix, name) {
298 return prefix + ":" + name;
302 HTMLSerializer.prototype._rjoin = function(prefix, name) {
304 return prefix + ":" + name;
308 HTMLSerializer.prototype._serializeElement = function(node) {
311 if (self._nodeIgnored(node)) {
312 self._pushTagEnd('');
313 self._pushChildren(node);
316 var ns = node.getAttribute('x-ns');
318 var newNamespaces = [];
320 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
323 newNamespaces.push(nsData);
330 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
332 /* retrieve attributes */
333 var attributeIDs = [];
334 for (var i = 0; i < node.attributes.length; i++) {
335 var attr = node.attributes.item(i);
337 // check if name starts with "x-attr-name"
338 var m = attr.name.match(XATTR_RE);
340 attributeIDs.push(m[1]);
345 self.result += '<' + tagName;
347 $.each(attributeIDs, function() {
348 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
351 newNamespaces.push(nsData);
358 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
359 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
362 /* print new namespace declarations */
363 $.each(newNamespaces, function() {
364 self.result += " " + self._rjoin("xmlns", this.prefix);
365 self.result += '="' + this.uri + '"';
368 if (node.childNodes.length > 0) {
370 self._pushTagEnd(tagName);
371 self._pushChildren(node);
379 function html2text(params) {
381 var s = new HTMLSerializer();
382 params.success( s.serialize(params.element, params.stripOuter) );
384 params.error("Nie udało się zserializować tekstu:" + e)