PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/dom/test-dom-attr.html

https://bitbucket.org/cng1985/kissy
HTML | 235 lines | 194 code | 40 blank | 1 comment | 0 complexity | 26369bef16ccee92e3837c73410160fa MD5 | raw file
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title>dom-attr Test</title>
  6. </head>
  7. <body>
  8. <script src="../test/test.js"></script>
  9. <script>
  10. KISSY.Test.Config.times = 1;
  11. </script>
  12. <h2>Test Data</h2>
  13. <script src="../../build/seed/seed-pkg.js"></script>
  14. <script src="../../build/ua/ua-pkg.js"></script>
  15. <script src="dom.js"></script>
  16. <script src="selector.js"></script>
  17. <script src="dom-data.js"></script>
  18. <script src="dom-class.js"></script>
  19. <script src="dom-attr.js"></script>
  20. <div id="test-data">
  21. <p id="foo">
  22. <a href="../kissy/" style="color:red; border-top:1px solid #333;" class="link" title="test" data-test="test">test link</a>
  23. <input type="text" id="test-input" readonly maxlength="20" value="hello"/>
  24. <input type="radio" id="test-radio" />
  25. <input type="radio" id="test-radio2" checked />
  26. <label class="test" for="test-input">label</label>
  27. <button type="button" tabindex="3">Submit</button>
  28. <textarea rows="2" cols="2">
  29. test
  30. </textarea>
  31. </p>
  32. <div id="test-div"></div>
  33. <img id="test-img" src="../../docs/assets/logo.png" alt="kissy" />
  34. <table id="test-table" cellspacing="10">
  35. <tbody>
  36. <tr>
  37. <td rowspan="2" colspan="3">td</td>
  38. </tr>
  39. </tbody>
  40. </table>
  41. <select id="test-select">
  42. <option id="test-opt" value="1">0</option>
  43. <option>2</option>
  44. <option>3</option>
  45. </select>
  46. <select id="test-select2">
  47. <option>2</option>
  48. </select>
  49. <select id="test-select3" multiple autocomplete="off">
  50. <option selected>1</option>
  51. <option selected>2</option>
  52. <option>3</option>
  53. </select>
  54. <br />
  55. <br />
  56. <input type="checkbox" id="test-20100728-checkbox" />test checked
  57. </div>
  58. <!-- Test Cases -->
  59. <script>
  60. var S = KISSY, DOM = S.DOM,
  61. foo = S.get('#foo'),
  62. a = S.get('#foo a'),
  63. img = S.get('#test-img'),
  64. input = S.get('#foo input'),
  65. radio = S.get('#test-radio'),
  66. radio2 = S.get('#test-radio2'),
  67. button = S.get('#foo button'),
  68. label = S.get('#foo label'),
  69. table = S.get('#test-table'),
  70. td = S.get('#test-table td'),
  71. select = S.get('#test-select'),
  72. select2 = S.get('#test-select2'),
  73. select3 = S.get('#test-select3'),
  74. opt = S.get('#test-opt'),
  75. div = S.get('#test-div'),
  76. opt2 = S.query('#test-select option')[1],
  77. area = S.get('#foo textarea');
  78. function test_attr(test) {
  79. ///////////////////
  80. // 不存在的属性:
  81. //alert(a.getAttribute('no-exist')); // null
  82. //alert(a['no-exist']); // undefined
  83. if (DOM.attr(a, 'no-exist') !== undefined) test.fail(); // kissy 里,对不存在的属性,统一返回 undefined
  84. //////////////////////
  85. // 对于 mapping 属性:
  86. // 1. readonly, checked, selected 属性只能通过 el[name] 才能正确获取
  87. // 2. 可以获取用 getAttribute 不一定能获取到的值,比如 tabindex 默认值
  88. //alert(input.readonly);
  89. //alert(input.readOnly); // true, 这是预期值,其它方式都有兼容性问题
  90. //alert(input.getAttribute('readonly'));
  91. //alert(input.getAttribute('readOnly'));
  92. if (DOM.attr(input, 'readonly') !== true) test.fail(DOM.attr(input, 'readonly'));
  93. //if (DOM.attr(a, 'readonly') !== undefined) test.fail();
  94. if (DOM.attr(radio, 'checked') !== false) test.fail();
  95. if (DOM.attr(input, 'value') !== 'hello') test.fail();
  96. //alert(a.getAttribute('style'));
  97. //alert(DOM.attr(a, 'style'));
  98. if (!S.isString(DOM.attr(a, 'style'))) test.fail();
  99. if (DOM.attr(opt, 'selected') !== true) test.fail();
  100. ///////////////////////////
  101. // 对于非 mapping 属性:
  102. // ie 下可以用 a.name 或 a['name'] 获取,其它浏览器下不能,即便有值也返回 undefined
  103. //alert(a['data-test']);
  104. //alert(a.getAttribute('data-test'));
  105. if (DOM.attr(a, 'data-test') !== 'test') test.fail();
  106. /////////////////////
  107. // ie bugs fix:
  108. if (DOM.attr(label, 'class') !== 'test') test.fail(); // ie7- 要用 className
  109. if (DOM.attr(label, 'for') !== 'test-input') test.fail(); // ie7- 要用 htmlFor
  110. // href - http://www.glennjones.net/Post/809/getAttributehrefbug.htm
  111. //alert(a.href); // 在所有浏览器下,a.href 和 a['href'] 都返回绝对地址
  112. //alert(a.getAttribute('href')); // ie7- 下,会返回绝对地址
  113. if (DOM.attr(a, 'href') !== '../kissy/') test.fail();
  114. if (DOM.attr(img, 'src') !== '../../docs/assets/logo.png') test.fail();
  115. // colspan / rowspan:
  116. if (DOM.attr(td, 'rowspan') != 2) test.fail();
  117. //if($.attr(td, 'rowspan') != 2) test.fail(); // jquery has bug in ie7-
  118. ///////////////////
  119. // 普通属性的获取:
  120. if (DOM.attr(a, 'title') !== 'test') test.fail();
  121. ///////////////////
  122. // 属性的设置:
  123. // normal
  124. DOM.attr(a, 'data-set', 'test-xx');
  125. if (DOM.attr(a, 'data-set') !== 'test-xx') test.fail();
  126. // style
  127. DOM.attr(td, 'style', 'padding: 10px; border: 1px solid #333;');
  128. ///////////////////
  129. // 异常参数测试:
  130. DOM.attr();
  131. DOM.attr(undefined,'name');
  132. DOM.attr(undefined,'name', '');
  133. ///////////////////
  134. // batch 测试:
  135. if(DOM.attr('input', 'id') !== 'hidepasses') test.fail();
  136. DOM.attr('#test-data div', 'data-test', 'test');
  137. if(DOM.attr('#test-data div', 'data-test') !== 'test') test.fail();
  138. DOM.attr([td], 'style', 'background: #eee; padding: 10px');
  139. ////////////////////
  140. // 测试 checked 的 setter
  141. var checkbox2 = S.get('#test-20100728-checkbox');
  142. checkbox2.checked = false;
  143. DOM.attr(checkbox2, 'checked', true);
  144. if(DOM.attr(checkbox2, 'checked') !== true) test.fail('attr checked');
  145. DOM.removeAttr(checkbox2, 'checked');
  146. if(DOM.attr(checkbox2, 'checked') !== false) test.fail('removeAttr checked');
  147. }
  148. function test_removeAttr(test) {
  149. // normal
  150. DOM.attr(label, 'test-remove', 'xx');
  151. if (DOM.attr(label, 'test-remove') !== 'xx') test.fail();
  152. DOM.removeAttr(label, 'test-remove');
  153. if (DOM.attr(label, 'test-remove') !== undefined) test.fail();
  154. // style
  155. DOM.removeAttr(a, 'style');
  156. //alert(DOM.attr(a, 'style'));
  157. if (DOM.attr(a, 'style')) test.fail();
  158. }
  159. function test_val(test) {
  160. // normal
  161. if (DOM.val(input) !== 'hello') test.fail();
  162. // area
  163. if (DOM.val(area).length !== 25) test.fail();
  164. // option
  165. if(DOM.val(opt) != 1) test.fail();
  166. if(DOM.val(opt2) != 2) test.fail();
  167. // select
  168. if(DOM.val(select) != 1) test.fail();
  169. if(DOM.val(select2) != 2) test.fail();
  170. if(DOM.val(select3).toString() != '1,2') test.fail();
  171. // radio
  172. if(DOM.val(radio) != 'on') test.fail();
  173. if(DOM.val(radio2) != 'on') test.fail();
  174. // set value
  175. DOM.val(a, 'test');
  176. if(DOM.val(a) !== 'test') test.fail();
  177. DOM.removeAttr(a, 'value');
  178. // select set value
  179. DOM.val(select, '3');
  180. if(DOM.val(select) != 3) test.fail();
  181. DOM.val(select, 0); // restore
  182. // select set value
  183. DOM.val(select, 3);
  184. if(DOM.val(select) != 3) test.fail();
  185. DOM.val(select, 0); // restore
  186. DOM.val(select3, ['2','3']);
  187. if(DOM.val(select3).length != 2) test.fail();
  188. }
  189. function test_text(test) {
  190. DOM.text(div, 'hello, are you ok?');
  191. if(DOM.text(div) !== 'hello, are you ok?') test.fail();
  192. }
  193. </script>
  194. </body>
  195. </html>