keystrokes.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: dongyancen
  4. * Date: 12-10-9
  5. * Time: 下午6:52
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. module( "plugins.keystrokes" );
  9. test('trace 3714跨节点输入tab键',function(){
  10. var editor = te.obj[0];
  11. editor.setContent( '<h1>hello<br></h1><p>he<img src="http://img.baidu.com/hi/jx2/j_0015.gif" />oll</p>' );
  12. var range = te.obj[1];
  13. setTimeout(function(){
  14. range.setStart( editor.body.lastChild.firstChild,0 ).setEnd(editor.body.lastChild.firstChild.nextSibling,1).select();
  15. ua.keydown(editor.body,{'keyCode':9});
  16. ua.keyup(editor.body,{'keyCode':9});
  17. setTimeout(function(){
  18. equal(te.obj[0].undoManger.list.length,1,'');
  19. var html = '<h1>hello<br></h1><p>&nbsp;&nbsp;&nbsp;&nbsp;oll</p>';
  20. equal(ua.getChildHTML(te.obj[0].body),html,'跨节点输入tab键');
  21. start();
  22. },20);
  23. },20);
  24. stop();
  25. });
  26. test('删除块元素,块元素在后',function(){
  27. var editor = te.obj[0];
  28. editor.setContent( '<h1>hello<br></h1><h2><img src="http://img.baidu.com/hi/jx2/j_0015.gif" /></h2>' );
  29. var range = te.obj[1];
  30. setTimeout(function(){
  31. range.setStart( editor.body.lastChild.lastChild,0 ).setEnd(editor.body.lastChild.lastChild,1).select();
  32. ua.keydown(editor.body,{'keyCode':8});
  33. ua.keyup(editor.body,{'keyCode':8});
  34. setTimeout(function(){
  35. equal(te.obj[0].undoManger.index,1,'');
  36. var html = '<h1>hello<br></h1>';
  37. if(!ua.browser.opera)
  38. equal(ua.getChildHTML(te.obj[0].body),html,'删除块元素');
  39. start();
  40. },20);
  41. },20);
  42. stop();
  43. });
  44. test('删除块元素,块元素在前',function(){
  45. var editor = te.obj[0];
  46. editor.setContent( '<h2><img src="http://img.baidu.com/hi/jx2/j_0015.gif" /></h2><h1>hello<br></h1>' );
  47. var range = te.obj[1];
  48. setTimeout(function(){
  49. range.setStart( editor.body.firstChild,0 ).setEnd(editor.body.firstChild,1).select();
  50. ua.keydown(editor.body,{'keyCode':8});
  51. ua.keyup(editor.body,{'keyCode':8});
  52. setTimeout(function(){
  53. equal(te.obj[0].undoManger.index,1,'');
  54. var html = '<h1>hello<br></h1>';
  55. equal(ua.getChildHTML(te.obj[0].body),html,'删除块元素');
  56. start();
  57. },20);
  58. },20);
  59. stop();
  60. });
  61. test('trace 2747 普通情况,选中一个节点,输入tab键',function(){
  62. var editor = te.obj[0];
  63. editor.setContent( '<h1>hello<br></h1><p>he<img src="http://img.baidu.com/hi/jx2/j_0015.gif" />oll</p>' );
  64. var range = te.obj[1];
  65. setTimeout(function(){
  66. range.setStart( editor.body.lastChild,1 ).setEnd(editor.body.lastChild,2).select();
  67. ua.keydown(editor.body,{'keyCode':9});
  68. ua.keyup(editor.body,{'keyCode':9});
  69. setTimeout(function(){
  70. equal(te.obj[0].undoManger.list.length,1,'');
  71. var html = '<h1>hello<br></h1><p>he&nbsp;&nbsp;&nbsp;&nbsp;oll</p>';
  72. equal(ua.getChildHTML(te.obj[0].body),html,'普通情况,选中一个节点,输入tab键');
  73. start();
  74. },20);
  75. },20);
  76. stop();
  77. });
  78. test('trace 2746 删除自闭合标签',function(){
  79. var editor = te.obj[0];
  80. editor.setContent( '<h1>hello<br></h1><p>he<img src="http://img.baidu.com/hi/jx2/j_0015.gif" />oll</p>' );
  81. var range = te.obj[1];
  82. setTimeout(function(){
  83. range.setStart( editor.body.lastChild,1 ).setEnd(editor.body.lastChild,2).select();
  84. ua.keydown(editor.body,{'keyCode':8});
  85. ua.keyup(editor.body,{'keyCode':8});
  86. setTimeout(function(){
  87. equal(te.obj[0].undoManger.index,1,'');
  88. var html = '<h1>hello<br></h1><p>heoll</p>';
  89. equal(ua.getChildHTML(te.obj[0].body),html,'删除自闭合标签');
  90. start();
  91. },20);
  92. },20);
  93. stop();
  94. });
  95. test('全选后,退格,剩下空p',function(){
  96. var editor = te.obj[0];
  97. editor.setContent( 'hello' );
  98. var range = te.obj[1];
  99. range.selectNode( editor.body.firstChild ).select();
  100. editor.execCommand( 'bold' );
  101. editor.execCommand('selectall');
  102. ua.keydown(editor.body,{'keyCode':8});
  103. ua.keyup(editor.body,{'keyCode':8});
  104. stop();
  105. setTimeout(function(){
  106. var br = ua.browser.ie?'':'<br>';
  107. equal(ua.getChildHTML(te.obj[0].body),'<p>'+br+'</p>','全选后,退格,剩下空p');
  108. start();
  109. },20);
  110. });
  111. //TODO 1.2.6
  112. //test('针对ff下在列表首行退格,不能删除空格行的问题 ',function(){
  113. // if(ua.browser.gecko){
  114. // var editor = te.obj[0];
  115. // editor.body.innerHTML = '<p>欢迎使用ueditor!</p><ol style="list-style-type:decimal;"><li><br /></li></ol>';
  116. // var range = te.obj[1];
  117. // setTimeout(function(){
  118. // range.setStartAtFirst(editor.body.firstChild).collapse(true);
  119. // ua.keyup(te.obj[0].body,{'keyCode':8});
  120. // setTimeout(function(){
  121. // equal(ua.getChildHTML(editor.body),'<p>欢迎使用ueditor!</p>','删除空行 ');
  122. // start();
  123. // },20);
  124. // },20);
  125. // stop();
  126. // }
  127. //});
  128. test('在列表中,跨行选中第2,3行,输入tab键',function(){
  129. var editor = te.obj[0];
  130. editor.setContent( '<ol style="list-style-type:decimal;"><li><p>欢迎使用</p></li><li><p>ueditor</p></li><li><p>ueditor</p></li></ol>' );
  131. var range = te.obj[1];
  132. setTimeout(function(){
  133. range.setStart( editor.body.childNodes[0].childNodes[1].firstChild.firstChild,1 ).setEnd(editor.body.childNodes[0].childNodes[2].firstChild.firstChild,1 ).select();
  134. ua.keydown(editor.body,{'keyCode':9});
  135. ua.keyup(editor.body,{'keyCode':9});
  136. setTimeout(function(){
  137. ua.manualDeleteFillData(te.obj[0].body);
  138. equal(te.obj[0].body.firstChild.tagName.toLowerCase(),'ol','原列表');
  139. equal($(te.obj[0].body.firstChild).css('list-style-type'),'decimal','原列表类型');
  140. equal(ua.getChildHTML(te.obj[0].body.firstChild.firstChild),'<p>欢迎使用</p>','第一行保持原来的列表样式');
  141. equal(te.obj[0].body.firstChild.lastChild.tagName.toLowerCase(),'ol','后两行变成第二层列表');
  142. equal($(te.obj[0].body.firstChild.lastChild).css('list-style-type'),'lower-alpha','第二层列表类型');
  143. equal(ua.getChildHTML(te.obj[0].body.firstChild.lastChild),'<li><p>ueditor</p></li><li><p>ueditor</p></li>','检查内容');
  144. start();
  145. },20);
  146. },50);
  147. stop();
  148. });
  149. //todo 这个检查存在问题,如何检查 evt.preventDefault();?
  150. test('在h1内输入del',function(){
  151. var editor = te.obj[0];
  152. editor.setContent( '<h1><br></h1><p>hello</p>' );
  153. var range = te.obj[1];
  154. setTimeout(function(){
  155. range.setStart(editor.body.childNodes[0],0).collapse(true).select(true);
  156. ua.keydown(te.obj[0].body,{'keyCode':46});
  157. ua.keyup(te.obj[0].body,{'keyCode':46});
  158. setTimeout(function(){
  159. equal(ua.getChildHTML(te.obj[0].body),'<h1><br></h1><p>hello</p>','在h1内输入del');
  160. start();
  161. },20);
  162. },20);
  163. stop();
  164. });
  165. test('在列表中,跨行选中,输入tab键',function(){
  166. var editor = te.obj[0];
  167. editor.setContent( '<ol style="list-style-type:decimal;"><li><p>欢迎使用</p></li><li><p>ueditor</p></li><li><p>ueditor</p></li></ol>' );
  168. var range = te.obj[1];
  169. setTimeout(function(){
  170. range.setStart( editor.body.firstChild.firstChild.firstChild.firstChild,1 ).setEnd(editor.body.firstChild.childNodes[1].firstChild.firstChild,1 ).select();
  171. ua.keydown(editor.body,{'keyCode':9});
  172. ua.keyup(editor.body,{'keyCode':9});
  173. setTimeout(function(){
  174. equal(te.obj[0].undoManger.index,1,'undoManger.index');
  175. ua.manualDeleteFillData(te.obj[0].body);
  176. equal(te.obj[0].body.firstChild.tagName.toLowerCase(),'ol','外面套了一层ol');
  177. equal(te.obj[0].body.firstChild.childNodes.length,2,'');
  178. equal(te.obj[0].body.firstChild.firstChild.tagName.toLowerCase(),'ol','原列表');
  179. equal($(te.obj[0].body.firstChild).css('list-style-type'),'decimal','原列表类型');
  180. equal(ua.getChildHTML(te.obj[0].body.firstChild.firstChild),'<li><p>欢迎使用</p></li><li><p>ueditor</p></li>','检查内容');
  181. start();
  182. },20);
  183. },50);
  184. stop();
  185. });
  186. test(' 光标定位到列表前,输入tab键',function(){
  187. var editor = te.obj[0];
  188. editor.setContent( '<ol style="list-style-type:decimal;"><li><p>欢迎使用</p></li><li><p>ueditor</p></li></ol>' );
  189. var range = te.obj[1];
  190. setTimeout(function(){
  191. range.setStart( editor.body.firstChild.firstChild.firstChild,0 ).collapse(true).select();
  192. ua.keydown(editor.body,{'keyCode':9});
  193. ua.keyup(editor.body,{'keyCode':9});
  194. setTimeout(function(){
  195. equal(te.obj[0].undoManger.list.length,1,'undoManger.index');
  196. ua.manualDeleteFillData(te.obj[0].body);
  197. equal($(te.obj[0].body.firstChild).css('list-style-type'),'decimal','原列表类型');
  198. equal(te.obj[0].body.firstChild.childNodes.length,2,'列表有两个子节点');
  199. equal($(te.obj[0].body.firstChild.firstChild).css('list-style-type'),'lower-alpha','第一个节点是另一类型的列表');
  200. equal(ua.getChildHTML(te.obj[0].body.firstChild.firstChild),'<li><p>欢迎使用</p></li>','检查内容');
  201. equal(te.obj[0].body.firstChild.lastChild.tagName.toLowerCase(),'li','第一个节点是原列表的li');
  202. equal(ua.getChildHTML(te.obj[0].body.firstChild.lastChild),'<p>ueditor</p>','检查内容');
  203. start();
  204. },20);
  205. },50);
  206. stop();
  207. });
  208. test( '删除inline的标签', function() {
  209. var editor = te.obj[0];
  210. editor.setContent( '<p><strong><em>hello world</em><span>wasai</span></strong></p>' );
  211. var range = te.obj[1];
  212. setTimeout(function(){
  213. var strong = editor.body.firstChild.firstChild;
  214. range.selectNode( strong ).select();
  215. ua.keydown(editor.body,{'keyCode':8});
  216. ua.keyup(editor.body,{'keyCode':8});
  217. setTimeout(function(){
  218. ua.manualDeleteFillData( editor.body );
  219. equal( editor.body.firstChild.tagName.toLowerCase(), 'p', 'strong 以及子inline节点都被删除' );
  220. if ( !baidu.editor.browser.ie )
  221. equal( editor.body.lastChild.innerHTML, '<br>', '内容被删除了' );
  222. else
  223. equal( editor.body.lastChild.innerHTML, '', '内容被删除了' );
  224. start();
  225. },20);
  226. },20);
  227. stop();
  228. } );
  229. /*trace 1089*/
  230. test( '跨行选择2个块元素', function() {
  231. var editor = te.obj[0];
  232. editor.setContent( '<p><strong>hello world<span>wasai</span></strong></p><div><em><span>hello 2</span></em></div>' );
  233. var range = te.obj[1];
  234. setTimeout(function(){
  235. var body = editor.body;
  236. range.setStart( body.firstChild, 0 ).setEnd( body.lastChild,1 ).select();
  237. ua.keydown(editor.body,{'keyCode':8});
  238. ua.keyup(editor.body,{'keyCode':8});
  239. setTimeout(function(){
  240. ua.manualDeleteFillData( editor.body );
  241. range = editor.selection.getRange();
  242. equal( body.childNodes.length, 1, 'div被删除,保留p' );
  243. var br = baidu.editor.browser.ie?"":"<br>";
  244. equal( ua.getChildHTML( body ), '<p>'+br+'</p>' );
  245. start();
  246. },20);
  247. },20);
  248. stop();
  249. } );
  250. //test('删除空节点 ',function(){
  251. // var editor = te.obj[0];
  252. // editor.setContent('<p><em><span style="color: red"><br></span></em></p>') ;
  253. // var range = te.obj[1];
  254. // setTimeout(function(){
  255. // range.setStartAtFirst(editor.body.getElementsByTagName('span')[0]).collapse(true).select(true);
  256. // ua.keyup(te.obj[0].body,{'keyCode':8});
  257. // setTimeout(function(){
  258. // var br = ua.browser.ie?'':'<br>';
  259. // equal(ua.getChildHTML(editor.body),'<p>'+br+'</p>','删除空节点');
  260. // start();
  261. // },20);
  262. // },20);
  263. // stop();
  264. //});