From a11cf903969595b065b46cc991d70c43035af76d Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Rekucki?= Date: Fri, 12 Mar 2010 11:07:24 +0100 Subject: [PATCH] Complete serialization with pretty printing. Works both in FF and Webkit. This fixes #358. --- platforma/static/js/main.js | 2 +- platforma/static/js/xslt.js | 54 ++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/platforma/static/js/main.js b/platforma/static/js/main.js index 0baf04ad..b98e1c20 100644 --- a/platforma/static/js/main.js +++ b/platforma/static/js/main.js @@ -616,7 +616,7 @@ function html(element) { xml2html({ xml: '<' + nodeName + '>' + insertedText + '', success: function(element) { - $box.html($(element).html()); + $origin.html($(element).html()); $overlay.remove(); }, error: function(text) { diff --git a/platforma/static/js/xslt.js b/platforma/static/js/xslt.js index 750acb2e..6cc545a5 100644 --- a/platforma/static/js/xslt.js +++ b/platforma/static/js/xslt.js @@ -43,15 +43,21 @@ function xml2html(options) { var error = $('parsererror', doc); if (error.length == 0) { - doc = xml2htmlStylesheet.transformToDocument(doc); - console.log(doc); + doc = xml2htmlStylesheet.transformToFragment(doc, document); + console.log(doc.firstChild); + + if(doc.firstChild === null) { + options.error("Błąd w przetwarzaniu XML."); + return; + } + error = $('parsererror', doc); } if (error.length > 0 && options.error) { options.error(error.text()); } else { - options.success(document.importNode(doc.documentElement, true)); + options.success(doc.firstChild); } }, function() { options.error && options.error('Nie udało się załadować XSLT'); }); } @@ -123,13 +129,27 @@ const PADDING = { "wers_akap": 1, "wers_wciety": 1, - "rdf:RDF": 3, + "rdf:RDF": 3, + "rdf:Description": 1, +}; + +function getPadding(name) { + + if(name.match(/^dc:.*$/)) + return -1; + + if(PADDING[name]) + return PADDING[name]; + + return 0; } function HTMLSerializer() { // empty constructor } + + HTMLSerializer.prototype._prepare = function() { this.stack = []; @@ -170,20 +190,23 @@ HTMLSerializer.prototype._verseBefore = function(node) { return false; } -HTMLSerializer.prototype.serialize = function(rootElement) +HTMLSerializer.prototype.serialize = function(rootElement, stripOuter) { var self = this; self._prepare(); - self._pushElement(rootElement); + + if(!stripOuter) + self._pushElement(rootElement); + else + self._pushChildren(rootElement); while(self.stack.length > 0) { var token = self.stack.pop(); if(token.type === ELEM_END) { - self.result += ""; - if (PADDING[token.tagName]) { - for(var n=0; n < PADDING[token.tagName]; n++) - self.result += "\n"; + self.result += ""; + for(var padding = getPadding(token.tagName); padding > 0; padding--) { + self.result += "\n"; } continue; }; @@ -305,6 +328,9 @@ HTMLSerializer.prototype._serializeElement = function(node) { }; /* print out */ + if (getPadding(tagName)) + self.result += '\n'; + self.result += '<' + tagName; $.each(attributeIDs, function() { @@ -338,14 +364,10 @@ HTMLSerializer.prototype._serializeElement = function(node) { }; }; -function html2text(params) { - if (params.stripOuter) { - // ... - }; - +function html2text(params) { try { var s = new HTMLSerializer(); - params.success( s.serialize(params.element) ); + params.success( s.serialize(params.element, params.stripOuter) ); } catch(e) { params.error("Nie udało się zserializować tekstu:" + e) } -- 2.20.1