PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/dom/test-dom-create.html

https://bitbucket.org/cng1985/kissy
HTML | 179 lines | 150 code | 28 blank | 1 comment | 0 complexity | 4a5f4c90e00e8021d785a30579fdbc14 MD5 | raw file
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title>dom-create 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. <script src="dom-traversal.js"></script>
  21. <script src="dom-create.js"></script>
  22. <script src="../../build/sizzle/sizzle-pkg.js"></script>
  23. <div id="test-data">
  24. <p id="foo">
  25. <a href="../kissy/" style="color:red" class="link" title="test" data-test="test">test link</a>
  26. <input type="text" id="test-input" readonly maxlength="20" value="hello"/>
  27. <input type="radio" id="test-radio" />
  28. <input type="radio" id="test-radio2" checked />
  29. <label class="test" for="test-input">label</label>
  30. <button type="button" tabindex="3">Submit</button>
  31. <textarea rows="2" cols="2">
  32. test
  33. </textarea>
  34. </p>
  35. <div id="test-div"></div>
  36. <table id="test-table" cellspacing="10">
  37. <tbody>
  38. <tr>
  39. <td rowspan="2" colspan="3">td</td>
  40. </tr>
  41. </tbody>
  42. </table>
  43. <select id="test-select">
  44. <option id="test-opt" value="1">0</option>
  45. <option>2</option>
  46. <option>3</option>
  47. </select>
  48. <select id="test-select2">
  49. <option>2</option>
  50. </select>
  51. <select id="test-select3" multiple autocomplete="off">
  52. <option selected>1</option>
  53. <option selected>2</option>
  54. <option>3</option>
  55. </select>
  56. <div id="test-children" class="test-parent">
  57. <p id="test-next"><a>1</a></p>
  58. <p class="test-next-p"><a class="test-a">2</a></p>
  59. <p class="test-next"><a id="test-parent3">3</a></p>
  60. <p class="test-p" id="test-prev"><em class="test-em"><span><a id="test-parent4">4</a></span></em></p>
  61. </div>
  62. <div id="test-html">
  63. <p class="test-p">test html</p>
  64. </div>
  65. <div id="test-sethtml">initial html</div>
  66. <div id="test-sethtml2">initial html</div>
  67. <div id="test-sethtml3">initial html</div>
  68. </div>
  69. <style>
  70. .red { background: red; width: 50px; height: 50px; }
  71. </style>
  72. <!-- Test Cases -->
  73. <script>
  74. var S = KISSY, DOM = S.DOM;
  75. function test_create(test) {
  76. var div = DOM.create('<div>'), html = '', tag = '';
  77. S.each(['option', 'optgroup', 'td', 'th', 'tr', 'tbody', 'thead', 'tfoot', 'caption', 'col', 'colgroup', 'legend'], function(tag) {
  78. html = '<' + tag + '></' + tag + '>';
  79. //div.innerHTML = html;
  80. div.appendChild(DOM.create(html));
  81. S.log(tag + ' - ' + div.innerHTML);
  82. html = div.innerHTML.toLowerCase();
  83. if (!(html.indexOf('<' + tag + '>') === 0 || html.indexOf('<' + tag + ' ') === 0)) test.fail(tag);
  84. div.innerHTML = '';
  85. });
  86. // script
  87. html = tag = 'script';
  88. div.appendChild(DOM.create('<script><\/script>'));
  89. S.log(tag + ' - ' + div.innerHTML);
  90. html = S.trim(div.innerHTML.toLowerCase());
  91. if (!(html.indexOf('<' + tag + '>') === 0 || html.indexOf('<' + tag + ' ') === 0)) test.fail(tag);
  92. div.innerHTML = '';
  93. // null
  94. if(DOM.create() !== null) test.fail();
  95. // textNode
  96. if(DOM.create('text node').nodeType !== 3) test.fail();
  97. // 稍微复杂点
  98. if(DOM.attr(DOM.create('<img id="test-img" />'), 'id') !== 'test-img') test.fail();
  99. // 多个元素
  100. if(DOM.create('<p></p><div></div>').nodeType !== 11) test.fail();
  101. if(DOM.create('<p></p><div></div>').childNodes[0].tagName !== 'P') test.fail();
  102. // 属性支持
  103. if(DOM.create('<p>', { rel: '-1', 'class': 'test-p', data: 'test'}).className !== 'test-p') test.fail();
  104. }
  105. function test_html(test) {
  106. var t = S.get('#test-html');
  107. DOM.html(t, '<div>');
  108. if(t.firstChild.nodeName !== 'DIV') test.fail();
  109. DOM.html(t, '<p class="test-html">test p</p>');
  110. if(!DOM.hasClass(t.firstChild, 'test-html')) test.fail();
  111. if(DOM.text(t) !== 'test p') test.fail();
  112. try {
  113. DOM.html(t, '');
  114. //S.get('#test-table').innerHTML = ''; // will throw error in ie
  115. DOM.html('#test-table', '2');
  116. } catch(ex) {
  117. test.fail(ex);
  118. }
  119. // loadScripts
  120. DOM.html('#test-sethtml', '<script>KISSY.Test.echo("echo from loadScripts 1.");window.g_sethtml = 1;<\/script>', true);
  121. S.later(function() {
  122. if(window['g_sethtml'] !== 1) KISSY.Test.echo('html() is FAILED for loadScripts 1.');
  123. }, 500);
  124. // callback
  125. DOM.html('#test-sethtml2', '<script>KISSY.Test.echo("echo from loadScripts 2.");window.g_sethtml2 = 2;<\/script>', true, function() {
  126. test.echo('loadScripts 2 callback is fired.')
  127. });
  128. S.later(function() {
  129. if(window['g_sethtml2'] !== 2) KISSY.Test.echo('html() is FAILED for loadScripts 2.');
  130. }, 500);
  131. // src js
  132. DOM.html('#test-sethtml3', '<script src="test-dom-create.js"><\/script>', true);
  133. S.later(function() {
  134. if(window['g_testLoadScriptViaInnerHTML']) KISSY.Test.echo('test-dom-create.js callback is fired.');
  135. }, 500);
  136. // exceptions
  137. try {
  138. DOM.html(t, 1);
  139. } catch(ex) {
  140. test.fail(ex);
  141. }
  142. }
  143. function test_remove(test) {
  144. DOM.remove('.test');
  145. if(S.query('.test').length !== 0) test.fail();
  146. }
  147. // temp code
  148. DOM.create('<col width="20" />');
  149. </script>
  150. </body>
  151. </html>