#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:
@@ -199,12 +199,26 @@ OpenLayers.Map.prototype = {
|
|||||||
|
|
||||||
// only append link stylesheet if the theme property is set
|
// only append link stylesheet if the theme property is set
|
||||||
if(this.theme) {
|
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');
|
var cssNode = document.createElement('link');
|
||||||
cssNode.setAttribute('rel', 'stylesheet');
|
cssNode.setAttribute('rel', 'stylesheet');
|
||||||
cssNode.setAttribute('type', 'text/css');
|
cssNode.setAttribute('type', 'text/css');
|
||||||
cssNode.setAttribute('href', this.theme);
|
cssNode.setAttribute('href', this.theme);
|
||||||
document.getElementsByTagName('head')[0].appendChild(cssNode);
|
document.getElementsByTagName('head')[0].appendChild(cssNode);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.layers = [];
|
this.layers = [];
|
||||||
|
|
||||||
|
|||||||
@@ -336,34 +336,48 @@
|
|||||||
function test_01_Map_defaultTheme(t) {
|
function test_01_Map_defaultTheme(t) {
|
||||||
t.plan(5);
|
t.plan(5);
|
||||||
|
|
||||||
var head = document.getElementsByTagName('head')[0];
|
var links = document.getElementsByTagName('link');
|
||||||
var nodeCount = head.childNodes.length;
|
|
||||||
|
|
||||||
map = new OpenLayers.Map('map');
|
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" );
|
// reconstruct the map to prove that another link is not added
|
||||||
t.eq(lastNode.tagName, "LINK", "node added is a link element");
|
map = new OpenLayers.Map('map');
|
||||||
t.eq(lastNode.rel, "stylesheet", "node added has rel set to stylesheet");
|
t.eq(links.length, document.getElementsByTagName('link').length,
|
||||||
t.eq(lastNode.type, "text/css", "node added has type set to text/css");
|
"calling the map constructor twice with the same theme doesn't add duplicate link nodes");
|
||||||
t.ok(OpenLayers.Util.isEquivalentUrl(map.theme, lastNode.href), "node added has href equivalent to map.theme");
|
|
||||||
}
|
}
|
||||||
function test_01_Map_customTheme(t) {
|
function test_01_Map_customTheme(t) {
|
||||||
t.plan(5);
|
t.plan(5);
|
||||||
|
|
||||||
var head = document.getElementsByTagName('head')[0];
|
var customTheme = 'foo';
|
||||||
var nodeCount = head.childNodes.length;
|
var options = {theme: customTheme};
|
||||||
|
|
||||||
var options = {theme: 'foo'};
|
|
||||||
map = new OpenLayers.Map('map', options);
|
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(map.theme, customTheme, "map theme is properly set");
|
||||||
t.eq(lastNode.tagName, "LINK", "node added is a link element");
|
t.eq(gotNodes, 1, "with custom theme, a single link node is added to document");
|
||||||
t.eq(lastNode.rel, "stylesheet", "node added has rel set to stylesheet");
|
t.ok(themeNode != null, "a link node with the theme href was added");
|
||||||
t.eq(lastNode.type, "text/css", "node added has type set to text/css");
|
t.eq(themeNode.rel, "stylesheet", "node added has rel set to stylesheet");
|
||||||
t.ok(OpenLayers.Util.isEquivalentUrl(map.theme, lastNode.href), "node added has href equivalent to map.theme");
|
t.eq(themeNode.type, "text/css", "node added has type set to text/css");
|
||||||
}
|
}
|
||||||
function test_01_Map_noTheme(t) {
|
function test_01_Map_noTheme(t) {
|
||||||
t.plan(1);
|
t.plan(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user