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?20110520',
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 ALIEN_REGEX = /([^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+)/g;
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);
73 if (error.length == 0) {
74 doc = xml2htmlStylesheet.transformToFragment(doc, document);
75 console.log(doc.firstChild);
77 if(doc.firstChild === null) {
78 options.error("Błąd w przetwarzaniu XML.");
82 error = $('parsererror', doc);
85 if (error.length > 0 && options.error) {
86 source = $('sourcetext', doc);
87 source_text = source.text();
89 options.error(error.text(), source_text);
91 options.success(doc.childNodes);
93 withThemes(function(canonThemes) {
94 if (canonThemes != null) {
95 $('.theme-text-list').addClass('canon').each(function(){
96 var themes = $(this).html().split(',');
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>"
102 $(this).html(themes.join(', '));
107 }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
110 /* USEFULL CONSTANTS */
111 const ELEMENT_NODE = 1;
112 const ATTRIBUTE_NODE = 2;
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-(.*)$/;
125 const ELEM_START = 1;
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"
136 function HTMLSerializer() {
142 HTMLSerializer.prototype._prepare = function() {
145 // XML namespace is implicit
146 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
152 HTMLSerializer.prototype._pushElement = function(element) {
159 HTMLSerializer.prototype._pushChildren = function(element) {
160 for(var i = element.childNodes.length-1; i >= 0; i--)
161 this._pushElement(element.childNodes.item(i));
164 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
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')
177 var prev = node.previousSibling;
179 while((prev !== null) && (prev.nodeType != ELEMENT_NODE)) {
180 prev = prev.previousSibling;
183 return (prev !== null) && prev.hasAttribute('x-verse');
186 HTMLSerializer.prototype._nodeIgnored = function(node) {
187 return node.getAttribute('x-node') == 'wers';
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/)
199 HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
205 self._pushElement(rootElement);
207 self._pushChildren(rootElement);
209 var text_buffer = '';
211 while(self.stack.length > 0) {
212 var token = self.stack.pop();
214 if(token.type === ELEM_END) {
215 self.result += text_buffer;
217 if (token.tagName != '')
218 self.result += "</" + token.tagName + ">";
222 if(token.type === NS_END) {
223 self._unassignNamespace(token.namespace);
228 switch(token.node.nodeType) {
230 if(token.node.hasAttribute('x-pass-thru')
231 || token.node.hasAttribute('data-pass-thru')) {
232 self._pushChildren(token.node);
236 if(!token.node.hasAttribute('x-node'))
239 var xnode = token.node.getAttribute('x-node');
241 if(xnode === 'out-of-flow-text') {
242 self._pushChildren(token.node);
246 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
248 // add whitespace if there's none
249 if (!(text_buffer.match(/^\s/) || self._ignoredWithWhitespace(token.node)))
253 self.result += text_buffer;
255 self._serializeElement(token.node);
258 self.result += text_buffer;
259 text_buffer = token.node.nodeValue.replace(/&/g, '&').replace(/</g, '<');
262 self.result += text_buffer;
264 self.result += '<!--' + token.node.nodeValue + '-->';
268 self.result += text_buffer;
274 * TODO: this doesn't support prefix redefinitions
276 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
277 this.nsMap[nsData.uri] = undefined;
280 HTMLSerializer.prototype._assignNamespace = function(uri) {
283 return ({"prefix": "", "uri": "", "fresh": false});
286 if(this.nsMap[uri] === undefined) {
287 // this prefix hasn't been defined yet in current context
288 var prefix = NAMESPACES[uri];
290 if (prefix === undefined) { // not predefined
291 prefix = "ns" + this.nsCounter;
295 this.nsMap[uri] = prefix;
303 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
306 HTMLSerializer.prototype._join = function(prefix, name) {
308 return prefix + ":" + name;
312 HTMLSerializer.prototype._rjoin = function(prefix, name) {
314 return prefix + ":" + name;
318 HTMLSerializer.prototype._serializeElement = function(node) {
321 if (self._nodeIgnored(node)) {
322 self._pushTagEnd('');
323 self._pushChildren(node);
326 var ns = node.getAttribute('x-ns');
328 var newNamespaces = [];
330 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
333 newNamespaces.push(nsData);
340 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
342 /* retrieve attributes */
343 var attributeIDs = [];
344 for (var i = 0; i < node.attributes.length; i++) {
345 var attr = node.attributes.item(i);
347 // check if name starts with "x-attr-name"
348 var m = attr.name.match(XATTR_RE);
350 attributeIDs.push(m[1]);
355 self.result += '<' + tagName;
357 $.each(attributeIDs, function() {
358 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
361 newNamespaces.push(nsData);
368 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
369 self.result += '="'+node.getAttribute('x-attr-value-'+this).replace(/&/g, '&').replace(/</g, '<')+'"';
372 /* print new namespace declarations */
373 $.each(newNamespaces, function() {
374 self.result += " " + self._rjoin("xmlns", this.prefix);
375 self.result += '="' + this.uri + '"';
378 if (node.childNodes.length > 0) {
380 self._pushTagEnd(tagName);
381 self._pushChildren(node);
389 function html2text(params) {
391 var s = new HTMLSerializer();
392 params.success( s.serialize(params.element, params.stripOuter) );
394 params.error("Nie udało się zserializować tekstu:" + e)