[OpenLayers-Commits] r3153 - in trunk/openlayers: lib/OpenLayers tests

commits at openlayers.org commits at openlayers.org
Thu May 17 12:35:27 EDT 2007


Author: tschaub
Date: 2007-05-17 12:35:26 -0400 (Thu, 17 May 2007)
New Revision: 3153

Modified:
   trunk/openlayers/lib/OpenLayers/Map.js
   trunk/openlayers/tests/test_Map.html
Log:
#706 - map constructor should not add duplicate theme related link nodes to the dom - in the case where there are two maps per page, one should be customizable and the other default - this is now fixed for map + overview map or any other multiple map combo

Modified: trunk/openlayers/lib/OpenLayers/Map.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Map.js	2007-05-17 15:16:04 UTC (rev 3152)
+++ trunk/openlayers/lib/OpenLayers/Map.js	2007-05-17 16:35:26 UTC (rev 3153)
@@ -199,11 +199,25 @@
         
         // only append link stylesheet if the theme property is set
         if(this.theme) {
-            var cssNode = document.createElement('link');
-            cssNode.setAttribute('rel', 'stylesheet');
-            cssNode.setAttribute('type', 'text/css');
-            cssNode.setAttribute('href', this.theme);
-            document.getElementsByTagName('head')[0].appendChild(cssNode);
+            // check existing links for equivalent url
+            var addNode = true;
+            var nodes = document.getElementsByTagName('link');
+            for(var i=0; i<nodes.length; ++i) {
+                if(OpenLayers.Util.isEquivalentUrl(nodes.item(i).href,
+                                                   this.theme)) {
+                    addNode = false;
+                    break;
+                }
+            }
+            // only add a new node if one with an equivalent url hasn't already
+            // been added
+            if(addNode) {
+                var cssNode = document.createElement('link');
+                cssNode.setAttribute('rel', 'stylesheet');
+                cssNode.setAttribute('type', 'text/css');
+                cssNode.setAttribute('href', this.theme);
+                document.getElementsByTagName('head')[0].appendChild(cssNode);
+            }
         }
 
         this.layers = [];

Modified: trunk/openlayers/tests/test_Map.html
===================================================================
--- trunk/openlayers/tests/test_Map.html	2007-05-17 15:16:04 UTC (rev 3152)
+++ trunk/openlayers/tests/test_Map.html	2007-05-17 16:35:26 UTC (rev 3153)
@@ -336,34 +336,48 @@
     function test_01_Map_defaultTheme(t) {
         t.plan(5);
         
-        var head = document.getElementsByTagName('head')[0];
-        var nodeCount = head.childNodes.length;
+        var links = document.getElementsByTagName('link');
+        map = new OpenLayers.Map('map');
+        var gotNodes = 0;
+        var themeNode = null;
+        for(var i=0; i<links.length; ++i) {
+            if(OpenLayers.Util.isEquivalentUrl(map.theme, links.item(i).href)) {
+                gotNodes += 1;
+                themeNode = links.item(i);
+            }
+        }
+        t.eq(gotNodes, 1, "by default, a single link node is added to document");
+        t.ok(themeNode != null, "a link node with the theme href was added");
+        t.eq(themeNode.rel, "stylesheet", "node added has rel set to stylesheet");
+        t.eq(themeNode.type, "text/css", "node added has type set to text/css");
         
+        // reconstruct the map to prove that another link is not added
         map = new OpenLayers.Map('map');
-        var lastNode = head.childNodes[head.childNodes.length - 1];
-
-        t.eq(nodeCount + 1, head.childNodes.length, "by default, a node is added to document head" );
-        t.eq(lastNode.tagName, "LINK", "node added is a link element");
-        t.eq(lastNode.rel, "stylesheet", "node added has rel set to stylesheet");
-        t.eq(lastNode.type, "text/css", "node added has type set to text/css");
-        t.ok(OpenLayers.Util.isEquivalentUrl(map.theme, lastNode.href), "node added has href equivalent to map.theme");
+        t.eq(links.length, document.getElementsByTagName('link').length,
+             "calling the map constructor twice with the same theme doesn't add duplicate link nodes");
     }
     function test_01_Map_customTheme(t) {
         t.plan(5);
         
-        var head = document.getElementsByTagName('head')[0];
-        var nodeCount = head.childNodes.length;
-        
-        var options = {theme: 'foo'};
+        var customTheme = 'foo';
+        var options = {theme: customTheme};
         map = new OpenLayers.Map('map', options);
 
-        var lastNode = head.childNodes[head.childNodes.length - 1];
-
-        t.eq(nodeCount + 1, head.childNodes.length, "with custom theme, a node is added to document head" );
-        t.eq(lastNode.tagName, "LINK", "node added is a link element");
-        t.eq(lastNode.rel, "stylesheet", "node added has rel set to stylesheet");
-        t.eq(lastNode.type, "text/css", "node added has type set to text/css");
-        t.ok(OpenLayers.Util.isEquivalentUrl(map.theme, lastNode.href), "node added has href equivalent to map.theme");
+        var links = document.getElementsByTagName('link');
+        var gotNodes = 0;
+        var themeNode = null;
+        for(var i=0; i<links.length; ++i) {
+            if(OpenLayers.Util.isEquivalentUrl(map.theme, links.item(i).href)) {
+                gotNodes += 1;
+                themeNode = links.item(i);
+            }
+        }
+        
+        t.eq(map.theme, customTheme, "map theme is properly set");
+        t.eq(gotNodes, 1, "with custom theme, a single link node is added to document");
+        t.ok(themeNode != null, "a link node with the theme href was added");
+        t.eq(themeNode.rel, "stylesheet", "node added has rel set to stylesheet");
+        t.eq(themeNode.type, "text/css", "node added has type set to text/css");
     }
     function test_01_Map_noTheme(t) {
         t.plan(1);



More information about the Commits mailing list