@@ -4,7 +4,7 @@ import $document from '../document'
4
4
import $jquery from '../jquery'
5
5
import { getTagName } from './elementHelpers'
6
6
import { isWithinShadowRoot , getShadowElementFromPoint } from './shadow'
7
- import { normalizeWhitespaces , escapeQuotes , isSelector } from './utils'
7
+ import { normalizeWhitespaces , escapeQuotes } from './utils'
8
8
9
9
/**
10
10
* Find Parents relative to an initial element
@@ -125,10 +125,16 @@ export const getFirstDeepestElement = ($el: JQuery, index = 0) => {
125
125
const $current = $el . slice ( index , index + 1 )
126
126
const $next = $el . slice ( index + 1 , index + 2 )
127
127
128
- if ( ! $next ) {
128
+ if ( ! $next || $current . length === 0 ) {
129
129
return $current
130
130
}
131
131
132
+ // https://github.com/cypress-io/cypress/issues/14861
133
+ // filter out the <script> and <style> tags
134
+ if ( $current && [ 'SCRIPT' , 'STYLE' ] . includes ( $current . prop ( 'tagName' ) ) ) {
135
+ return getFirstDeepestElement ( $el , index + 1 )
136
+ }
137
+
132
138
// does current contain next?
133
139
if ( $ . contains ( $current . get ( 0 ) , $next . get ( 0 ) ) ) {
134
140
return getFirstDeepestElement ( $el , index + 1 )
@@ -217,20 +223,6 @@ export const getElements = ($el) => {
217
223
return els
218
224
}
219
225
220
- // Remove <style> and <script> elements inside <body>. Even though the contains
221
- // selector avoids selecting them with :not(script,style), it will find the
222
- // text anyway and attribute it to the <body>
223
- // https://github.com/cypress-io/cypress/issues/14861
224
- const removeScriptAndStyleElements = ( elem ) => {
225
- const $elem = $ ( elem )
226
-
227
- if ( ! isSelector ( $elem , 'body' ) ) return elem
228
-
229
- $elem . find ( 'script,style' ) . remove ( )
230
-
231
- return $elem [ 0 ]
232
- }
233
-
234
226
export const getContainsSelector = ( text , filter = '' , options : {
235
227
matchCase ?: boolean
236
228
} = { } ) => {
@@ -252,14 +244,12 @@ export const getContainsSelector = (text, filter = '', options: {
252
244
253
245
// taken from jquery's normal contains method
254
246
cyContainsSelector = function ( elem ) {
255
- elem = removeScriptAndStyleElements ( elem )
256
- let testText = normalizeWhitespaces ( elem )
247
+ const testText = normalizeWhitespaces ( elem )
257
248
258
249
return text . test ( testText )
259
250
}
260
251
} else if ( _ . isString ( text ) ) {
261
252
cyContainsSelector = function ( elem ) {
262
- elem = removeScriptAndStyleElements ( elem )
263
253
let testText = normalizeWhitespaces ( elem )
264
254
265
255
if ( ! options . matchCase ) {
@@ -284,7 +274,7 @@ export const getContainsSelector = (text, filter = '', options: {
284
274
const textToFind = escapedText . includes ( `\'` ) ? `"${ escapedText } "` : `'${ escapedText } '`
285
275
286
276
// use custom cy-contains selector that is registered above
287
- return `${ filter } :not(script,style): cy-contains(${ textToFind } ), ${ filter } [type='submit'][value~=${ textToFind } ]`
277
+ return `${ filter } :cy-contains(${ textToFind } ), ${ filter } [type='submit'][value~=${ textToFind } ]`
288
278
} )
289
279
290
280
return selectors . join ( )
0 commit comments