Skip to content

Commit e19e70f

Browse files
committed
fix: extra protection around tagName accessor
1 parent 2d5844c commit e19e70f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/getTag.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
*/
77
export function getTag( el, filter )
88
{
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+
920
const tagName = el.tagName.toLowerCase().replace(/:/g, '\\:')
1021

1122
if (filter && !filter('tag', 'tag', tagName)) {

test/unique-selector.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ describe( 'Unique Selector Tests', () =>
112112
expect( uniqueSelector ).to.equal( 'span' );
113113
} );
114114

115-
116115
it( 'Tag', () =>
117116
{
118117
$( 'body' ).append( '<div class="test5"><span></span></div><div class="test5"><span></span></div>' );
@@ -129,6 +128,35 @@ describe( 'Unique Selector Tests', () =>
129128
expect( uniqueSelector ).to.equal( 'a' );
130129
} );
131130

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+
132160
it( 'Attributes', () =>
133161
{
134162
$( 'body' ).append( '<div class="test5" test="5"></div>' );

0 commit comments

Comments
 (0)