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',
22 success: function(data) {
23 xml2htmlStylesheet = createXSLT(data);
37 function xml2html(options) {
38 withStylesheets(function() {
39 var xml = options.xml.replace(/\/\s+/g, '<br />');
40 var parser = new DOMParser();
41 var serializer = new XMLSerializer();
42 var doc = parser.parseFromString(xml, 'text/xml');
43 var error = $('parsererror', doc);
45 if (error.length == 0) {
46 doc = xml2htmlStylesheet.transformToDocument(doc);
48 error = $('parsererror', doc);
51 if (error.length > 0 && options.error) {
52 options.error(error.text());
54 options.success(document.importNode(doc.documentElement, true));
56 }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
59 /* USEFULL CONSTANTS */
60 const ELEMENT_NODE = 1;
61 const ATTRIBUTE_NODE = 2;
63 const CDATA_SECTION_NODE = 4;
64 const ENTITY_REFERENCE_NODE = 5;
65 const ENTITY_NODE = 6;
66 const PROCESSING_INSTRUCTION_NODE = 7;
67 const COMMENT_NODE = 8;
68 const DOCUMENT_NODE = 9;
69 const DOCUMENT_TYPE_NODE = 10;
70 const DOCUMENT_FRAGMENT_NODE = 11;
71 const NOTATION_NODE = 12;
72 const XATTR_RE = /^x-attr-name-(.*)$/;
79 // namespaces not listed here will be assigned random names
80 "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
81 "http://purl.org/dc/elements/1.1/": "dc",
82 "http://www.w3.org/XML/1998/namespace": "xml"
86 * PADDING for pretty-printing
89 dramat_wierszowany_l: 4,
90 dramat_wierszowany_lp: 4,
91 dramat_wspolczesny: 4,
129 function HTMLSerializer() {
133 HTMLSerializer.prototype._prepare = function() {
136 // XML namespace is implicit
137 this.nsMap = {"http://www.w3.org/XML/1998/namespace": "xml"};
143 HTMLSerializer.prototype._pushElement = function(element) {
150 HTMLSerializer.prototype._pushChildren = function(element) {
151 for(var i = element.childNodes.length-1; i >= 0; i--)
152 this._pushElement(element.childNodes.item(i));
155 HTMLSerializer.prototype._pushTagEnd = function(tagName) {
162 HTMLSerializer.prototype._verseBefore = function(node) {
163 var prev = node.previousSibling;
166 if(prev.nodeType == ELEMENT_NODE && prev.hasAttribute('x-verse'))
173 HTMLSerializer.prototype.serialize = function(rootElement)
177 self._pushElement(rootElement);
179 while(self.stack.length > 0) {
180 var token = self.stack.pop();
182 if(token.type === ELEM_END) {
183 self.result += "</" + token.tagName + ">";
184 if (PADDING[token.tagName]) {
185 for(var n=0; n < PADDING[token.tagName]; n++)
191 if(token.type === NS_END) {
192 self._unassignNamespace(token.namespace);
197 switch(token.node.nodeType) {
199 if(token.node.hasAttribute('x-pass-thru')) {
200 self._pushChildren(token.node);
204 if(!token.node.hasAttribute('x-node'))
207 var xnode = token.node.getAttribute('x-node');
209 if(xnode === 'wers') {
211 if(self._verseBefore(token.node))
212 self.result += '/\n';
213 self._pushChildren(token.node);
217 if(token.node.hasAttribute('x-verse') && self._verseBefore(token.node)) {
218 self.result += '/\n';
221 self._serializeElement(token.node);
224 self.result += token.node.nodeValue;
233 * TODO: this doesn't support prefix redefinitions
235 HTMLSerializer.prototype._unassignNamespace = function(nsData) {
236 this.nsMap[nsData.uri] = undefined;
239 HTMLSerializer.prototype._assignNamespace = function(uri) {
242 return ({"prefix": "", "uri": "", "fresh": false});
245 if(this.nsMap[uri] === undefined) {
246 // this prefix hasn't been defined yet in current context
247 var prefix = NAMESPACES[uri];
249 if (prefix === undefined) { // not predefined
250 prefix = "ns" + this.nsCounter;
254 this.nsMap[uri] = prefix;
262 return ({"prefix": this.nsMap[uri], "uri": uri, "fresh": false});
265 HTMLSerializer.prototype._join = function(prefix, name) {
267 return prefix + ":" + name;
271 HTMLSerializer.prototype._rjoin = function(prefix, name) {
273 return prefix + ":" + name;
277 HTMLSerializer.prototype._serializeElement = function(node) {
280 var ns = node.getAttribute('x-ns');
282 var newNamespaces = [];
284 var nsData = self._assignNamespace(node.getAttribute('x-ns'));
287 newNamespaces.push(nsData);
294 var tagName = self._join(nsData.prefix, node.getAttribute('x-node'));
296 /* retrieve attributes */
297 var attributeIDs = [];
298 for (var i = 0; i < node.attributes.length; i++) {
299 var attr = node.attributes.item(i);
301 // check if name starts with "x-attr-name"
302 var m = attr.name.match(XATTR_RE);
304 attributeIDs.push(m[1]);
308 self.result += '<' + tagName;
310 $.each(attributeIDs, function() {
311 var nsData = self._assignNamespace(node.getAttribute('x-attr-ns-'+this));
314 newNamespaces.push(nsData);
321 self.result += ' ' + self._join(nsData.prefix, node.getAttribute('x-attr-name-'+this));
322 self.result += '="'+node.getAttribute('x-attr-value-'+this) +'"';
325 /* print new namespace declarations */
326 $.each(newNamespaces, function() {
327 self.result += " " + self._rjoin("xmlns", this.prefix);
328 self.result += '="' + this.uri + '"';
331 if (node.childNodes.length > 0) {
333 self._pushTagEnd(tagName);
334 self._pushChildren(node);
341 function html2text(params) {
342 if (params.stripOuter) {
347 var s = new HTMLSerializer();
348 params.success( s.serialize(params.element) );
350 params.error("Nie udało się zserializować tekstu:" + e)