[Laszlo-checkins] r13761 - openlaszlo/trunk/WEB-INF/lps/lfc/kernel/dhtml

bargull@openlaszlo.org bargull at openlaszlo.org
Tue Apr 28 09:00:13 PDT 2009


Author: bargull
Date: 2009-04-28 09:00:09 -0700 (Tue, 28 Apr 2009)
New Revision: 13761

Modified:
   openlaszlo/trunk/WEB-INF/lps/lfc/kernel/dhtml/LzXMLTranslator.js
Log:
Change 20090419-bargull-HV3 by bargull at dell--p4--2-53 on 2009-04-19 17:43:56
    in /home/Admin/src/svn/openlaszlo/trunk
    for http://svn.openlaszlo.org/openlaszlo/trunk

Summary: copy native xml-tree iteratively

New Features:

Bugs Fixed: LPP-8067 (Improve data performance) (DHTML)

Technical Reviewer: hminsky
QA Reviewer: (pending)
Doc Reviewer: (pending)

Documentation:

Release Notes:

Details:
Remove recursion in copyBrowserXML() when traversing the DOM tree.

Similar changes need to be made for the flash kernel (already in progress)
    

Tests:
smoke, alldata in dhtml (Firefox, Safari, Opera, IE)



Modified: openlaszlo/trunk/WEB-INF/lps/lfc/kernel/dhtml/LzXMLTranslator.js
===================================================================
--- openlaszlo/trunk/WEB-INF/lps/lfc/kernel/dhtml/LzXMLTranslator.js	2009-04-28 02:32:39 UTC (rev 13760)
+++ openlaszlo/trunk/WEB-INF/lps/lfc/kernel/dhtml/LzXMLTranslator.js	2009-04-28 16:00:09 UTC (rev 13761)
@@ -1,7 +1,7 @@
 /**
   * LzXMLTranslator.js
   *
-  * @copyright Copyright 2006-2008 Laszlo Systems, Inc.  All Rights Reserved.
+  * @copyright Copyright 2006-2009 Laszlo Systems, Inc.  All Rights Reserved.
   *            Use is subject to license terms.
   *
   * @topic Kernel
@@ -11,72 +11,157 @@
 /**
   * @shortdesc Utility for converting native XML DOM object into LzDataNode tree
   */
