Attributes: Shave off a couple of bytes
[jquery.git] / src / attributes / attr.js
blob426a8e5245d3e733bf06a000de42a2168d439024
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 );
12 removeAttr: function( name ) {
13 return this.each( function() {
14 jQuery.removeAttr( this, name );
15 } );
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;
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 );
43 return;
46 if ( hooks && "set" in hooks &&
47 ( ret = hooks.set( elem, value, name ) ) !== undefined ) {
48 return ret;
51 elem.setAttribute( name, value );
52 return value;
55 if ( hooks && "get" in hooks && ( ret = hooks.get( elem, name ) ) !== null ) {
56 return ret;
59 ret = elem.getAttribute( name );
61 // Non-existent attributes return null, we normalize to undefined
62 return ret == null ? undefined : ret;
65 attrHooks: {},
67 removeAttr: function( elem, value ) {
68 var name,
69 i = 0,
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 );
81 } );
83 // Support: IE <=11+
84 // An input loses its value after becoming a radio
85 if ( isIE ) {
86 jQuery.attrHooks.type = {
87 set: function( elem, value ) {
88 if ( value === "radio" && nodeName( elem, "input" ) ) {
89 var val = elem.value;
90 elem.setAttribute( "type", value );
91 if ( val ) {
92 elem.value = val;
94 return 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
105 jQuery.each( (
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 ?
112 name.toLowerCase() :
113 null;
116 set: function( elem, value, name ) {
117 if ( value === false ) {
119 // Remove boolean attributes when set to false
120 jQuery.removeAttr( elem, name );
121 } else {
122 elem.setAttribute( name, name );
124 return name;
127 } );