-1
What is the best way to write the following function?
This?
function DOMtoString(document_root) {
var html = '',
node = document_root.firstChild;
while (node) {
switch (node.nodeType) {
case Node.ELEMENT_NODE:
html += node.outerHTML;
break;
case Node.TEXT_NODE:
html += node.nodeValue;
break;
case Node.CDATA_SECTION_NODE:
html += '<![CDATA[' + node.nodeValue + ']]>';
break;
case Node.COMMENT_NODE:
html += '<!--' + node.nodeValue + '-->';
break;
case Node.DOCUMENT_TYPE_NODE:
// (X)HTML documents are identified by public identifiers
html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
break;
}
node = node.nextSibling;
}
return html;
}
Or this?
function DOMtoString(a) {
var b = "",
c = a["firstChild"];
while (c) {
switch (c["nodeType"]) {
case Node["ELEMENT_NODE"]:
b += c["outerHTML"];
break;
case Node["TEXT_NODE"]:
b += c["nodeValue"];
break;
case Node["CDATA_SECTION_NODE"]:
b += "<![CDATA[" + c["nodeValue"] + "]]>";
break;
case Node["COMMENT_NODE"]:
b += "<!--" + c["nodeValue"] + "-->";
break;
case Node["DOCUMENT_TYPE_NODE"]:
b += "<!DOCTYPE " + c["name"] + (c["publicId"] ? " PUBLIC \"" + c["publicId"] + "\"" : "") + (!c["publicId"] && c["systemId"] ? " SYSTEM" : "") + (c["systemId"] ? " \"" + c["systemId"] + "\"" : "") + ">";
break;
}
c = c["nextSibling"];
}
return b;
}
What you see positive in the second version. The impression I have is that the first one (with the biggest names and comments) is the original one and the second one went through a code minificador.
– hugomg