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 ||
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 );
53 if ( hooks && "set" in hooks &&
54 ( ret = hooks.set( elem, value, name ) ) !== undefined ) {
58 elem.setAttribute( name, value );
62 if ( hooks && "get" in hooks && ( ret = hooks.get( elem, name ) ) !== null ) {
66 ret = elem.getAttribute( name );
68 // Non-existent attributes return null, we normalize to undefined
69 return ret == null ? undefined : ret;
74 removeAttr: function( elem, value ) {
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 );
91 // An input loses its value after becoming a radio
93 jQuery.attrHooks.type = {
94 set: function( elem, value ) {
95 if ( value === "radio" && nodeName( elem, "input" ) ) {
97 elem.setAttribute( "type", value );