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

Skip to content

Commit 6ee4613

Browse files
committed
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. Ref gh-5388 Ref gh-5452
1 parent fc874a0 commit 6ee4613

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/attributes/attr.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ define( [
1010
"use strict";
1111

1212
var boolHook,
13-
attrHandle = jQuery.expr.attrHandle;
13+
attrHandle = jQuery.expr.attrHandle,
14+
15+
// Some formerly boolean attributes gained new values with special meaning.
16+
// Skip the old boolean attr logic for those values.
17+
extraBoolAttrValues = {
18+
hidden: [ "until-found" ]
19+
};
1420

1521
jQuery.fn.extend( {
1622
attr: function( name, value ) {
@@ -110,8 +116,13 @@ boolHook = {
110116

111117
// Remove boolean attributes when set to false
112118
jQuery.removeAttr( elem, name );
113-
} else {
119+
} else if (
120+
( extraBoolAttrValues[ name.toLowerCase() ] || [] )
121+
.indexOf( String( value ).toLowerCase() ) === -1
122+
) {
114123
elem.setAttribute( name, name );
124+
} else {
125+
elem.setAttribute( name, value );
115126
}
116127
return name;
117128
}
@@ -129,9 +140,16 @@ jQuery.each( jQuery.expr.match.bool.source.match( /\w+/g ), function( _i, name )
129140
// Avoid an infinite loop by temporarily removing this function from the getter
130141
handle = attrHandle[ lowercaseName ];
131142
attrHandle[ lowercaseName ] = ret;
132-
ret = getter( elem, name, isXML ) != null ?
133-
lowercaseName :
134-
null;
143+
144+
ret = getter( elem, name, isXML );
145+
if ( ret != null ) {
146+
if ( ( extraBoolAttrValues[ name ] || [] )
147+
.indexOf( ret.toLowerCase() ) === -1
148+
) {
149+
ret = lowercaseName;
150+
}
151+
}
152+
135153
attrHandle[ lowercaseName ] = handle;
136154
}
137155
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