Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dom-attrs-debug.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('dom-attrs', function(Y) {
  9. var documentElement = Y.config.doc.documentElement,
  10. Y_DOM = Y.DOM,
  11. TAG_NAME = 'tagName',
  12. OWNER_DOCUMENT = 'ownerDocument',
  13. EMPTY_STRING = '',
  14. addFeature = Y.Features.add,
  15. testFeature = Y.Features.test;
  16. Y.mix(Y_DOM, {
  17. /**
  18. * Returns the text content of the HTMLElement.
  19. * @method getText
  20. * @param {HTMLElement} element The html element.
  21. * @return {String} The text content of the element (includes text of any descending elements).
  22. */
  23. getText: (documentElement.textContent !== undefined) ?
  24. function(element) {
  25. var ret = '';
  26. if (element) {
  27. ret = element.textContent;
  28. }
  29. return ret || '';
  30. } : function(element) {
  31. var ret = '';
  32. if (element) {
  33. ret = element.innerText || element.nodeValue; // might be a textNode
  34. }
  35. return ret || '';
  36. },
  37. /**
  38. * Sets the text content of the HTMLElement.
  39. * @method setText
  40. * @param {HTMLElement} element The html element.
  41. * @param {String} content The content to add.
  42. */
  43. setText: (documentElement.textContent !== undefined) ?
  44. function(element, content) {
  45. if (element) {
  46. element.textContent = content;
  47. }
  48. } : function(element, content) {
  49. if ('innerText' in element) {
  50. element.innerText = content;
  51. } else if ('nodeValue' in element) {
  52. element.nodeValue = content;
  53. }
  54. },
  55. CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
  56. 'for': 'htmlFor',
  57. 'class': 'className'
  58. } : { // w3c
  59. 'htmlFor': 'for',
  60. 'className': 'class'
  61. },
  62. /**
  63. * Provides a normalized attribute interface.
  64. * @method setAttribute
  65. * @param {HTMLElement} el The target element for the attribute.
  66. * @param {String} attr The attribute to set.
  67. * @param {String} val The value of the attribute.
  68. */
  69. setAttribute: function(el, attr, val, ieAttr) {
  70. if (el && attr && el.setAttribute) {
  71. attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
  72. el.setAttribute(attr, val, ieAttr);
  73. }
  74. else { Y.log('bad input to setAttribute', 'warn', 'dom'); }
  75. },
  76. /**
  77. * Provides a normalized attribute interface.
  78. * @method getAttibute
  79. * @param {HTMLElement} el The target element for the attribute.
  80. * @param {String} attr The attribute to get.
  81. * @return {String} The current value of the attribute.
  82. */
  83. getAttribute: function(el, attr, ieAttr) {
  84. ieAttr = (ieAttr !== undefined) ? ieAttr : 2;
  85. var ret = '';
  86. if (el && attr && el.getAttribute) {
  87. attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
  88. ret = el.getAttribute(attr, ieAttr);
  89. if (ret === null) {
  90. ret = ''; // per DOM spec
  91. }
  92. }
  93. else { Y.log('bad input to getAttribute', 'warn', 'dom'); }
  94. return ret;
  95. },
  96. VALUE_SETTERS: {},
  97. VALUE_GETTERS: {},
  98. getValue: function(node) {
  99. var ret = '', // TODO: return null?
  100. getter;
  101. if (node && node[TAG_NAME]) {
  102. getter = Y_DOM.VALUE_GETTERS[node[TAG_NAME].toLowerCase()];
  103. if (getter) {
  104. ret = getter(node);
  105. } else {
  106. ret = node.value;
  107. }
  108. }
  109. // workaround for IE8 JSON stringify bug
  110. // which converts empty string values to null
  111. if (ret === EMPTY_STRING) {
  112. ret = EMPTY_STRING; // for real
  113. }
  114. return (typeof ret === 'string') ? ret : '';
  115. },
  116. setValue: function(node, val) {
  117. var setter;
  118. if (node && node[TAG_NAME]) {
  119. setter = Y_DOM.VALUE_SETTERS[node[TAG_NAME].toLowerCase()];
  120. if (setter) {
  121. setter(node, val);
  122. } else {
  123. node.value = val;
  124. }
  125. }
  126. },
  127. creators: {}
  128. });
  129. addFeature('value-set', 'select', {
  130. test: function() {
  131. var node = Y.config.doc.createElement('select');
  132. node.innerHTML = '<option>1</option><option>2</option>';
  133. node.value = '2';
  134. return (node.value && node.value === '2');
  135. }
  136. });
  137. if (!testFeature('value-set', 'select')) {
  138. Y_DOM.VALUE_SETTERS.select = function(node, val) {
  139. for (var i = 0, options = node.getElementsByTagName('option'), option;
  140. option = options[i++];) {
  141. if (Y_DOM.getValue(option) === val) {
  142. option.selected = true;
  143. //Y_DOM.setAttribute(option, 'selected', 'selected');
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. Y.mix(Y_DOM.VALUE_GETTERS, {
  150. button: function(node) {
  151. return (node.attributes && node.attributes.value) ? node.attributes.value.value : '';
  152. }
  153. });
  154. Y.mix(Y_DOM.VALUE_SETTERS, {
  155. // IE: node.value changes the button text, which should be handled via innerHTML
  156. button: function(node, val) {
  157. var attr = node.attributes.value;
  158. if (!attr) {
  159. attr = node[OWNER_DOCUMENT].createAttribute('value');
  160. node.setAttributeNode(attr);
  161. }
  162. attr.value = val;
  163. }
  164. });
  165. Y.mix(Y_DOM.VALUE_GETTERS, {
  166. option: function(node) {
  167. var attrs = node.attributes;
  168. return (attrs.value && attrs.value.specified) ? node.value : node.text;
  169. },
  170. select: function(node) {
  171. var val = node.value,
  172. options = node.options;
  173. if (options && options.length) {
  174. // TODO: implement multipe select
  175. if (node.multiple) {
  176. Y.log('multiple select normalization not implemented', 'warn', 'DOM');
  177. } else {
  178. val = Y_DOM.getValue(options[node.selectedIndex]);
  179. }
  180. }
  181. return val;
  182. }
  183. });
  184. }, '3.4.0' ,{requires:['dom-core']});