paragraph.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. module( "plugins.paragraph" );
  2. /**
  3. * h1和p之间的转换
  4. * 表格中添加p和h1
  5. * 列表里加h1
  6. * 传入2个参数,style和attrs
  7. */
  8. test( '不闭合h1和p之间的转换', function() {
  9. var editor = te.obj[0];
  10. var range = te.obj[1];
  11. var body = editor.body;
  12. editor.setContent( '<p>hello</p>' );
  13. setTimeout(function(){
  14. range.selectNode( body.firstChild.firstChild ).select();
  15. /*p===>h1*/
  16. editor.execCommand( 'paragraph', 'h1' );
  17. equal( ua.getChildHTML( body ), '<h1>hello</h1>' );
  18. equal( editor.queryCommandValue( 'paragraph' ), 'h1', '当前的blcok元素为h1' );
  19. /*h1===>p*/
  20. range.selectNode( body.firstChild.firstChild ).select();
  21. editor.execCommand( 'paragraph', 'p' );
  22. equal( ua.getChildHTML( body ), '<p>hello</p>' );
  23. /*多个段落的部分文本*/
  24. editor.setContent( '<p>hello</p><h2>hello2</h2>' );
  25. setTimeout(function(){
  26. range.setStart( body.firstChild.firstChild, 2 ).setEnd( body.lastChild.firstChild, 1 ).select();
  27. editor.execCommand( 'paragraph', 'h3' );
  28. equal( ua.getChildHTML( body ), '<h3>hello</h3><h3>hello2</h3>' );
  29. equal( editor.queryCommandValue( 'paragraph' ), 'h3', '当前的blcok元素为h3' );
  30. start();
  31. },50);
  32. },50);
  33. stop();
  34. } );
  35. test( '闭合h1和p之间的转换', function() {
  36. var editor = te.obj[0];
  37. var range = te.obj[1];
  38. var body = editor.body;
  39. editor.setContent( '<p>hello</p><p>hello2</p>' );
  40. setTimeout(function(){
  41. range.setStart( body.firstChild.firstChild, 1 ).collapse( 1 ).select();
  42. /*p===>h1*/
  43. editor.execCommand( 'paragraph', 'h1' );
  44. equal( ua.getChildHTML( body ), '<h1>hello</h1><p>hello2</p>' );
  45. /*h1===>p*/
  46. range.setStart( body.firstChild.firstChild, 1 ).collapse( 1 ).select();
  47. editor.execCommand( 'paragraph', 'p' );
  48. equal( ua.getChildHTML( body ), '<p>hello</p><p>hello2</p>' );
  49. equal( editor.queryCommandValue( 'paragraph' ), 'p', '当前的blcok元素为p' );
  50. start();
  51. },50);
  52. stop();
  53. } );
  54. /*如果是h1===>p并且传参的话,h1不会变化。因为这段代码的操作是为了indent和justify做的,传入参数p只是为了好处理,所以不支持h1变为p*/
  55. test( '传入段落的样式', function() {
  56. var editor = te.obj[0];
  57. var range = te.obj[1];
  58. var body = editor.body;
  59. editor.setContent( '<p>hello</p><p>hello2</p>' );
  60. setTimeout(function(){
  61. range.setStart( body.firstChild.firstChild, 1 ).collapse( 1 ).select();
  62. /*p===>p,但是变化了样式*/
  63. editor.execCommand( 'paragraph', 'p', {style:'text-indent:2em'} );
  64. equal( body.firstChild.style.textIndent, '2em', '改变了第一个孩子的缩进量' );
  65. equal( body.firstChild.tagName.toLowerCase(), 'p', 'tagName仍然是p' );
  66. /*p===>h4,但是变化了样式*/
  67. editor.execCommand( 'paragraph', 'h4', {style:'text-indent:3em'} );
  68. equal( body.firstChild.style['textIndent'], '3em', '改变了第一个孩子的缩进量' );
  69. equal( body.firstChild.tagName.toLowerCase(), 'h4', 'tagName是h4' );
  70. start();
  71. },50);
  72. stop();
  73. } );
  74. test( '对表格设置样式', function() {
  75. var editor = te.obj[0];
  76. var range = te.obj[1];
  77. var body = editor.body;
  78. editor.setContent( '<table><tbody><tr><td><h1>hello1</h1></td></tr><tr><td></td></tr></tbody></table>' );
  79. setTimeout(function(){
  80. var tds = body.getElementsByTagName( 'td' );
  81. range.setStart( tds[0].firstChild, 0 ).collapse( 1 ).select();
  82. editor.currentSelectedArr = [tds[0]];
  83. /*h4===>p,但是变化了样式*/
  84. editor.execCommand( 'paragraph', 'p', {style:'text-indent:3em'} );
  85. equal( tds[0].firstChild.style['textIndent'], '3em', '改变了第一个孩子的缩进量' );
  86. // equal( tds[0].firstChild.tagName.toLowerCase(), 'h1', 'tagName仍然是h1' );
  87. range.setStart( tds[1], 0 ).collapse( 1 ).select();
  88. editor.currentSelectedArr = [tds[1]];
  89. editor.execCommand( 'paragraph', 'p', {style:'text-indent:3em'} );
  90. // ua.manualDeleteFillData( editor.body );
  91. ua.clearWhiteNode(tds[1]);
  92. equal( tds[1].firstChild.style['textIndent'], '3em', '改变了第一个孩子的缩进量' );
  93. equal( tds[1].firstChild.tagName.toLowerCase(), 'p', 'tagName是p' );
  94. start();
  95. },50);
  96. stop();
  97. } );