prop.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. define( [
  2. "../core",
  3. "../core/access",
  4. "./support",
  5. "../selector"
  6. ], function( jQuery, access, support ) {
  7. "use strict";
  8. var rfocusable = /^(?:input|select|textarea|button)$/i,
  9. rclickable = /^(?:a|area)$/i;
  10. jQuery.fn.extend( {
  11. prop: function( name, value ) {
  12. return access( this, jQuery.prop, name, value, arguments.length > 1 );
  13. },
  14. removeProp: function( name ) {
  15. return this.each( function() {
  16. delete this[ jQuery.propFix[ name ] || name ];
  17. } );
  18. }
  19. } );
  20. jQuery.extend( {
  21. prop: function( elem, name, value ) {
  22. var ret, hooks,
  23. nType = elem.nodeType;
  24. // Don't get/set properties on text, comment and attribute nodes
  25. if ( nType === 3 || nType === 8 || nType === 2 ) {
  26. return;
  27. }
  28. if ( nType !== 1 || !jQuery.isXMLDoc( elem ) ) {
  29. // Fix name and attach hooks
  30. name = jQuery.propFix[ name ] || name;
  31. hooks = jQuery.propHooks[ name ];
  32. }
  33. if ( value !== undefined ) {
  34. if ( hooks && "set" in hooks &&
  35. ( ret = hooks.set( elem, value, name ) ) !== undefined ) {
  36. return ret;
  37. }
  38. return ( elem[ name ] = value );
  39. }
  40. if ( hooks && "get" in hooks && ( ret = hooks.get( elem, name ) ) !== null ) {
  41. return ret;
  42. }
  43. return elem[ name ];
  44. },
  45. propHooks: {
  46. tabIndex: {
  47. get: function( elem ) {
  48. // Support: IE <=9 - 11 only
  49. // elem.tabIndex doesn't always return the
  50. // correct value when it hasn't been explicitly set
  51. // https://web.archive.org/web/20141116233347/http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
  52. // Use proper attribute retrieval(#12072)
  53. var tabindex = jQuery.find.attr( elem, "tabindex" );
  54. if ( tabindex ) {
  55. return parseInt( tabindex, 10 );
  56. }
  57. if (
  58. rfocusable.test( elem.nodeName ) ||
  59. rclickable.test( elem.nodeName ) &&
  60. elem.href
  61. ) {
  62. return 0;
  63. }
  64. return -1;
  65. }
  66. }
  67. },
  68. propFix: {
  69. "for": "htmlFor",
  70. "class": "className"
  71. }
  72. } );
  73. // Support: IE <=11 only
  74. // Accessing the selectedIndex property
  75. // forces the browser to respect setting selected
  76. // on the option
  77. // The getter ensures a default option is selected
  78. // when in an optgroup
  79. // eslint rule "no-unused-expressions" is disabled for this code
  80. // since it considers such accessions noop
  81. if ( !support.optSelected ) {
  82. jQuery.propHooks.selected = {
  83. get: function( elem ) {
  84. /* eslint no-unused-expressions: "off" */
  85. var parent = elem.parentNode;
  86. if ( parent && parent.parentNode ) {
  87. parent.parentNode.selectedIndex;
  88. }
  89. return null;
  90. },
  91. set: function( elem ) {
  92. /* eslint no-unused-expressions: "off" */
  93. var parent = elem.parentNode;
  94. if ( parent ) {
  95. parent.selectedIndex;
  96. if ( parent.parentNode ) {
  97. parent.parentNode.selectedIndex;
  98. }
  99. }
  100. }
  101. };
  102. }
  103. jQuery.each( [
  104. "tabIndex",
  105. "readOnly",
  106. "maxLength",
  107. "cellSpacing",
  108. "cellPadding",
  109. "rowSpan",
  110. "colSpan",
  111. "useMap",
  112. "frameBorder",
  113. "contentEditable"
  114. ], function() {
  115. jQuery.propFix[ this.toLowerCase() ] = this;
  116. } );
  117. } );