cleardoc.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: shenlixia01
  4. * Date: 11-8-15
  5. * Time: 下午3:47
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. module( 'plugins.cleardoc' );
  9. test( '取得焦点后清空后查看range', function () {
  10. var editor = te.obj[0];
  11. editor.setContent( '<p>hello1</p><table><tr><td>hello2</td></tr></table>' );
  12. editor.focus();
  13. var body = editor.body;
  14. editor.execCommand( 'cleardoc' );
  15. ua.manualDeleteFillData( editor.body );
  16. if ( UE.browser.ie ) {
  17. equal( ua.getChildHTML( body ), '<p></p>' ); //目前ie清空文档后不放空格占位符
  18. }
  19. else {
  20. equal( ua.getChildHTML( body ), '<p><br></p>', '清空文档' );
  21. }
  22. } );
  23. test( '编辑器没有焦点,清空', function () {
  24. var editor = te.obj[0];
  25. editor.setContent( '<p>hello1</p><table><tr><td>hello2</td></tr></table>' );
  26. var body = editor.body;
  27. editor.execCommand( 'cleardoc' );
  28. ua.manualDeleteFillData( editor.body );
  29. if ( UE.browser.ie ) {
  30. equal( ua.getChildHTML( body ), '<p></p>' );
  31. }
  32. else {
  33. equal( ua.getChildHTML( body ), '<p><br></p>', '清空文档' );
  34. }
  35. } );
  36. test( 'enterTag为br', function () {
  37. var editor = te.obj[0];
  38. editor.options.enterTag='br';
  39. editor.setContent( '<table><tr><td>hello</td></tr></table>' );
  40. var body = editor.body;
  41. editor.execCommand( 'cleardoc' );
  42. ua.manualDeleteFillData( editor.body );
  43. if (UE.browser.ie) {
  44. equal(ua.getChildHTML(body), '<br>', '清空文档');
  45. }
  46. else {
  47. equal(ua.getChildHTML(body), '<br>', '清空文档');
  48. }
  49. } );
  50. /*trace1061*/
  51. test( '删除时不会删除block元素', function() {
  52. if(ua.browser.opera) return 0;
  53. var editor = te.obj[0];
  54. editor.setContent( '<h1>hello</h1>' );
  55. setTimeout(function() {
  56. var range = te.obj[1];
  57. range.selectNode( editor.body.firstChild ).select();
  58. editor.execCommand( 'cleardoc' );
  59. equal( editor.body.lastChild.tagName.toLowerCase(), 'p', 'h1替换为p' );
  60. ua.manualDeleteFillData(editor.body);
  61. if ( !baidu.editor.browser.ie )
  62. equal( editor.body.lastChild.innerHTML, '<br>', '内容被删除了' );
  63. else
  64. equal( editor.body.lastChild.innerHTML, '', '内容被删除了' );
  65. // if(!ua.browser.opera){
  66. // range = editor.selection.getRange();
  67. // equal( range.startContainer.tagName.toLowerCase(), 'p', '光标位置' );
  68. // }
  69. start();
  70. },50);
  71. stop();
  72. } );
  73. test('选中文本,清空',function(){
  74. var editor = te.obj[0];
  75. var range = te.obj[1];
  76. editor.setContent('<p>hello</p><p>hello1</p>')
  77. range.selectNode(editor.body.firstChild).select();
  78. editor.execCommand('cleardoc');
  79. var br = ua.browser.ie?'':'<br>';
  80. equal(ua.getChildHTML(editor.body),'<p>'+br+'</p>','');
  81. });
  82. /*trace 1104*/
  83. test( '全选后删除', function() {
  84. var editor = te.obj[0];
  85. if ( baidu.editor.browser.ie )
  86. editor.setContent( '<p>dsafds&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>' );
  87. else
  88. editor.setContent( '<p><br></p><p><br></p><p><br></p><p>d<br></p><p><br></p><p><br></p><p><br></p><p><br></p><p><br></p>' );
  89. setTimeout(function() {
  90. editor.focus();
  91. editor.execCommand( 'selectall' );
  92. editor.execCommand( 'cleardoc' );
  93. ua.manualDeleteFillData(editor.body);
  94. equal( editor.body.childNodes.length, 1, '删除后只剩一个bolock元素' );
  95. equal( editor.body.firstChild.tagName.toLowerCase(), 'p', '删除后只剩一个p' );
  96. if ( !UE.browser.ie )
  97. equal( editor.body.lastChild.innerHTML, '<br>', '内容被删除了' );
  98. else
  99. equal( editor.body.lastChild.innerHTML, '', '内容被删除了' );
  100. start();
  101. },50);
  102. stop();
  103. } );
  104. test( '删除所有列表', function() {
  105. var editor = te.obj[0];
  106. editor.setContent('<ol><li>hello1</li><li>你好</li></ol>');
  107. setTimeout(function() {
  108. var body = editor.body;
  109. editor.focus();
  110. editor.execCommand( 'selectall' );
  111. editor.execCommand( 'cleardoc' );
  112. equal( body.childNodes.length, 1, '删除后只剩一个ol元素' );
  113. var br = UE.browser.ie?"":"<br>";
  114. equal( ua.getChildHTML(body), '<p>'+br+'</p>', '删除后只剩一个p' );
  115. start();
  116. },50);
  117. stop();
  118. } );