@@ -32,6 +32,36 @@ function convertElementToCssSelector(element: Element) {
32
32
return getUniqueCssSelector ( element )
33
33
}
34
34
35
+ function escapeIdForCSSSelector ( id : string ) {
36
+ return id
37
+ . split ( '' )
38
+ . map ( ( char ) => {
39
+ const code = char . charCodeAt ( 0 )
40
+
41
+ if ( char === ' ' || char === '#' || char === '.' || char === ':' || char === '[' || char === ']' || char === '>' || char === '+' || char === '~' || char === '\\' ) {
42
+ // Escape common special characters with backslashes
43
+ return `\\${ char } `
44
+ }
45
+ else if ( code >= 0x10000 ) {
46
+ // Unicode escape for characters outside the BMP
47
+ return `\\${ code . toString ( 16 ) . toUpperCase ( ) . padStart ( 6 , '0' ) } `
48
+ }
49
+ else if ( code < 0x20 || code === 0x7F ) {
50
+ // Non-printable ASCII characters (0x00-0x1F and 0x7F) are escaped
51
+ return `\\${ code . toString ( 16 ) . toUpperCase ( ) . padStart ( 2 , '0' ) } `
52
+ }
53
+ else if ( code >= 0x80 ) {
54
+ // Non-ASCII characters (0x80 and above) are escaped
55
+ return `\\${ code . toString ( 16 ) . toUpperCase ( ) . padStart ( 2 , '0' ) } `
56
+ }
57
+ else {
58
+ // Allowable characters are used directly
59
+ return char
60
+ }
61
+ } )
62
+ . join ( '' )
63
+ }
64
+
35
65
function getUniqueCssSelector ( el : Element ) {
36
66
const path = [ ]
37
67
let parent : null | ParentNode
@@ -44,10 +74,10 @@ function getUniqueCssSelector(el: Element) {
44
74
45
75
const tag = el . tagName
46
76
if ( el . id ) {
47
- path . push ( `#${ el . id } ` )
77
+ path . push ( `#${ escapeIdForCSSSelector ( el . id ) } ` )
48
78
}
49
79
else if ( ! el . nextElementSibling && ! el . previousElementSibling ) {
50
- path . push ( tag )
80
+ path . push ( tag . toLowerCase ( ) )
51
81
}
52
82
else {
53
83
let index = 0
@@ -65,15 +95,15 @@ function getUniqueCssSelector(el: Element) {
65
95
}
66
96
67
97
if ( sameTagSiblings > 1 ) {
68
- path . push ( `${ tag } :nth-child(${ elementIndex } )` )
98
+ path . push ( `${ tag . toLowerCase ( ) } :nth-child(${ elementIndex } )` )
69
99
}
70
100
else {
71
- path . push ( tag )
101
+ path . push ( tag . toLowerCase ( ) )
72
102
}
73
103
}
74
104
el = parent as Element
75
105
} ;
76
- return `${ provider === 'webdriverio' && hasShadowRoot ? '>>>' : '' } ${ path . reverse ( ) . join ( ' > ' ) } ` . toLowerCase ( )
106
+ return `${ provider === 'webdriverio' && hasShadowRoot ? '>>>' : '' } ${ path . reverse ( ) . join ( ' > ' ) } `
77
107
}
78
108
79
109
function getParent ( el : Element ) {
0 commit comments