Skip to content

Commit 735e31b

Browse files
committed
Escape css selectors properly
1 parent 75bbf3d commit 735e31b

File tree

1 file changed

+13
-10
lines changed
  • java/client/src/org/openqa/selenium

1 file changed

+13
-10
lines changed

java/client/src/org/openqa/selenium/By.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Objects;
27-
import java.util.stream.Stream;
28-
29-
import static java.util.stream.Collectors.joining;
3027

3128
/**
3229
* Mechanism used to locate elements within a document. In order to create your own locating
@@ -185,7 +182,7 @@ public ById(String id) {
185182
super(
186183
"id",
187184
Require.argument("Id", id).nonNull("Cannot find elements when id is null."),
188-
new ByCssSelector(Stream.of(id.split("\\s+")).map(str -> "#" + str).collect(joining(" "))));
185+
"#%s");
189186

190187
this.id = id;
191188
}
@@ -241,7 +238,7 @@ public ByName(String name) {
241238
super(
242239
"name",
243240
Require.argument("Name", name).nonNull("Cannot find elements when name text is null."),
244-
new ByCssSelector(String.format("*[name='%s']", name.replace("'", "\\'"))));
241+
String.format("*[name='%s']", name.replace("'", "\\'")));
245242

246243
this.name = name;
247244
}
@@ -299,7 +296,7 @@ public ByClassName(String className) {
299296
"class",
300297
Require.argument("Class name", className)
301298
.nonNull("Cannot find elements when the class name expression is null."),
302-
new ByCssSelector(Stream.of(className.split("\\s+")).map(str -> "." + str).collect(joining(" "))));
299+
".%s");
303300

304301
if (className.matches(".*\\s.*")) {
305302
throw new InvalidSelectorException("Compound class names not permitted");
@@ -382,7 +379,6 @@ private Map<String, Object> toJson() {
382379
}
383380

384381
private static abstract class BaseW3CLocator extends By implements Remotable {
385-
386382
private final Parameters params;
387383

388384
protected BaseW3CLocator(String using, String value) {
@@ -412,13 +408,12 @@ protected final Map<String, Object> toJson() {
412408
}
413409

414410
private static abstract class PreW3CLocator extends By implements Remotable {
415-
416411
private final Parameters remoteParams;
417412
private final ByCssSelector fallback;
418413

419-
private PreW3CLocator(String using, String value, ByCssSelector fallback) {
414+
private PreW3CLocator(String using, String value, String formatString) {
420415
this.remoteParams = new Remotable.Parameters(using, value);
421-
this.fallback = fallback;
416+
this.fallback = new ByCssSelector(String.format(formatString, cssEscape(value)));
422417
}
423418

424419
@Override
@@ -439,5 +434,13 @@ final public Parameters getRemoteParameters() {
439434
final protected Map<String, Object> toJson() {
440435
return fallback.toJson();
441436
}
437+
438+
private String cssEscape(String using) {
439+
using = using.replaceAll("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])", "\\\\$1");
440+
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
441+
using = "\\" + (30 + Integer.parseInt(using.substring(0,1))) + " " + using.substring(1);
442+
}
443+
return using;
444+
}
442445
}
443446
}

0 commit comments

Comments
 (0)