#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

git-svn-id: http://svn.openlayers.org/trunk/openlayers@3153 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2007-05-17 16:35:26 +00:00
parent 7871d26df4
commit a80d7f2826
2 changed files with 54 additions and 26 deletions

View File

@@ -199,12 +199,26 @@ OpenLayers.Map.prototype = {
// only append link stylesheet if the theme property is set
if(this.theme) {
// 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 = [];

View File

@@ -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 lastNode = head.childNodes[head.childNodes.length - 1];
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");
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");
// reconstruct the map to prove that another link is not added
map = new OpenLayers.Map('map');
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];
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(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");
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);