2009/dap/ReSpec.js/js respec.js,1.167,1.168

Update of /sources/public/2009/dap/ReSpec.js/js
In directory hutz:/tmp/cvs-serv31260/js

Modified Files:
	respec.js 
Log Message:
try to fix the XML serialiser

Index: respec.js
===================================================================
RCS file: /sources/public/2009/dap/ReSpec.js/js/respec.js,v
retrieving revision 1.167
retrieving revision 1.168
diff -u -d -r1.167 -r1.168
--- respec.js	14 Sep 2011 15:03:45 -0000	1.167
+++ respec.js	21 Sep 2011 09:57:45 -0000	1.168
@@ -352,24 +352,18 @@
         var ats = document.documentElement.attributes;
         var prefixAtr = '' ;
 
-        var hasxmlns = 0 ;
+        var hasxmlns = false;
         for (var i = 0; i < ats.length; i++) {
             var an = ats[i].name;
-            if (an == "lang" ) {
-                continue ;
-            }
-            if (an == "xmlns" ) {
-                hasxmlns = 1;
-            }
+            if (an == "lang") continue;
+            if (an == "xmlns") hasxmlns = true;
             if (an == "prefix") {
                 prefixAtr = ats[i].value;
                 continue;
             }
             str += " " + an + "=\"" + this._esc(ats[i].value) + "\"";
         }
-        if (!hasxmlns) {
-            str += ' xmlns="http://www.w3.org/1999/xhtml"';
-        }
+        if (!hasxmlns) str += ' xmlns="http://www.w3.org/1999/xhtml"';
         if (this.doRDFa) {
             str += " xmlns:dcterms='http://purl.org/dc/terms/' xmlns:bibo='http://purl.org/ontology/bibo/' xmlns:foaf='http://xmlns.com/foaf/0.1/' xmlns:xsd='http://www.w3.org/2001/XMLSchema#'";
             if (prefixAtr != '') {
@@ -381,13 +375,15 @@
                 }
             }
         }
-
         str += ">\n";
         // walk the entire DOM tree grabbing nodes and emitting them - possibly modifying them
         // if they need the funny closing tag
         var pRef = this ;
-        var closers = [ "br", "img", "input", "area", "base", "basefont", "col", "isindex", "link", "meta", "param", "hr"] ;
-        var dumpNode = function(node) {
+        var selfClosing = {};
+        "br img input area base basefont col isindex link meta param hr".split(" ").forEach(function (n) {
+            selfClosing[n] = true;
+        })
+        var dumpNode = function (node) {
             var out = '' ;
             // if the node is the document node.. process the children
             if ( node.nodeType == 9 || ( node.nodeType == 1 && node.nodeName.toLowerCase() == 'html' ) ) {
@@ -398,54 +394,48 @@
                         out += dumpNode(children[i]) ;
                     }
                 }
-            } else 
-            // if the node is an element, process it
-            if ( node.nodeType == 1 ) {
+            } 
+            // element
+            else if (1 === node.nodeType) {
                 var ename = node.nodeName.toLowerCase() ;
-                var empty = 0 ;
-                for (var i = 0; i < closers.length; i++) {
-                    if (ename == closers[i]) {
-                        empty = 1;
-                    }
-                }
                 out += '<' + ename ;
-                var attrs = node.attributes;
-                var aLen = attrs.length ;
-                if (aLen) {
-                    for (var i = 0; i < aLen; i++) {
-                        out += " " + attrs[i].name + "=\"" + pRef._esc(attrs[i].value) + "\"";
-                    }
+                for (var i = 0; i < node.attributes.length; i++) {
+                    var atn = node.attributes[i]
+                    out += " " + atn.name + "=\"" + pRef._esc(atn.value) + "\"";
                 }
-                if (empty) {
+                if (selfClosing[ename]) {
                     out += ' />';
-                } else {
-                    out += '>' ;
-                    if ( ename == 'pre' ) {
-                        out += "\n" + node.innerHTML;
-                    } else {
-                        var children = node.childNodes;
-                        var cLen = children.length ;
-                        if (cLen) {
-                            for (var i = 0; i < cLen; i++) {
-                                out += dumpNode(children[i]) ;
-                            }
-                        }
-                    }
-                    out += '</' + ename + '>' ;
                 }
-            } else if (node.nodeType == 8 ) {
+                else {
+                    out += '>';
+                    // XXX removing this, as it does not seem correct at all
+                    // if ( ename == 'pre' ) {
+                    //     out += "\n" + node.innerHTML;
+                    // } 
+                    // else {
+                        for (var i = 0; i < node.childNodes.length; i++) out += dumpNode(node.childNodes[i]);
+                    // }
+                    out += '</' + ename + '>';
+                }
+            }
+            // comments
+            else if (8 === node.nodeType) {
                 out += "\n<!-- " + node.nodeValue + " -->\n";
-            } else {
-                // otherwise, return the content of the node as a string
-                out += node.nodeValue ;
             }
-            return out ;
+            // text or cdata
+            else if (3 === node.nodeType || 4 === node.nodeType) {
+                out += this._esc(node.nodeValue);
+            }
+            // we don't handle other types for the time being
+            else {
+                warning("Cannot handle serialising nodes of type: " + node.nodeType);
+            }
+            return out;
         };
         var node = document.documentElement;
         str += dumpNode(document.documentElement) ;
         str += "</html>";
         return str;
-
     },
     
     toDiffHTMLSource:  function () {

Received on Wednesday, 21 September 2011 09:57:50 UTC