@@ -80,9 +80,24 @@ function createMatcher(selector: string): Matcher {
80
80
return text => text . toLowerCase ( ) . includes ( selector ) ;
81
81
}
82
82
83
+ // Skips <head>, <script> and <style> elements and all their children.
84
+ const nodeFilter : NodeFilter = {
85
+ acceptNode : node => {
86
+ return node . nodeName === 'HEAD' || node . nodeName === 'SCRIPT' || node . nodeName === 'STYLE' ?
87
+ NodeFilter . FILTER_REJECT : NodeFilter . FILTER_ACCEPT ;
88
+ }
89
+ } ;
90
+
91
+ // If we are querying inside a filtered element, nodeFilter is never called, so we need a separate check.
92
+ function isFilteredNode ( root : SelectorRoot , document : Document ) {
93
+ return root . nodeName === 'SCRIPT' || root . nodeName === 'STYLE' || document . head && document . head . contains ( root ) ;
94
+ }
95
+
83
96
function queryInternal ( root : SelectorRoot , matcher : Matcher , shadow : boolean ) : Element | undefined {
84
97
const document = root instanceof Document ? root : root . ownerDocument ! ;
85
- const walker = document . createTreeWalker ( root , NodeFilter . SHOW_TEXT | NodeFilter . SHOW_ELEMENT ) ;
98
+ if ( isFilteredNode ( root , document ) )
99
+ return ;
100
+ const walker = document . createTreeWalker ( root , NodeFilter . SHOW_TEXT | NodeFilter . SHOW_ELEMENT , nodeFilter ) ;
86
101
const shadowRoots : ShadowRoot [ ] = [ ] ;
87
102
if ( shadow && ( root as Element ) . shadowRoot )
88
103
shadowRoots . push ( ( root as Element ) . shadowRoot ! ) ;
@@ -94,7 +109,7 @@ function queryInternal(root: SelectorRoot, matcher: Matcher, shadow: boolean): E
94
109
95
110
const textParent = ( node && node . nodeType === Node . TEXT_NODE ) ? node . parentElement : null ;
96
111
if ( lastTextParent && textParent !== lastTextParent ) {
97
- if ( lastTextParent . nodeName !== 'SCRIPT' && lastTextParent . nodeName !== 'STYLE' && matcher ( lastText ) )
112
+ if ( matcher ( lastText ) )
98
113
return lastTextParent ;
99
114
lastText = '' ;
100
115
}
@@ -122,7 +137,9 @@ function queryInternal(root: SelectorRoot, matcher: Matcher, shadow: boolean): E
122
137
123
138
function queryAllInternal ( root : SelectorRoot , matcher : Matcher , shadow : boolean , result : Element [ ] ) {
124
139
const document = root instanceof Document ? root : root . ownerDocument ! ;
125
- const walker = document . createTreeWalker ( root , NodeFilter . SHOW_TEXT | NodeFilter . SHOW_ELEMENT ) ;
140
+ if ( isFilteredNode ( root , document ) )
141
+ return ;
142
+ const walker = document . createTreeWalker ( root , NodeFilter . SHOW_TEXT | NodeFilter . SHOW_ELEMENT , nodeFilter ) ;
126
143
const shadowRoots : ShadowRoot [ ] = [ ] ;
127
144
if ( shadow && ( root as Element ) . shadowRoot )
128
145
shadowRoots . push ( ( root as Element ) . shadowRoot ! ) ;
@@ -134,7 +151,7 @@ function queryAllInternal(root: SelectorRoot, matcher: Matcher, shadow: boolean,
134
151
135
152
const textParent = ( node && node . nodeType === Node . TEXT_NODE ) ? node . parentElement : null ;
136
153
if ( lastTextParent && textParent !== lastTextParent ) {
137
- if ( lastTextParent . nodeName !== 'SCRIPT' && lastTextParent . nodeName !== 'STYLE' && matcher ( lastText ) )
154
+ if ( matcher ( lastText ) )
138
155
result . push ( lastTextParent ) ;
139
156
lastText = '' ;
140
157
}
0 commit comments