Manipulation: Make jQuery.cleanData not skip elements during cleanup
[jquery.git] / src / attributes / attr.js
blob118895916eee67c32ce0c3f873257e1eedaeb3c7
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";
7 jQuery.fn.extend( {
8         attr: function( name, value ) {
9                 return access( this, jQuery.attr, name, value, arguments.length > 1 );
10         },
12         removeAttr: function( name ) {
13                 return this.each( function() {
14                         jQuery.removeAttr( this, name );
15                 } );
16         }
17 } );
19 jQuery.extend( {
20         attr: function( elem, name, value ) {
21                 var ret, hooks,
22                         nType = elem.nodeType;
24                 // Don't get/set attributes on text, comment and attribute nodes
25                 if ( nType === 3 || nType === 8 || nType === 2 ) {
26                         return;
27                 }
29                 // Fallback to prop when attributes are not supported
30                 if ( typeof elem.getAttribute === "undefined" ) {
31                         return jQuery.prop( elem, name, value );
32                 }
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() ];
38                 }
40                 if ( value !== undefined ) {
41                         if ( value === null ||
43                                 // For compat with previous handling of boolean attributes,
44                                 // remove when `false` passed. For ARIA attributes -
45                                 // many of which recognize a `"false"` value - continue to
46                                 // set the `"false"` value as jQuery <4 did.
47                                 ( value === false && name.toLowerCase().indexOf( "aria-" ) !== 0 ) ) {
49                                 jQuery.removeAttr( elem, name );
50                                 return;
51                         }
53                         if ( hooks && "set" in hooks &&
54                                 ( ret = hooks.set( elem, value, name ) ) !== undefined ) {
55                                 return ret;
56                         }
58                         elem.setAttribute( name, value );
59                         return value;
60                 }
62                 if ( hooks && "get" in hooks && ( ret = hooks.get( elem, name ) ) !== null ) {
63                         return ret;
64                 }
66                 ret = elem.getAttribute( name );
68                 // Non-existent attributes return null, we normalize to undefined
69                 return ret == null ? undefined : ret;
70         },
72         attrHooks: {},
74         removeAttr: function( elem, value ) {
75                 var name,
76                         i = 0,
78                         // Attribute names can contain non-HTML whitespace characters
79                         // https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
80                         attrNames = value && value.match( rnothtmlwhite );
82                 if ( attrNames && elem.nodeType === 1 ) {
83                         while ( ( name = attrNames[ i++ ] ) ) {
84                                 elem.removeAttribute( name );
85                         }
86                 }
87         }
88 } );
90 // Support: IE <=11+
91 // An input loses its value after becoming a radio
92 if ( isIE ) {
93         jQuery.attrHooks.type = {
94                 set: function( elem, value ) {
95                         if ( value === "radio" && nodeName( elem, "input" ) ) {
96                                 var val = elem.value;
97                                 elem.setAttribute( "type", value );
98                                 if ( val ) {
99                                         elem.value = val;
100                                 }
101                                 return value;
102                         }
103                 }
104         };