Attributes: Support the until-found value for the hidden attribute

The `hidden` attribute used to be a boolean one but it gained a new
`until-found` eventually. This led us to change the way we handle boolean
attributes in jQuery 4.0 in gh-5452 to avoid these issues in the future.

That said, currently from the attributes we treat as boolean only `hidden`
has gained an extra value, let's support it.

Closes gh-5607
Ref gh-5388
Ref gh-5452
This commit is contained in:
Michał Gołębiowski-Owczarek
2025-01-16 14:15:37 +01:00
committed by GitHub
parent 9b29b10de0
commit 85290c5972
2 changed files with 33 additions and 4 deletions

View File

@@ -106,12 +106,19 @@ jQuery.extend( {
// Hooks for boolean attributes
boolHook = {
set: function( elem, value, name ) {
var strValue = String( value );
if ( value === false ) {
// Remove boolean attributes when set to false
jQuery.removeAttr( elem, name );
} else {
elem.setAttribute( name, name );
elem.setAttribute( name,
name.toLowerCase() === "hidden" &&
strValue.toLowerCase() === "until-found" ?
strValue :
name
);
}
return name;
}
@@ -129,9 +136,13 @@ jQuery.each( jQuery.expr.match.bool.source.match( /\w+/g ), function( _i, name )
// Avoid an infinite loop by temporarily removing this function from the getter
handle = attrHandle[ lowercaseName ];
attrHandle[ lowercaseName ] = ret;
ret = getter( elem, name, isXML ) != null ?
lowercaseName :
null;
ret = getter( elem, name, isXML );
ret = ret == null ||
( lowercaseName === "hidden" && ret.toLowerCase() === "until-found" ) ?
ret :
lowercaseName;
attrHandle[ lowercaseName ] = handle;
}
return ret;

View File

@@ -479,6 +479,24 @@ QUnit.test( "attr(String, Object)", function( assert ) {
assert.equal( jQuery( "#name" ).attr( "nonexisting", undefined ).attr( "nonexisting" ), undefined, ".attr('attribute', undefined) does not create attribute (trac-5571)" );
} );
QUnit.test( "attr( previously-boolean-attr, non-boolean-value)", function( assert ) {
assert.expect( 3 );
var div = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" );
div.attr( "hidden", "foo" );
assert.strictEqual( div.attr( "hidden" ), "hidden",
"Values normalized for previously-boolean hidden attribute" );
div.attr( "hidden", "until-found" );
assert.strictEqual( div.attr( "hidden" ), "until-found",
"`until-found` value preserved for hidden attribute" );
div.attr( "hiDdeN", "uNtil-fOund" );
assert.strictEqual( div.attr( "hidden" ), "uNtil-fOund",
"`uNtil-fOund` different casing preserved" );
} );
QUnit.test( "attr(non-ASCII)", function( assert ) {
assert.expect( 2 );