From faaa2cec1fc1edfe1a23feae4e871b3876bb2035 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 18 Apr 2012 09:46:24 -0400 Subject: [PATCH] Using compiled regexp to replace dots. Though split and join appear to be more efficient in Chrome, a compiled regexp looks to be a safer bet across the board. See http://jsperf.com/dotless (and http://jsperf.com/dotless-nop for the NOP). --- lib/OpenLayers/Util.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/OpenLayers/Util.js b/lib/OpenLayers/Util.js index cfa5ce2517..4c44dbe599 100644 --- a/lib/OpenLayers/Util.js +++ b/lib/OpenLayers/Util.js @@ -130,6 +130,17 @@ OpenLayers.Util.indexOf = function(array, obj) { }; +/** + * Property: dotless + * {RegExp} + * Compiled regular expression to match dots ("."). This is used for replacing + * dots in identifiers. Because object identifiers are frequently used for + * DOM element identifiers by the library, we avoid using dots to make for + * more sensible CSS selectors. + * + * TODO: Use a module pattern to avoid bloating the API with stuff like this. + */ +OpenLayers.Util.dotless = /\./g; /** * Function: modifyDOMElement @@ -158,7 +169,7 @@ OpenLayers.Util.modifyDOMElement = function(element, id, px, sz, position, border, overflow, opacity) { if (id) { - element.id = id.replace(/\./g, "_"); + element.id = id.replace(OpenLayers.Util.dotless, "_"); } if (px) { element.style.left = px.x + "px"; @@ -939,7 +950,7 @@ OpenLayers.Util.createUniqueID = function(prefix) { if (prefix == null) { prefix = "id_"; } else { - prefix = prefix.replace(/\./g, "_"); + prefix = prefix.replace(OpenLayers.Util.dotless, "_"); } OpenLayers.Util.lastSeqID += 1; return prefix + OpenLayers.Util.lastSeqID;