inserthtml.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. module( "plugins.inserthtml" );
  2. test( '向span里面插入p', function() {
  3. var editor = te.obj[0];
  4. var range = te.obj[1];
  5. editor.setContent('<address><span style="color:#ff0000">hello1</span></address>');
  6. range.setStart(editor.body.firstChild.firstChild, 0 ).collapse(true).select();
  7. editor.execCommand( 'inserthtml','<p >hello3</p>' );
  8. stop();
  9. setTimeout(function(){
  10. // equal(editor.body.innerHTML.toLowerCase(),'<address><p >hello3</p><span style="color:#ff0000">hello1</span></address>','向span里面插入p');
  11. ua.checkSameHtml(editor.body.getElementsByTagName('address')[0].innerHTML.toLowerCase(),'<p >hello3</p><span style="color:#ff0000">hello1</span>','向span里面插入p');
  12. start();
  13. },50);
  14. });
  15. //列表中插入列表 TODO 1.2.6 trace 3413
  16. //test( '列表中插入列表 trace 3413', function() {
  17. // var editor = te.obj[0];
  18. // var range = te.obj[1];
  19. // editor.setContent('<ol><li><p>hello1</p></li><li><p>hello2</p></li></ol>');
  20. // var lis = editor.body.getElementsByTagName('li');
  21. // range.setStart( lis[1], 0 ).collapse(true).select();
  22. // editor.execCommand( 'inserthtml','<ul><li><p>hello3</p></li></ul>' );
  23. // stop();
  24. // setTimeout(function(){
  25. // lis = editor.body.getElementsByTagName('li');
  26. // equal(lis.length,3,'列表长度');
  27. // equal(lis[1].innerHTML.toLowerCase(),'<p>hello3</p>','列表中插入列表');
  28. // start();
  29. // },50);
  30. //
  31. //});
  32. test( 'trace 3301:闭合方式插入文本', function() {
  33. var editor = te.obj[0];
  34. var range = te.obj[1];
  35. var body = editor.body;
  36. editor.setContent( '<p><br></p>' );
  37. range.setStart( body.firstChild, 0 ).collapse( 1 ).select();
  38. editor.execCommand( 'inserthtml', 'hello2' );
  39. equal( ua.getChildHTML( body ), '<p>hello2</p>', '插入文本节点' );
  40. } );
  41. test( '选中多个单元格插入列表', function() {
  42. var editor = te.obj[0];
  43. var range = te.obj[1];
  44. var body = editor.body;
  45. editor.setContent( '<table><tbody><tr><td></td><td></td></tr></tbody></table>' );
  46. setTimeout(function(){
  47. var trs = editor.body.firstChild.getElementsByTagName( 'tr' );
  48. var ut = editor.getUETable(editor.body.firstChild);
  49. var cellsRange = ut.getCellsRange(trs[0].cells[0],trs[0].cells[1]);
  50. ut.setSelected(cellsRange);
  51. if(ua.browser.ie)
  52. range.setStart( trs[0].cells[0], 0 ).collapse( true ).select();
  53. var tds = body.firstChild.getElementsByTagName( 'td' );
  54. editor.execCommand( 'inserthtml', '<ol><li>hello</li></ol>' );
  55. equal( tds[0].firstChild.tagName.toLowerCase(), 'ol', '插入列表' );
  56. equal( ua.getChildHTML( tds[0].firstChild ), '<li><p>hello</p></li>', '查询列表内容' );
  57. //空的td有br
  58. var br = ua.browser.ie?'':'<br>';
  59. ua.manualDeleteFillData(tds[1]);
  60. equal( tds[1].innerHTML, br, '第二个单元格没有插入任何东西' );
  61. start();
  62. },50);
  63. stop();
  64. } );
  65. test( '表格中插入图片', function() {
  66. var editor = te.obj[0];
  67. var range = te.obj[1];
  68. var body = editor.body;
  69. editor.setContent( '<table><tbody><tr><td></td><td></td></tr></tbody></table>' );
  70. setTimeout(function(){
  71. var trs = editor.body.firstChild.getElementsByTagName( 'tr' );
  72. var ut = editor.getUETable(editor.body.firstChild);
  73. var cellsRange = ut.getCellsRange(trs[0].cells[0], trs[0].cells[1]);
  74. ut.setSelected(cellsRange);
  75. if (ua.browser.ie)
  76. range.setStart( trs[0].cells[0], 0 ).collapse( true ).select();
  77. var tds = body.firstChild.getElementsByTagName( 'td' );
  78. editor.execCommand( 'inserthtml', '<img style="float:left"/>' );
  79. equal( tds[0].firstChild.tagName.toLowerCase(), 'img', '插入图片' );
  80. equal( tds[0].firstChild.style['styleFloat']||tds[0].firstChild.style['cssFloat'], 'left', '查询图片浮动方式' );
  81. var br = ua.browser.ie?'':'<br>';
  82. ua.manualDeleteFillData(tds[1]);
  83. equal( tds[1].innerHTML, br, '第二个单元格没有插入任何东西' );
  84. start();
  85. },50);
  86. stop();
  87. } );
  88. //test('',function(){stop()});
  89. test( '选中多个单元格插入超链接', function() {
  90. if(ua.browser.ie>8)return ;//TODO 1.2.6
  91. var editor = te.obj[0];
  92. var range = te.obj[1];
  93. var body = editor.body;
  94. editor.setContent( '<table><tbody><tr><td></td><td></td></tr></tbody></table>' );
  95. setTimeout(function(){
  96. var trs = editor.body.firstChild.getElementsByTagName( 'tr' );
  97. var ut = editor.getUETable(editor.body.firstChild);
  98. var cellsRange = ut.getCellsRange(trs[0].cells[0],trs[0].cells[1]);
  99. ut.setSelected(cellsRange);
  100. if(ua.browser.ie&&ua.browser.ie<9)
  101. range.setStart( trs[0].cells[0], 0 ).collapse( true ).select();
  102. editor.execCommand( 'link', {href:'http://www.baidu.com/'} );
  103. var tds = body.firstChild.getElementsByTagName( 'td' );
  104. equal( tds[0].firstChild.tagName.toLowerCase(), 'a', '插入超链接' );
  105. var br = ua.browser.ie?'':'<br>';
  106. equal( ua.getChildHTML(tds[0]), '<a href="http://www.baidu.com/">http://www.baidu.com/</a>'+(ua.browser.ie>8?' ':br), '查询第一个表格插入的超链接' );
  107. equal( ua.getChildHTML(tds[1]), br, '第二个单元格也插入超链接' );
  108. start();
  109. },50);
  110. stop();
  111. } );
  112. test( 'trace 3297:notSerialize', function() {
  113. var editor = te.obj[0];
  114. var range = te.obj[1];
  115. var body = editor.body;
  116. editor.setContent( '<p><br></p>' );
  117. setTimeout(function(){
  118. range.setStart( body.firstChild, 0 ).collapse( 1 ).select();
  119. editor.execCommand( 'inserthtml', '<p>b</p>_ueditor_page_break_tag_' ,false);
  120. equal( editor.body.childNodes.length, 3, 'notSerialize=false 插入分页符' );
  121. equal( editor.body.childNodes[1].tagName.toLowerCase(), 'hr', '插入分页符 hr class=\"pagebreak\" ' );
  122. equal( editor.body.childNodes[1].className.toLowerCase(), "pagebreak", '插入分页符 hr class=\"pagebreak\" ' );
  123. editor.setContent( '<p><br></p>' );
  124. setTimeout(function(){
  125. range.setStart( body.firstChild, 0 ).collapse( 1 ).select();
  126. editor.execCommand( 'inserthtml', '<p>b</p>_ueditor_page_break_tag_' ,true);
  127. equal( editor.body.childNodes.length, 3, 'notSerialize=true 插入分页符' );
  128. equal( editor.body.childNodes[1].innerHTML , '_ueditor_page_break_tag_', '插入分页符');
  129. start();
  130. },50);
  131. },50);
  132. stop();
  133. } );
  134. //列表中插入表格
  135. test( '列表中插入表格', function() {
  136. var editor = te.obj[0];
  137. var range = te.obj[1];
  138. editor.setContent('<ol><li><p></p></li></ol>');
  139. var lis = editor.body.getElementsByTagName('li');
  140. range.setStart( lis[0], 0 ).collapse(true).select();
  141. editor.execCommand( 'inserttable', {numCols:2, numRows:2});
  142. stop();
  143. setTimeout(function(){
  144. equal(lis.length,1,'列表长度没有变化');
  145. equal(lis[0].firstChild.tagName.toLowerCase(),'table','列表中插入表格');
  146. start();
  147. },50);
  148. });
  149. //刘表中插入img
  150. test( '列表中插入img', function() {
  151. var editor = te.obj[0];
  152. var range = te.obj[1];
  153. editor.setContent('<ol><li><p></p></li></ol>');
  154. var lis = editor.body.getElementsByTagName('li');
  155. range.setStart( lis[0], 0 ).collapse(true).select();
  156. editor.execCommand( 'insertimage', {src:'http://img.baidu.com/hi/jx2/j_0001.gif', width:50, height:51} );
  157. stop();
  158. setTimeout(function(){
  159. equal(lis.length,1,'列表长度没有变化');
  160. ua.manualDeleteFillData(lis[0]);
  161. if(ua.browser.ie){
  162. equal(lis[0].firstChild.firstChild.tagName.toLowerCase(),'img','列表中插入img');
  163. equal(lis[0].firstChild.firstChild.attributes['src'].nodeValue,'http://img.baidu.com/hi/jx2/j_0001.gif','列表中插入img');
  164. }
  165. else{
  166. equal(lis[0].firstChild.tagName.toLowerCase(),'img','列表中插入img');
  167. equal(lis[0].firstChild.attributes['src'].nodeValue,'http://img.baidu.com/hi/jx2/j_0001.gif','列表中插入img');
  168. }
  169. start();
  170. },50);
  171. });