From 4466770992d5833358169d0248c4deedadea1a96 Mon Sep 17 00:00:00 2001 From: Michał Gołębiowski-Owczarek Date: Mon, 16 Dec 2024 19:00:50 +0100 Subject: 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 gh-5568 --- test/unit/selector.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/unit/selector.js b/test/unit/selector.js index 1c3358254..5fecfae94 100644 --- a/test/unit/selector.js +++ b/test/unit/selector.js @@ -2237,6 +2237,37 @@ QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "custom pseudos", function( as } } ); +QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "custom attribute getters", function( assert ) { + assert.expect( 2 ); + + var original = jQuery.attrHooks.hreflang, + selector = "a:contains('mozilla')[hreflang='https://mozilla.org/en']"; + + try { + jQuery.attrHooks.hreflang = { + get: function( elem, name ) { + var href = elem.getAttribute( "href" ), + lang = elem.getAttribute( name ); + return lang && ( href + lang ); + } + }; + + assert.deepEqual( + jQuery.find( selector, createWithFriesXML() ), + [], + "Custom attrHooks (preferred document)" + ); + assert.t( "Custom attrHooks (preferred document)", selector, [ "mozilla" ] ); + } finally { + jQuery.attrHooks.hreflang = original; + } +} ); +QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "Ensure no 'undefined' handler is added", function( assert ) { + assert.expect( 1 ); + assert.ok( !jQuery.attrHooks.hasOwnProperty( "undefined" ), + "Extra attr handlers are not added to jQuery.attrHooks (https://github.com/jquery/sizzle/issues/353)" ); +} ); + QUnit.test( "jQuery.find.matchesSelector", function( assert ) { assert.expect( 15 ); -- cgit v1.2.3