1 import { jQuery
} from "../core.js";
2 import { access
} from "../core/access.js";
3 import { nodeName
} from "../core/nodeName.js";
4 import { rnothtmlwhite
} from "../var/rnothtmlwhite.js";
5 import { isIE
} from "../var/isIE.js";
8 attr: function( name
, value
) {
9 return access( this, jQuery
.attr
, name
, value
, arguments
.length
> 1 );
12 removeAttr: function( name
) {
13 return this.each( function() {
14 jQuery
.removeAttr( this, name
);
20 attr: function( elem
, name
, value
) {
22 nType
= elem
.nodeType
;
24 // Don't get/set attributes on text, comment and attribute nodes
25 if ( nType
=== 3 || nType
=== 8 || nType
=== 2 ) {
29 // Fallback to prop when attributes are not supported
30 if ( typeof elem
.getAttribute
=== "undefined" ) {
31 return jQuery
.prop( elem
, name
, value
);
34 // Attribute hooks are determined by the lowercase version
35 // Grab necessary hook if one is defined
36 if ( nType
!== 1 || !jQuery
.isXMLDoc( elem
) ) {
37 hooks
= jQuery
.attrHooks
[ name
.toLowerCase() ];
40 if ( value
!== undefined ) {
41 if ( value
=== null ) {
42 jQuery
.removeAttr( elem
, name
);
46 if ( hooks
&& "set" in hooks
&&
47 ( ret
= hooks
.set( elem
, value
, name
) ) !== undefined ) {
51 elem
.setAttribute( name
, value
);
55 if ( hooks
&& "get" in hooks
&& ( ret
= hooks
.get( elem
, name
) ) !== null ) {
59 ret
= elem
.getAttribute( name
);
61 // Non-existent attributes return null, we normalize to undefined
62 return ret
== null ? undefined : ret
;
67 removeAttr: function( elem
, value
) {
71 // Attribute names can contain non-HTML whitespace characters
72 // https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
73 attrNames
= value
&& value
.match( rnothtmlwhite
);
75 if ( attrNames
&& elem
.nodeType
=== 1 ) {
76 while ( ( name
= attrNames
[ i
++ ] ) ) {
77 elem
.removeAttribute( name
);
84 // An input loses its value after becoming a radio
86 jQuery
.attrHooks
.type
= {
87 set: function( elem
, value
) {
88 if ( value
=== "radio" && nodeName( elem
, "input" ) ) {
90 elem
.setAttribute( "type", value
);
100 // HTML boolean attributes have special behavior:
101 // we consider the lowercase name to be the only valid value, so
102 // getting (if the attribute is present) normalizes to that, as does
103 // setting to any non-`false` value (and setting to `false` removes the attribute).
104 // See https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes
106 "checked selected async autofocus autoplay controls defer disabled " +
107 "hidden ismap loop multiple open readonly required scoped"
108 ).split( " " ), function( _i
, name
) {
109 jQuery
.attrHooks
[ name
] = {
110 get: function( elem
) {
111 return elem
.getAttribute( name
) != null ?
116 set: function( elem
, value
, name
) {
117 if ( value
=== false ) {
119 // Remove boolean attributes when set to false
120 jQuery
.removeAttr( elem
, name
);
122 elem
.setAttribute( name
, name
);