-
 var LzXMLTranslator = {
 
 whitespacePat: new RegExp("^\\s*$"),
 stringTrimPat: new RegExp("^\\s+|\\s+$", "g"),
 
+/**
+  * LzXMLTranslator interface
+  */
 copyXML: function (xmldoc, trimwhitespace, nsprefix) {
     var lfcnode = this.copyBrowserXML(xmldoc, true, trimwhitespace, nsprefix);
-    // create a new, empty ownerDocument (LPP-7537)
-    new LzDataElement(null, {}, [lfcnode]);
-    return lfcnode;
+    if (lfcnode instanceof LzDataElement) {
+        return lfcnode;
+    } else {
+        return null;
+    }
 },
 
-copyBrowserXML: function (node, ignorewhite, trimwhite, nsprefix) {
-    if (! node) {
-        return node;
-    } else if (node.nodeType == 3 || node.nodeType == 4) {
+/**
+  * Translate a single native XML DOM object into a LzDataNode.
+  * This function is inlined in copyBrowserXML
+  */
+copyBrowserNode: function (node, ignorewhite, trimwhite, nsprefix) {
+    var type = node.nodeType;
+    if (type == 3 || type == 4) {
         // text node (3: TEXT_NODE, 4: CDATA_SECTION_NODE)
         var nv = node.nodeValue;
-        
         // If ignorewhite is true, discard a text node which is all whitespace
-        if (ignorewhite && this.whitespacePat.test(nv)) {
-            return null;
+        if (! (ignorewhite && this.whitespacePat.test(nv))) {
+            if (trimwhite) {
+                nv = nv.replace(this.stringTrimPat, "");
+            }
+            return new LzDataText(nv);
         }
-        if (trimwhite) {
-            nv = nv.replace(this.stringTrimPat, "");
-        }
-        
-        return new LzDataText(nv);
-    } else if (node.nodeType == 1 || node.nodeType == 9) {
+    } else if (type == 1 || type == 9) {
         // element or document node (1: ELEMENT_NODE, 9: DOCUMENT_NODE)
+        // older IE's use "baseName" instead of DOM Level2 property "localName"
+        var nname = (!nsprefix && (node.localName || node.baseName)) || node.nodeName;
 
         var cattrs = {};
         var nattrs = node.attributes;
         if (nattrs) {
             // slow but sure way to copy attributes
-            for (var k = 0; k < nattrs.length; k++) {
-                var attrnode = nattrs.item(k);
+            for (var k = 0, len = nattrs.length; k < len; k++) {
+                var attrnode = nattrs[k];
                 if (attrnode) {
-                    //older IE's use "baseName" instead of DOM Level2 property "localName"
+                    // older IE's use "baseName" instead of DOM Level2 property "localName"
                     var attrname = (!nsprefix && (attrnode.localName || attrnode.baseName)) || attrnode.name;
                     cattrs[attrname] = attrnode.value;
                 }
             }
         }
 
-        //older IE's use "baseName" instead of DOM Level2 property "localName"
-        var nname = (!nsprefix && (node.localName || node.baseName)) || node.nodeName;
-        var lfcnode = new LzDataElement(nname, cattrs);
-        
-        if (node.hasChildNodes()) {
-            var newchildren = [];
-            var children = node.childNodes;
-            for (var i = 0; i < children.length; i++) {
-                var lfcchild = this.copyBrowserXML(children[i], ignorewhite, trimwhite, nsprefix);
-                if (lfcchild != null) {
-                    newchildren.push(lfcchild);
+        var lfcnode = new LzDataElement(nname);
+        // avoid copy of cattrs (see LzDataElement ctor)
+        lfcnode.attributes = cattrs;
+        return lfcnode;
+    } else {
+        // ignore other node types
+    }
+},
+
+/**
+  * Translate a native XML DOM tree into a LzDataNode tree
+  */
+copyBrowserXML: function (xmlnode, ignorewhite, trimwhite, nsprefix) {
+    // create a new, empty ownerDocument (LPP-7537)
+    var document = new LzDataElement(null);
+    // handle this separately so you don't need to worry about the
+    // case when xmlnode has got siblings
+    if (! xmlnode.firstChild) {
+        return document.appendChild(this.copyBrowserNode(xmlnode, ignorewhite, trimwhite, nsprefix));
+    }
+    var wsPat = this.whitespacePat;
+    var trimPat = this.stringTrimPat;
+    var lfcparent = document;
+    var next, node = xmlnode;
+    // traverse DOM tree
+    for (;;) {
+        var type = node.nodeType;
+        if (type == 3 || type == 4) {
+            // text node (3: TEXT_NODE, 4: CDATA_SECTION_NODE)
+
+            // this is inlined:
+            // var lfcnode = this.copyBrowserNode(node);
+            // lfcparent.appendChild(lfcnode);
+
+            var nv = node.nodeValue;
+            // If ignorewhite is true, discard a text node which is all whitespace
+            if (! (ignorewhite && wsPat.test(nv))) {
+                if (trimwhite) {
+                    nv = nv.replace(trimPat, "");
                 }
+                var lfcnode = new LzDataText(nv);
+                // inlined lfcparent.appendChild(lfcnode)
+                lfcnode.parentNode = lfcparent;
+                lfcnode.ownerDocument = document;
+                lfcnode.__LZo = (lfcparent.childNodes.push(lfcnode) - 1);
             }
-            
-            lfcnode.$lzc$set_childNodes(newchildren);
+        } else if (type == 1 || type == 9) {
+            // element or document node (1: ELEMENT_NODE, 9: DOCUMENT_NODE)
+
+            // this is inlined:
+            // var lfcnode = this.copyBrowserNode(node);
+            // lfcparent.appendChild(lfcnode);
+
+            // older IE's use "baseName" instead of DOM Level2 property "localName"
+            var nname = (!nsprefix && (node.localName || node.baseName)) || node.nodeName;
+
+            var cattrs = {};
+            var nattrs = node.attributes;
+            if (nattrs) {
+                // slow but sure way to copy attributes
+                for (var k = 0, len = nattrs.length; k < len; k++) {
+                    var attrnode = nattrs[k];
+                    if (attrnode) {
+                        // older IE's use "baseName" instead of DOM Level2 property "localName"
+                        var attrname = (!nsprefix && (attrnode.localName || attrnode.baseName)) || attrnode.name;
+                        cattrs[attrname] = attrnode.value;
+                    }
+                }
+            }
+
+            var lfcnode = new LzDataElement(nname);
+            // avoid copy of cattrs (see LzDataElement ctor)
+            lfcnode.attributes = cattrs;
+            // inlined lfcparent.appendChild(lfcnode)
+            lfcnode.parentNode = lfcparent;
+            lfcnode.ownerDocument = document;
+            lfcnode.__LZo = (lfcparent.childNodes.push(lfcnode) - 1);
+
+            // traverse down first
+            if ((next = node.firstChild)) {
+                // this node is the new context
+                lfcparent = lfcnode;
+                node = next;
+                continue;
+            }
+        } else {
+            // ignore other node types
         }
-        return lfcnode;
-    } else {
-        // ignore all other node types
-        return null;
+        // select next node
+        while (! (next = node.nextSibling)) {
+            // no nextSibling, go back in DOM
+            node = node.parentNode;
+            lfcparent = lfcparent.parentNode;
+            if (node === xmlnode) {
+                // reached top element, copy finished
+                return document.childNodes[0];
+            }
+        }
+        node = next;
     }
 }
 



More information about the Laszlo-checkins mailing list