paragraph-intergration.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Paragraph from '../src/paragraph';
  6. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  7. import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
  8. import HeadingEngine from '@ckeditor/ckeditor5-heading/src/headingengine';
  9. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  10. import {
  11. getData as getModelData,
  12. setData as setModelData
  13. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { parse as parseView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  15. describe( 'Paragraph feature – integration', () => {
  16. describe( 'with clipboard', () => {
  17. it( 'pastes h1+h2+p as p+p+p when heading feature is not present', () => {
  18. return VirtualTestEditor.create( {
  19. plugins: [ Paragraph, Clipboard ]
  20. } )
  21. .then( newEditor => {
  22. const editor = newEditor;
  23. const doc = editor.document;
  24. const clipboard = editor.plugins.get( 'clipboard/clipboard' );
  25. setModelData( doc, '<paragraph>[]</paragraph>' );
  26. clipboard.fire( 'inputTransformation', {
  27. content: parseView( '<h1>foo</h1><h2>bar</h2><p>bom</p>' )
  28. } );
  29. expect( getModelData( doc ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph><paragraph>bom[]</paragraph>' );
  30. } );
  31. } );
  32. // Explainer: the heading feature is configured to handle h2-h4 elements, so h1 has no handler.
  33. it( 'pastes h1+h2+p as p+h2+p when heading feature is present', () => {
  34. return VirtualTestEditor.create( {
  35. plugins: [ Paragraph, Clipboard, HeadingEngine ]
  36. } )
  37. .then( newEditor => {
  38. const editor = newEditor;
  39. const doc = editor.document;
  40. const clipboard = editor.plugins.get( 'clipboard/clipboard' );
  41. setModelData( doc, '<paragraph>[]</paragraph>' );
  42. clipboard.fire( 'inputTransformation', {
  43. content: parseView( '<h1>foo</h1><h2>bar</h2><p>bom</p>' )
  44. } );
  45. expect( getModelData( doc ) ).to.equal( '<paragraph>foo</paragraph><heading1>bar</heading1><paragraph>bom[]</paragraph>' );
  46. } );
  47. } );
  48. it( 'pastes ul>li+li as p+p when list feature is not present', () => {
  49. return VirtualTestEditor.create( {
  50. plugins: [ Paragraph, Clipboard ]
  51. } )
  52. .then( newEditor => {
  53. const editor = newEditor;
  54. const doc = editor.document;
  55. const clipboard = editor.plugins.get( 'clipboard/clipboard' );
  56. setModelData( doc, '<paragraph>[]</paragraph>' );
  57. clipboard.fire( 'inputTransformation', {
  58. content: parseView( '<ul><li>foo</li><li>bar</li></ul>' )
  59. } );
  60. expect( getModelData( doc ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar[]</paragraph>' );
  61. } );
  62. } );
  63. // Check whether the paragraph feature doesn't breaking pasting such content by trying to
  64. // handle the li element.
  65. it( 'pastes ul>li>h2+h3+p as h2+h3+p when heading feature is present', () => {
  66. return VirtualTestEditor.create( {
  67. plugins: [ Paragraph, Clipboard, HeadingEngine ]
  68. } )
  69. .then( newEditor => {
  70. const editor = newEditor;
  71. const doc = editor.document;
  72. const clipboard = editor.plugins.get( 'clipboard/clipboard' );
  73. setModelData( doc, '<paragraph>[]</paragraph>' );
  74. clipboard.fire( 'inputTransformation', {
  75. content: parseView( '<ul><li>x</li><li><h2>foo</h2><h3>bar</h3><p>bom</p></li><li>x</li></ul>' )
  76. } );
  77. expect( getModelData( doc ) ).to.equal(
  78. '<paragraph>x</paragraph>' +
  79. '<heading1>foo</heading1><heading2>bar</heading2><paragraph>bom</paragraph>' +
  80. '<paragraph>x[]</paragraph>'
  81. );
  82. } );
  83. } );
  84. // See 'should convert ul>li>ul>li+li (in clipboard holder)' in clipboard.js.
  85. it( 'pastes ul>li>ul>li+li', () => {
  86. return VirtualTestEditor.create( {
  87. plugins: [ Paragraph, Clipboard ]
  88. } )
  89. .then( newEditor => {
  90. const editor = newEditor;
  91. const doc = editor.document;
  92. const clipboard = editor.plugins.get( 'clipboard/clipboard' );
  93. setModelData( doc, '<paragraph>[]</paragraph>' );
  94. clipboard.fire( 'inputTransformation', {
  95. content: parseView( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' )
  96. } );
  97. expect( getModelData( doc ) ).to.equal(
  98. '<paragraph>a</paragraph>' +
  99. '<paragraph>b</paragraph>' +
  100. '<paragraph>c[]</paragraph>'
  101. );
  102. } );
  103. } );
  104. // See 'should convert ul>li>p,text (in clipboard holder)' in clipboard.js.
  105. it( 'pastes ul>li>p,text', () => {
  106. return VirtualTestEditor.create( {
  107. plugins: [ Paragraph, Clipboard ]
  108. } )
  109. .then( newEditor => {
  110. const editor = newEditor;
  111. const doc = editor.document;
  112. const clipboard = editor.plugins.get( 'clipboard/clipboard' );
  113. setModelData( doc, '<paragraph>[]</paragraph>' );
  114. clipboard.fire( 'inputTransformation', {
  115. content: parseView( '<ul><li><p>a</p>b</li></ul>' )
  116. } );
  117. expect( getModelData( doc ) ).to.equal(
  118. '<paragraph>a</paragraph>' +
  119. '<paragraph>b[]</paragraph>'
  120. );
  121. } );
  122. } );
  123. } );
  124. describe( 'with undo', () => {
  125. it( 'fixing empty roots should be transparent to undo', () => {
  126. return VirtualTestEditor.create( {
  127. plugins: [ Paragraph, UndoEngine ]
  128. } )
  129. .then( newEditor => {
  130. const editor = newEditor;
  131. const doc = editor.document;
  132. const root = doc.getRoot();
  133. // Selection is in <p>, hence &nbsp;
  134. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  135. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  136. editor.setData( '<p>Foobar.</p>' );
  137. doc.enqueueChanges( () => {
  138. doc.batch().remove( root.getChild( 0 ) );
  139. } );
  140. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  141. editor.execute( 'undo' );
  142. expect( editor.getData() ).to.equal( '<p>Foobar.</p>' );
  143. editor.execute( 'redo' );
  144. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  145. editor.execute( 'undo' );
  146. expect( editor.getData() ).to.equal( '<p>Foobar.</p>' );
  147. } );
  148. } );
  149. } );
  150. } );