paragraph-intergration.js 7.0 KB

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