Skip to content

Commit 24a5055

Browse files
committed
Escape spaces in ID locators.
Spaces aren't legal characters in IDs, but the singular locator (bot.locators.id.single) accepts them, as does document.getElementById in every browser, so the plural locator should work for the sake of consistency.
1 parent cedaa07 commit 24a5055

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

javascript/atoms/locators/id.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ bot.locators.id.many = function(target, root) {
7979
}
8080
if (bot.locators.id.canUseQuerySelector_(root, target)) {
8181
try {
82-
// ID can contain anything but spaces. Need to escape for CSS selector.
83-
// http://www.w3.org/TR/html5/dom.html#the-id-attribute
82+
// Need to escape the ID for use in a CSS selector.
8483
return root.querySelectorAll('#' + bot.locators.id.cssEscape_(target));
8584
} catch (e) {
8685
return [];
@@ -97,6 +96,10 @@ bot.locators.id.many = function(target, root) {
9796
* Given a string, escapes all the characters that have special meaning in CSS.
9897
* https://mathiasbynens.be/notes/css-escapes
9998
*
99+
* An ID can contain anything but spaces, but we also escape spaces because some
100+
* webpages use spaces, and getElementById allows spaces in every browser.
101+
* http://www.w3.org/TR/html5/dom.html#the-id-attribute
102+
*
100103
* This could be further improved, perhaps by using
101104
* http://dev.w3.org/csswg/cssom/#the-css.escape()-method , where implemented,
102105
* or a polyfill such as https://github.com/mathiasbynens/CSS.escape.
@@ -107,5 +110,5 @@ bot.locators.id.many = function(target, root) {
107110
*/
108111
bot.locators.id.cssEscape_ = function(s) {
109112
// One backslash escapes things in a regex statement; we need two in a string.
110-
return s.replace(/(['"\\#.:;,!?+<>=~*^$|%&@`{}\-\/\[\]\(\)])/g, '\\$1');
113+
return s.replace(/([ '"\\#.:;,!?+<>=~*^$|%&@`{}\-\/\[\]\(\)])/g, '\\$1');
111114
};

javascript/atoms/test/locator_test.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,16 @@
229229
var els = bot.locators.findElements({id: id});
230230
assertEquals("Expected exactly one element with ID " + id, 1, els.length);
231231
assertEquals(expectedName, els[0].getAttribute('name'));
232+
233+
var el = bot.locators.findElement({id: id});
234+
assertNotNullNorUndefined("Expected element with ID " + id, el);
235+
assertEquals(expectedName, el.getAttribute('name'));
232236
}
233237

234238
findById("'", 'apóstrofo');
235239
findById('"', 'comilla');
236240
findById('\\', 'barra_invertida');
241+
findById('s p a c e', 'space');
237242
findById('#.:;,!?+<>=~*^$|%&@`{}-/[]()', 'remaining_css_special_symbols');
238243
}
239244

@@ -396,6 +401,7 @@
396401
<div name="apóstrofo" id="'">apóstrofo</div>
397402
<div name="comilla" id='"'>comilla</div>
398403
<div name="barra_invertida" id="\">barra invertida</div>
404+
<div name="space" id="s p a c e">space</div>
399405
<div name="remaining_css_special_symbols" id="#.:;,!?+<>=~*^$|%&@`{}-/[]()">queso!</div>
400406

401407
<a id="link" href="#">this is a link</a>

0 commit comments

Comments
 (0)