changeset 4344:5c88b27f31e5

speed up javascript comments processing on IE by getElementsByClassName()
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 27 Sep 2008 23:04:40 +0200
parents c1a7c986afa5
children 5c29df15cd90
files wiki/htdocs/common/js/common.js
diffstat 1 files changed, 97 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/wiki/htdocs/common/js/common.js	Sat Sep 27 16:54:07 2008 +0200
+++ b/wiki/htdocs/common/js/common.js	Sat Sep 27 23:04:40 2008 +0200
@@ -174,37 +174,30 @@
     }
 }
 
+// for long documents with many comments this is expensive to calculate,
+// thus we keep it here:
+comments = null;
+
 function toggleComments() {
     // Toggle visibility of every tag with class == *comment*
-    var all = document.getElementsByTagName('*');
-    for (i = 0; i < all.length; i++){
-        el = all[i];
-        if ( el.className.indexOf('comment') >= 0 ){
-            if ( el.style.display != 'none' ) {
-                el.style.display = 'none';
-            } else {
-                el.style.display = '';
-            }
+    for (i = 0; i < comments.length; i++){
+        el = comments[i];
+        if ( el.style.display != 'none' ) {
+            el.style.display = 'none';
+        } else {
+            el.style.display = '';
         }
     }
 }
 
 function show_toggleComments() {
     // Show edit bar item "ToggleComments" if inline comments exist on this page
-    var all = document.getElementsByTagName('*');
-    var count = 0;
-    for (i = 0; i < all.length; i++){
-        el = all[i];
-        if ( el.className.indexOf('comment') >= 0 ){
-            count++;
-        }
-    }
-    if (count > 0) {
-        for (i = 0; i < all.length; i++){
-            el = all[i];
-            if ( el.className == 'toggleCommentsButton' ){
-                el.style.display = 'inline';
-            }
+    comments = getElementsByClassName('comment', null, document);
+    if (comments.length > 0) {
+        var buttons = getElementsByClassName('toggleCommentsButton', null, document);
+        for (i = 0; i < buttons.length; i++){
+            el = buttons[i];
+            el.style.display = 'inline';
         }
     }
 }
@@ -314,3 +307,84 @@
         }
     }
 }
+
+/*  getElementsByClassName
+    Developed by Robert Nyman, http://www.robertnyman.com
+    Code/licensing: http://code.google.com/p/getelementsbyclassname/ (MIT license)
+    Version: 1.0.1
+*/  
+var getElementsByClassName = function (className, tag, elm){
+    if (document.getElementsByClassName) {
+        getElementsByClassName = function (className, tag, elm) {
+            elm = elm || document;
+            var elements = elm.getElementsByClassName(className),
+                nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
+                returnElements = [],
+                current;
+            for(var i=0, il=elements.length; i<il; i+=1){
+                current = elements[i];
+                if(!nodeName || nodeName.test(current.nodeName)) {
+                    returnElements.push(current);
+                }
+            }
+            return returnElements;
+        };
+    }
+    else if (document.evaluate) {
+        getElementsByClassName = function (className, tag, elm) {
+            tag = tag || "*";
+            elm = elm || document;
+            var classes = className.split(" "),
+                classesToCheck = "",
+                xhtmlNamespace = "http://www.w3.org/1999/xhtml",
+                namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
+                returnElements = [],
+                elements,
+                node;
+            for(var j=0, jl=classes.length; j<jl; j+=1){
+                classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
+            }
+            try {
+                elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
+            }
+            catch (e) {
+                elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
+            }
+            while ((node = elements.iterateNext())) {
+                returnElements.push(node);
+            }
+            return returnElements;
+        };
+    }
+    else {
+        getElementsByClassName = function (className, tag, elm) {
+            tag = tag || "*";
+            elm = elm || document;
+            var classes = className.split(" "),
+                classesToCheck = [],
+                elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
+                current,
+                returnElements = [],
+                match;
+            for(var k=0, kl=classes.length; k<kl; k+=1){
+                classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
+            }
+            for(var l=0, ll=elements.length; l<ll; l+=1){
+                current = elements[l];
+                match = false;
+                for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
+                    match = classesToCheck[m].test(current.className);
+                    if (!match) {
+                        break;
+                    }
+                }
+                if (match) {
+                    returnElements.push(current);
+                }
+            }
+            return returnElements;
+        };
+    }
+    return getElementsByClassName(className, tag, elm);
+};
+