8000 Attributes: Support the `until-found` value for the `hidden` attribute · jquery/jquery@85290c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 85290c5

Browse files
authored
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
1 parent 9b29b10 commit 85290c5

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/attributes/attr.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,19 @@ jQuery.extend( {
106106
// Hooks for boolean attributes
107107
boolHook = {
108108
set: function( elem, value, name ) {
109+
var strValue = String( value );
110+
109111
if ( value === false ) {
110112

111113
// Remove boolean attributes when set to false
112114
jQuery.removeAttr( elem, name );
113115
} else {
114-
elem.setAttribute( name, name );
116+
elem.setAttribute( name,
117+
name.toLowerCase() === "hidden" &&
118+
strValue.toLowerCase() === "until-found" ?
119+
strValue :
120+
name
121+
);
115122
}
116123
return name;
117124
}
@@ -129,9 +136,13 @@ jQuery.each( jQuery.expr.match.bool.source.match( /\w+/g ), function( _i, name )
129136
// Avoid an infinite loop by temporarily removing this function from the getter
130137
handle = attrHandle[ lowercaseName ];
131138
attrHandle[ lowercaseName ] = ret;
132-
ret = getter( elem, name, isXML ) != null ?
133-
lowercaseName :
134-
null;
139+
140+
ret = getter( elem, name, isXML );
141+
ret = ret == null ||
142+
( lowercaseName === "hidden" && ret.toLowerCase() === "until-found" ) ?
143+
ret :
144+
lowercaseName;
145+
135146
attrHandle[ lowercaseName ] = handle;
136147
}
137148
return ret;

test/unit/attributes.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,24 @@ QUnit.test( "attr(String, Object)", function( assert ) {
479479
assert.equal( jQuery( "#name" ).attr( "nonexisting", undefined ).attr( "nonexisting" ), undefined, ".attr('attribute', undefined) does not create attribute (trac-5571)" );
480480
} );
481481

482+
QUnit.test( "attr( previously-boolean-attr, non-boolean-value)", function( assert ) {
483+
assert.expect( 3 );
484+
485+
var div = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" );
486+
487+
div.attr( "hidden", "foo" );
488+
assert.strictEqual( div.attr( "hidden" ), "hidden",
489+
"Values normalized for previously-boolean hidden attribute" );
490+
491+
div.attr( "hidden", "until-found" );
492+
assert.strictEqual( div.attr( "hidden" ), "until-found",
493+
"`until-found` value preserved for hidden attribute" );
494+
495+
div.attr( "hiDdeN", "uNtil-fOund" );
496+
assert.strictEqual( div.attr( "hidden" ), "uNtil-fOund",
497+
"`uNtil-fOund` different casing preserved" );
498+
} );
499+
482500
QUnit.test( "attr(non-ASCII)", function( assert ) {
483501
assert.expect( 2 );
484502

0 commit comments

Comments
 (0)
0