Skip to content

Commit 4466770

Browse files
authored
Tests: Add custom attribute getter tests to the selector module
Sizzle & the `3.x-stable` branch have tests adding a custom attribute getter to `attrHandle` and checking if selection takes it into account. `attrHandle` was removed from the `4.x` line so the tests were not ported to the `main` branch, but the `4.x` line takes standard jQuery attribute getters into account instead and we should test for that. Backport the `3.x-stable` selector tests for custom attribute getters, changing `jQuery.expr.attrHandle` to `jQuery.attrHooks`. Closes jquerygh-5568
1 parent 0e12350 commit 4466770

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

test/unit/selector.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,37 @@ QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "custom pseudos", function( as
22372237
}
22382238
} );
22392239

2240+
QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "custom attribute getters", function( assert ) {
2241+
assert.expect( 2 );
2242+
2243+
var original = jQuery.attrHooks.hreflang,
2244+
selector = "a:contains('mozilla')[hreflang='https://mozilla.org/en']";
2245+
2246+
try {
2247+
jQuery.attrHooks.hreflang = {
2248+
get: function( elem, name ) {
2249+
var href = elem.getAttribute( "href" ),
2250+
lang = elem.getAttribute( name );
2251+
return lang && ( href + lang );
2252+
}
2253+
};
2254+
2255+
assert.deepEqual(
2256+
jQuery.find( selector, createWithFriesXML() ),
2257+
[],
2258+
"Custom attrHooks (preferred document)"
2259+
);
2260+
assert.t( "Custom attrHooks (preferred document)", selector, [ "mozilla" ] );
2261+
} finally {
2262+
jQuery.attrHooks.hreflang = original;
2263+
}
2264+
} );
2265+
QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "Ensure no 'undefined' handler is added", function( assert ) {
2266+
assert.expect( 1 );
2267+
assert.ok( !jQuery.attrHooks.hasOwnProperty( "undefined" ),
2268+
"Extra attr handlers are not added to jQuery.attrHooks (https://github.com/jquery/sizzle/issues/353)" );
2269+
} );
2270+
22402271
QUnit.test( "jQuery.find.matchesSelector", function( assert ) {
22412272
assert.expect( 15 );
22422273

0 commit comments

Comments
 (0)