paragraph-intergration.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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( { plugins: [ Paragraph, Clipboard ] } )
  19. .then( newEditor => {
  20. const editor = newEditor;
  21. const doc = editor.document;
  22. const clipboard = editor.plugins.get( 'Clipboard' );
  23. setModelData( doc, '<paragraph>[]</paragraph>' );
  24. clipboard.fire( 'inputTransformation', {
  25. content: parseView( '<h1>foo</h1><h2>bar</h2><p>bom</p>' )
  26. } );
  27. expect( getModelData( doc ) ).to.equal(
  28. '<paragraph>foo</paragraph><paragraph>bar</paragraph><paragraph>bom[]</paragraph>'
  29. );
  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( { plugins: [ Paragraph, Clipboard, HeadingEngine ] } )
  35. .then( newEditor => {
  36. const editor = newEditor;
  37. const doc = editor.document;
  38. const clipboard = editor.plugins.get( 'Clipboard' );
  39. setModelData( doc, '<paragraph>[]</paragraph>' );
  40. clipboard.fire( 'inputTransformation', {
  41. content: parseView( '<h1>foo</h1><h2>bar</h2><p>bom</p>' )
  42. } );
  43. expect( getModelData( doc ) ).to.equal(
  44. '<paragraph>foo</paragraph><heading1>bar</heading1><paragraph>bom[]</paragraph>'
  45. );
  46. } );
  47. } );
  48. it( 'pastes ul>li+li as p+p when list feature is not present', () => {
  49. return VirtualTestEditor.create( { plugins: [ Paragraph, Clipboard ] } )
  50. .then( newEditor => {
  51. const editor = newEditor;
  52. const doc = editor.document;
  53. const clipboard = editor.plugins.get( 'Clipboard' );
  54. setModelData( doc, '<paragraph>[]</paragraph>' );
  55. clipboard.fire( 'inputTransformation', {
  56. content: parseView( '<ul><li>foo</li><li>bar</li></ul>' )
  57. } );
  58. expect( getModelData( doc ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar[]</paragraph>' );
  59. } );
  60. } );
  61. // Check whether the paragraph feature doesn't breaking pasting such content by trying to
  62. // handle the li element.
  63. it( 'pastes ul>li>h2+h3+p as h2+h3+p when heading feature is present', () => {
  64. return VirtualTestEditor.create( { plugins: [ Paragraph, Clipboard, HeadingEngine ] } )
  65. .then( newEditor => {
  66. const editor = newEditor;
  67. const doc = editor.document;
  68. const clipboard = editor.plugins.get( 'Clipboard' );
  69. setModelData( doc, '<paragraph>[]</paragraph>' );
  70. clipboard.fire( 'inputTransformation', {
  71. content: parseView( '<ul><li>x</li><li><h2>foo</h2><h3>bar</h3><p>bom</p></li><li>x</li></ul>' )
  72. } );
  73. expect( getModelData( doc ) ).to.equal(
  74. '<paragraph>x</paragraph>' +
  75. '<heading1>foo</heading1><heading2>bar</heading2><paragraph>bom</paragraph>' +
  76. '<paragraph>x[]</paragraph>'
  77. );
  78. } );
  79. } );
  80. // See 'should convert ul>li>ul>li+li (in clipboard holder)' in clipboard.js.
  81. it( 'pastes ul>li>ul>li+li', () => {
  82. return VirtualTestEditor.create( { plugins: [ Paragraph, Clipboard ] } )
  83. .then( newEditor => {
  84. const editor = newEditor;
  85. const doc = editor.document;
  86. const clipboard = editor.plugins.get( 'Clipboard' );
  87. setModelData( doc, '<paragraph>[]</paragraph>' );
  88. clipboard.fire( 'inputTransformation', {
  89. content: parseView( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' )
  90. } );
  91. expect( getModelData( doc ) ).to.equal(
  92. '<paragraph>a</paragraph>' +
  93. '<paragraph>b</paragraph>' +
  94. '<paragraph>c[]</paragraph>'
  95. );
  96. } );
  97. } );
  98. // See 'should convert ul>li>p,text (in clipboard holder)' in clipboard.js.
  99. it( 'pastes ul>li>p,text', () => {
  100. return VirtualTestEditor.create( { plugins: [ Paragraph, Clipboard ] } )
  101. .then( newEditor => {
  102. const editor = newEditor;
  103. const doc = editor.document;
  104. const clipboard = editor.plugins.get( 'Clipboard' );
  105. setModelData( doc, '<paragraph>[]</paragraph>' );
  106. clipboard.fire( 'inputTransformation', {
  107. content: parseView( '<ul><li><p>a</p>b</li></ul>' )
  108. } );
  109. expect( getModelData( doc ) ).to.equal(
  110. '<paragraph>a</paragraph>' +
  111. '<paragraph>b[]</paragraph>'
  112. );
  113. } );
  114. } );
  115. } );
  116. describe( 'with undo', () => {
  117. it( 'fixing empty roots should be transparent to undo', () => {
  118. return VirtualTestEditor.create( { plugins: [ Paragraph, UndoEngine ] } )
  119. .then( newEditor => {
  120. const editor = newEditor;
  121. const doc = editor.document;
  122. const root = doc.getRoot();
  123. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  124. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  125. editor.setData( '<p>Foobar.</p>' );
  126. doc.enqueueChanges( () => {
  127. doc.batch().remove( root.getChild( 0 ) );
  128. } );
  129. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  130. editor.execute( 'undo' );
  131. expect( editor.getData() ).to.equal( '<p>Foobar.</p>' );
  132. editor.execute( 'redo' );
  133. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  134. editor.execute( 'undo' );
  135. expect( editor.getData() ).to.equal( '<p>Foobar.</p>' );
  136. } );
  137. } );
  138. it( 'fixing empty roots should be transparent to undo - multiple roots', () => {
  139. return VirtualTestEditor.create( { plugins: [ Paragraph, UndoEngine ] } )
  140. .then( newEditor => {
  141. const editor = newEditor;
  142. const doc = editor.document;
  143. const root = doc.getRoot();
  144. const otherRoot = doc.createRoot( '$root', 'otherRoot' );
  145. editor.editing.createRoot( 'div', 'otherRoot' );
  146. editor.data.set( '<p>Foobar.</p>', 'main' );
  147. editor.data.set( '<p>Foobar.</p>', 'otherRoot' );
  148. doc.enqueueChanges( () => {
  149. doc.batch().remove( root.getChild( 0 ) );
  150. doc.batch().remove( otherRoot.getChild( 0 ) );
  151. } );
  152. expect( editor.data.get( 'main' ) ).to.equal( '<p>&nbsp;</p>' );
  153. expect( editor.data.get( 'otherRoot' ) ).to.equal( '<p>&nbsp;</p>' );
  154. editor.execute( 'undo' );
  155. expect( editor.data.get( 'main' ) ).to.equal( '<p>&nbsp;</p>' );
  156. expect( editor.data.get( 'otherRoot' ) ).to.equal( '<p>Foobar.</p>' );
  157. editor.execute( 'undo' );
  158. expect( editor.data.get( 'main' ) ).to.equal( '<p>Foobar.</p>' );
  159. expect( editor.data.get( 'otherRoot' ) ).to.equal( '<p>Foobar.</p>' );
  160. } );
  161. } );
  162. } );
  163. } );