File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -474,7 +474,7 @@ export default class SelectorParser {
474474 switch ( attribute . operator ) {
475475 // [attribute~="value"] - Contains a specified word.
476476 case '~' :
477- return new RegExp ( `[- ] ${ escapedValue } | ${ escapedValue } [- ]|^ ${ escapedValue } $ ` , modifier ) ;
477+ return new RegExp ( `(^|\\s) ${ escapedValue } (\\s|$) ` , modifier ) ;
478478 // [attribute|="value"] - Starts with the specified word.
479479 case '|' :
480480 return new RegExp ( `^${ escapedValue } [- ]|^${ escapedValue } $` , modifier ) ;
Original file line number Diff line number Diff line change @@ -706,6 +706,22 @@ describe('QuerySelector', () => {
706706 expect ( elements [ 1 ] === container . children [ 0 ] . children [ 1 ] . children [ 1 ] ) . toBe ( true ) ;
707707 } ) ;
708708
709+ it ( 'Does not match hyphenated substrings with "~=" selector.' , ( ) => {
710+ const container = document . createElement ( 'div' ) ;
711+ container . innerHTML =
712+ '<div data-controller="modal"><div data-controller="modal-auto-close"></div></div>' ;
713+ const parent = container . children [ 0 ] ;
714+ const child = container . children [ 0 ] . children [ 0 ] ;
715+
716+ expect ( parent . matches ( '[data-controller~="modal"]' ) ) . toBe ( true ) ;
717+ expect ( child . matches ( '[data-controller~="modal"]' ) ) . toBe ( false ) ;
718+ expect ( child . matches ( '[data-controller~="modal-auto"]' ) ) . toBe ( false ) ;
719+ expect ( child . matches ( '[data-controller~="auto"]' ) ) . toBe ( false ) ;
720+ expect ( child . matches ( '[data-controller~="odal"]' ) ) . toBe ( false ) ;
721+ expect ( child . matches ( '[data-controller~="modal-auto-close"]' ) ) . toBe ( true ) ;
722+ expect ( container . querySelector ( '[data-controller~="modal"]' ) ) . toBe ( parent ) ;
723+ } ) ;
724+
709725 it ( 'Returns all elements with an attribute value starting with the specified word using "[class|="class1"]".' , ( ) => {
710726 const container = document . createElement ( 'div' ) ;
711727 container . innerHTML = QuerySelectorHTML ;
You can’t perform that action at this time.
0 commit comments