24
24
import java .util .List ;
25
25
import java .util .Map ;
26
26
import java .util .Objects ;
27
- import java .util .stream .Stream ;
28
-
29
- import static java .util .stream .Collectors .joining ;
30
27
31
28
/**
32
29
* Mechanism used to locate elements within a document. In order to create your own locating
@@ -185,7 +182,7 @@ public ById(String id) {
185
182
super (
186
183
"id" ,
187
184
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" );
189
186
190
187
this .id = id ;
191
188
}
@@ -241,7 +238,7 @@ public ByName(String name) {
241
238
super (
242
239
"name" ,
243
240
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 ("'" , "\\ '" )));
245
242
246
243
this .name = name ;
247
244
}
@@ -299,7 +296,7 @@ public ByClassName(String className) {
299
296
"class" ,
300
297
Require .argument ("Class name" , className )
301
298
.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" );
303
300
304
301
if (className .matches (".*\\ s.*" )) {
305
302
throw new InvalidSelectorException ("Compound class names not permitted" );
@@ -382,7 +379,6 @@ private Map<String, Object> toJson() {
382
379
}
383
380
384
381
private static abstract class BaseW3CLocator extends By implements Remotable {
385
-
386
382
private final Parameters params ;
387
383
388
384
protected BaseW3CLocator (String using , String value ) {
@@ -412,13 +408,12 @@ protected final Map<String, Object> toJson() {
412
408
}
413
409
414
410
private static abstract class PreW3CLocator extends By implements Remotable {
415
-
416
411
private final Parameters remoteParams ;
417
412
private final ByCssSelector fallback ;
418
413
419
- private PreW3CLocator (String using , String value , ByCssSelector fallback ) {
414
+ private PreW3CLocator (String using , String value , String formatString ) {
420
415
this .remoteParams = new Remotable .Parameters (using , value );
421
- this .fallback = fallback ;
416
+ this .fallback = new ByCssSelector ( String . format ( formatString , cssEscape ( value ))) ;
422
417
}
423
418
424
419
@ Override
@@ -439,5 +434,13 @@ final public Parameters getRemoteParameters() {
439
434
final protected Map <String , Object > toJson () {
440
435
return fallback .toJson ();
441
436
}
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
+ }
442
445
}
443
446
}
0 commit comments