forked from jquery/jquery-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattributes.js
123 lines (101 loc) · 3.84 KB
/
attributes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { migratePatchFunc, migrateWarn } from "../main.js";
var oldJQueryAttr = jQuery.attr,
oldToggleClass = jQuery.fn.toggleClass,
booleans = "checked|selected|async|autofocus|autoplay|controls|defer|" +
"disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped",
rbooleans = new RegExp( "^(?:" + booleans + ")$", "i" ),
// Some formerly boolean attributes gained new values with special meaning.
// Skip the old boolean attr logic for those values.
extraBoolAttrValues = {
hidden: [ "until-found" ]
};
// HTML boolean attributes have special behavior:
// we consider the lowercase name to be the only valid value, so
// getting (if the attribute is present) normalizes to that, as does
// setting to any non-`false` value (and setting to `false` removes the attribute).
// See https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes
jQuery.each( booleans.split( "|" ), function( _i, name ) {
var origAttrHooks = jQuery.attrHooks[ name ] || {};
jQuery.attrHooks[ name ] = {
get: origAttrHooks.get || function( elem ) {
var attrValue;
if ( jQuery.migrateIsPatchEnabled( "boolean-attributes" ) ) {
attrValue = elem.getAttribute( name );
if ( attrValue !== name && attrValue != null &&
( extraBoolAttrValues[ name ] || [] )
.indexOf( String( attrValue ).toLowerCase() ) === -1
) {
migrateWarn( "boolean-attributes",
"Boolean attribute '" + name +
"' value is different from its lowercased name" );
return name.toLowerCase();
}
}
return null;
},
set: origAttrHooks.set || function( elem, value, name ) {
if ( jQuery.migrateIsPatchEnabled( "boolean-attributes" ) ) {
if ( value !== name &&
( extraBoolAttrValues[ name ] || [] )
.indexOf( String( value ).toLowerCase() ) === -1
) {
if ( value !== false ) {
migrateWarn( "boolean-attributes",
"Boolean attribute '" + name +
"' is not set to its lowercased name" );
}
if ( value === false ) {
// Remove boolean attributes when set to false
jQuery.removeAttr( elem, name );
} else {
elem.setAttribute( name, name );
}
return name;
}
}
}
};
} );
migratePatchFunc( jQuery, "attr", function( elem, name, value ) {
var nType = elem.nodeType;
// Fallback to the original method on text, comment and attribute nodes
// and when attributes are not supported.
if ( nType === 3 || nType === 8 || nType === 2 ||
typeof elem.getAttribute === "undefined" ) {
return oldJQueryAttr.apply( this, arguments );
}
if ( value === false && name.toLowerCase().indexOf( "aria-" ) !== 0 &&
!rbooleans.test( name ) ) {
migrateWarn( "attr-false",
"Setting the non-ARIA non-boolean attribute '" + name +
"' to false" );
jQuery.attr( elem, name, "false" );
return;
}
return oldJQueryAttr.apply( this, arguments );
}, "attr-false" );
migratePatchFunc( jQuery.fn, "toggleClass", function( state ) {
// Only deprecating no-args or single boolean arg
if ( state !== undefined && typeof state !== "boolean" ) {
return oldToggleClass.apply( this, arguments );
}
migrateWarn( "toggleClass-bool", "jQuery.fn.toggleClass( [ boolean ] ) is removed" );
// Toggle entire class name of each element
return this.each( function() {
var className = this.getAttribute && this.getAttribute( "class" ) || "";
if ( className ) {
jQuery.data( this, "__className__", className );
}
// If the element has a class name or if we're passed `false`,
// then remove the whole classname (if there was one, the above saved it).
// Otherwise bring back whatever was previously saved (if anything),
// falling back to the empty string if nothing was stored.
if ( this.setAttribute ) {
this.setAttribute( "class",
className || state === false ?
"" :
jQuery.data( this, "__className__" ) || ""
);
}
} );
}, "toggleClass-bool" );