File tree 2 files changed +40
-1
lines changed
2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 6
6
*/
7
7
export function getTag ( el , filter )
8
8
{
9
+ if ( typeof el . tagName !== 'string' ) {
10
+ // If the tagName attribute has been overridden, we should
11
+ // return null and not use tagName for selector generation.
12
+ //
13
+ // This can happen when a <form> element contains an <input>
14
+ // with an id of `tagName`. In this case, the form element's
15
+ // tagName property is a reference to the input element, not
16
+ // a string.
17
+ return null ;
18
+ }
19
+
9
20
const tagName = el . tagName . toLowerCase ( ) . replace ( / : / g, '\\:' )
10
21
11
22
if ( filter && ! filter ( 'tag' , 'tag' , tagName ) ) {
Original file line number Diff line number Diff line change @@ -112,7 +112,6 @@ describe( 'Unique Selector Tests', () =>
112
112
expect ( uniqueSelector ) . to . equal ( 'span' ) ;
113
113
} ) ;
114
114
115
-
116
115
it ( 'Tag' , ( ) =>
117
116
{
118
117
$ ( 'body' ) . append ( '<div class="test5"><span></span></div><div class="test5"><span></span></div>' ) ;
@@ -129,6 +128,35 @@ describe( 'Unique Selector Tests', () =>
129
128
expect ( uniqueSelector ) . to . equal ( 'a' ) ;
130
129
} ) ;
131
130
131
+ it ( 'Tag - filtered due to property override' , ( ) =>
132
+ {
133
+ $ ( 'body' ) . append ( `
134
+ <div class="test2">
135
+ <form action="" method="get">
136
+ <div class="form-example">
137
+ <label for="name">Enter your name: </label>
138
+ <input type="text" name="name" id="tagName" required />
139
+ </div>
140
+ </form>
141
+ </div>
142
+ ` ) ;
143
+
144
+ const formNode = $ ( 'form' ) . get ( 0 ) ;
145
+
146
+ // JSDOM doesn't actually exhibit this behavior;
147
+ // forcing the test to behave as a browser does.
148
+ Object . defineProperty ( formNode , 'tagName' , {
149
+ get : ( ) => {
150
+ return $ ( 'input#tagName' ) . get ( 0 ) ;
151
+ }
152
+ } )
153
+
154
+ expect ( typeof formNode . tagName ) . to . not . equal ( 'string' )
155
+
156
+ const uniqueSelector = unique ( formNode ) ;
157
+ expect ( uniqueSelector ) . to . equal ( '.test2 > :nth-child(1)' ) ;
158
+ } ) ;
159
+
132
160
it ( 'Attributes' , ( ) =>
133
161
{
134
162
$ ( 'body' ) . append ( '<div class="test5" test="5"></div>' ) ;
You can’t perform that action at this time.
0 commit comments