From 3ffb4cfcbc5dbbbe4926b48bd44ab73c037fe504 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Rekucki?= Date: Thu, 11 Mar 2010 22:14:38 +0100 Subject: [PATCH] Removed buggy html2wl XSLT - replaced with a Javascript HTML serializer. --- platforma/static/css/html.css | 2 +- platforma/static/js/main.js | 3 +- platforma/static/js/xslt.js | 132 ++++++++++++++++++------ platforma/static/xsl/html2wl_client.xsl | 100 ------------------ platforma/static/xsl/wl2html_client.xsl | 14 +-- 5 files changed, 108 insertions(+), 143 deletions(-) delete mode 100644 platforma/static/xsl/html2wl_client.xsl diff --git a/platforma/static/css/html.css b/platforma/static/css/html.css index 22be1ebb..fc8988f5 100644 --- a/platforma/static/css/html.css +++ b/platforma/static/css/html.css @@ -9,7 +9,7 @@ padding-left: 55px; } -.htmlview *[x-node='rdf:RDF'] { +.htmlview *[x-node='RDF'] { display: none; } diff --git a/platforma/static/js/main.js b/platforma/static/js/main.js index 8b55af96..a044b87d 100644 --- a/platforma/static/js/main.js +++ b/platforma/static/js/main.js @@ -329,7 +329,8 @@ function reverseTransform(editor, cont, errorCont, dontBlock) { } setTimeout(function() { html2xml({ - xml: serializer.serializeToString($('#html-view div').get(0)), + htmlElement: $('#html-view div').get(0), + /* xml: serializer.serializeToString($('#html-view div').get(0)), */ success: function(text) { editor.setCode(text); if (!dontBlock) { diff --git a/platforma/static/js/xslt.js b/platforma/static/js/xslt.js index c3f13d6a..b12bc774 100644 --- a/platforma/static/js/xslt.js +++ b/platforma/static/js/xslt.js @@ -71,7 +71,7 @@ function serialize(element, mode) { result.push('<'); result.push(element.tagName); - // Mozilla nie uważa deklaracji namespace za atrybuty + // Mozilla nie uważa deklaracji namespace za atrybuty | --lqc: bo nie są one atrybutami! var ns = element.tagName.indexOf(':'); if (ns != -1 && $.browser.mozilla) { result.push(' xmlns:'); @@ -153,52 +153,120 @@ function withStylesheets(block, onError) { function xml2html(options) { withStylesheets(function() { - var xml = options.xml.replace(/\/\s+/g, '
'); + var xml = options.xml.replace(/\/\s+/g, '
'); var parser = new DOMParser(); var serializer = new XMLSerializer(); - var doc = parser.parseFromString(xml, 'text/xml'); + var doc = parser.parseFromString(xml, 'text/xml'); var error = $('parsererror', doc); if (error.length == 0) { - doc = xml2htmlStylesheet.transformToFragment(doc, document); + doc = xml2htmlStylesheet.transformToDocument(doc); + console.log(doc); error = $('parsererror', doc); } if (error.length > 0 && options.error) { options.error(error.text()); - } else { - options.success(doc.firstChild); + } else { + options.success(document.importNode(doc.documentElement, true)); } }, function() { options.error && options.error('Nie udało się załadować XSLT'); }); } +/* USEFULL CONSTANTS */ +const ELEMENT_NODE = 1; +const ATTRIBUTE_NODE = 2; +const TEXT_NODE = 3; +const CDATA_SECTION_NODE = 4; +const ENTITY_REFERENCE_NODE = 5; +const ENTITY_NODE = 6; +const PROCESSING_INSTRUCTION_NODE = 7; +const COMMENT_NODE = 8; +const DOCUMENT_NODE = 9; +const DOCUMENT_TYPE_NODE = 10; +const DOCUMENT_FRAGMENT_NODE = 11; +const NOTATION_NODE = 12; +const XATTR_RE = /^x-attr-name-(.*)$/; -function html2xml(options) { - withStylesheets(function() { - var xml = options.xml; - var parser = new DOMParser(); - var serializer = new XMLSerializer(); - var doc = parser.parseFromString(xml, 'text/xml'); - var error = $('parsererror', doc.documentElement); +const ELEM_START = 1; +const ELEM_END = 2; - if (error.length == 0) { - doc = html2xmlStylesheet.transformToDocument(doc); - error = $('parsererror', doc.documentElement); - } - - if (error.length > 0 && options.error) { - options.error(error.text()); - } else { - if (options.inner) { - var result = []; - for (var i = 0; i < doc.documentElement.childNodes.length; i++) { - result.push(serialize(doc.documentElement.childNodes[i]).join('')); - }; - options.success(result.join('')); - } else { - options.success(serialize(doc.documentElement).join('')); - } - } - }, function() { options.error && options.error('Nie udało się załadować XSLT'); }); +const NAMESPACES = { + undefined: "", + null: "", + "": "", + "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf:", + "http://purl.org/dc/elements/1.1/": "dc:", + "http://www.w3.org/XML/1998/namespace": "xml:" }; +function html2text(rootElement) { + var stack = []; + var result = ""; + + stack.push([ELEM_START, rootElement]); + console.log("SERIALIZING") + + while( stack.length > 0) { + var pair = stack.pop(); + + var event = pair[0]; + var node = pair[1]; + + // console.log("NODE", event, node); + + if(event == ELEM_END) { + result += "\n"; + continue; + }; + + switch(node.nodeType) { + case ELEMENT_NODE: + if(!node.hasAttribute('x-node')) + break; + + var tag_name = NAMESPACES[node.getAttribute('x-ns')] + node.getAttribute('x-node'); + // console.log("ELEMENT: ", tag_name); + + /* retrieve attributes */ + var attr_ids = []; + for(var i=0; i < node.attributes.length; i++) { + var attr = node.attributes.item(i); + + // check if name starts with "x-attr-name" + var m = attr.name.match(XATTR_RE); + if(m !== null) + attr_ids.push(m[1]); + + }; + + result += '<' + tag_name; + + $.each(attr_ids, function() { + result += ' ' + NAMESPACES[node.getAttribute('x-attr-ns-'+this)]; + result += node.getAttribute('x-attr-name-'+this); + result += '="'+node.getAttribute('x-attr-value-'+this) +'"'; + }); + result += '>' + + stack.push([ELEM_END, tag_name]); + for(var i = node.childNodes.length-1; i >= 0; i--) + stack.push([ELEM_START, node.childNodes.item(i)]); + + break; + case TEXT_NODE: + result += node.nodeValue; + break; + } + } + + return result; +} + +function html2xml(options) { + try { + return options.success(html2text(options.htmlElement)); + } catch(e) { + options.error("Nie udało się zserializować tekstu:" + e) + } +}; \ No newline at end of file diff --git a/platforma/static/xsl/html2wl_client.xsl b/platforma/static/xsl/html2wl_client.xsl deleted file mode 100644 index d2a25af9..00000000 --- a/platforma/static/xsl/html2wl_client.xsl +++ /dev/null @@ -1,100 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - / - - - - - / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - / - - - - - / - - - - - - \ No newline at end of file diff --git a/platforma/static/xsl/wl2html_client.xsl b/platforma/static/xsl/wl2html_client.xsl index 32914f9a..3df7359b 100644 --- a/platforma/static/xsl/wl2html_client.xsl +++ b/platforma/static/xsl/wl2html_client.xsl @@ -1,3 +1,4 @@ + @@ -5,11 +6,7 @@ - - - + - + @@ -760,7 +756,7 @@ - + -- 2.20.1