14
14
* limitations under the License.
15
15
*/
16
16
17
- import { createAttributeEngine } from './attributeSelectorEngine' ;
18
17
import { SelectorEngine , SelectorRoot } from './selectorEngine' ;
19
18
import { createTextSelector } from './textSelectorEngine' ;
20
19
import { XPathEngine } from './xpathSelectorEngine' ;
21
20
import { ParsedSelector , ParsedSelectorPart , parseSelector } from '../common/selectorParser' ;
22
21
import { FatalDOMError } from '../common/domErrors' ;
23
22
import { SelectorEvaluatorImpl , isVisible , parentElementOrShadowHost } from './selectorEvaluator' ;
24
- import { createCSSEngine } from './cssSelectorEngine ' ;
23
+ import { CSSComplexSelectorList } from '../common/cssParser ' ;
25
24
26
25
type Predicate < T > = ( progress : InjectedScriptProgress , continuePolling : symbol ) => T | symbol ;
27
26
@@ -46,20 +45,18 @@ export class InjectedScript {
46
45
47
46
constructor ( customEngines : { name : string , engine : SelectorEngine } [ ] ) {
48
47
this . _enginesV1 = new Map ( ) ;
49
- this . _enginesV1 . set ( 'css' , createCSSEngine ( true ) ) ;
50
- this . _enginesV1 . set ( 'css:light' , createCSSEngine ( false ) ) ;
51
48
this . _enginesV1 . set ( 'xpath' , XPathEngine ) ;
52
49
this . _enginesV1 . set ( 'xpath:light' , XPathEngine ) ;
53
50
this . _enginesV1 . set ( 'text' , createTextSelector ( true ) ) ;
54
51
this . _enginesV1 . set ( 'text:light' , createTextSelector ( false ) ) ;
55
- this . _enginesV1 . set ( 'id' , createAttributeEngine ( 'id' , true ) ) ;
56
- this . _enginesV1 . set ( 'id:light' , createAttributeEngine ( 'id' , false ) ) ;
57
- this . _enginesV1 . set ( 'data-testid' , createAttributeEngine ( 'data-testid' , true ) ) ;
58
- this . _enginesV1 . set ( 'data-testid:light' , createAttributeEngine ( 'data-testid' , false ) ) ;
59
- this . _enginesV1 . set ( 'data-test-id' , createAttributeEngine ( 'data-test-id' , true ) ) ;
60
- this . _enginesV1 . set ( 'data-test-id:light' , createAttributeEngine ( 'data-test-id' , false ) ) ;
61
- this . _enginesV1 . set ( 'data-test' , createAttributeEngine ( 'data-test' , true ) ) ;
62
- this . _enginesV1 . set ( 'data-test:light' , createAttributeEngine ( 'data-test' , false ) ) ;
52
+ this . _enginesV1 . set ( 'id' , this . _createAttributeEngine ( 'id' , true ) ) ;
53
+ this . _enginesV1 . set ( 'id:light' , this . _createAttributeEngine ( 'id' , false ) ) ;
54
+ this . _enginesV1 . set ( 'data-testid' , this . _createAttributeEngine ( 'data-testid' , true ) ) ;
55
+ this . _enginesV1 . set ( 'data-testid:light' , this . _createAttributeEngine ( 'data-testid' , false ) ) ;
56
+ this . _enginesV1 . set ( 'data-test-id' , this . _createAttributeEngine ( 'data-test-id' , true ) ) ;
57
+ this . _enginesV1 . set ( 'data-test-id:light' , this . _createAttributeEngine ( 'data-test-id' , false ) ) ;
58
+ this . _enginesV1 . set ( 'data-test' , this . _createAttributeEngine ( 'data-test' , true ) ) ;
59
+ this . _enginesV1 . set ( 'data-test:light' , this . _createAttributeEngine ( 'data-test' , false ) ) ;
63
60
for ( const { name, engine } of customEngines )
64
61
this . _enginesV1 . set ( name , engine ) ;
65
62
@@ -133,6 +130,21 @@ export class InjectedScript {
133
130
return this . _enginesV1 . get ( part . name ) ! . queryAll ( root , part . body ) ;
134
131
}
135
132
133
+ private _createAttributeEngine ( attribute : string , shadow : boolean ) : SelectorEngine {
134
+ const toCSS = ( selector : string ) : CSSComplexSelectorList => {
135
+ const css = `[${ attribute } =${ JSON . stringify ( selector ) } ]` ;
136
+ return [ { simples : [ { selector : { css, functions : [ ] } , combinator : '' } ] } ] ;
137
+ } ;
138
+ return {
139
+ query : ( root : SelectorRoot , selector : string ) : Element | undefined => {
140
+ return this . _evaluator . evaluate ( { scope : root as Document | Element , pierceShadow : shadow } , toCSS ( selector ) ) [ 0 ] ;
141
+ } ,
142
+ queryAll : ( root : SelectorRoot , selector : string ) : Element [ ] => {
143
+ return this . _evaluator . evaluate ( { scope : root as Document | Element , pierceShadow : shadow } , toCSS ( selector ) ) ;
144
+ }
145
+ } ;
146
+ }
147
+
136
148
extend ( source : string , params : any ) : any {
137
149
const constrFunction = global . eval ( source ) ;
138
150
return new constrFunction ( this , params ) ;
0 commit comments