paragraph-intergration.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Paragraph from '../src/paragraph';
  6. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  7. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  8. import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
  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 clipboard = editor.plugins.get( 'Clipboard' );
  23. setModelData( editor.model, '<paragraph>[]</paragraph>' );
  24. clipboard.fire( 'inputTransformation', {
  25. content: parseView( '<h1>foo</h1><h2>bar</h2><p>bom</p>' )
  26. } );
  27. expect( getModelData( editor.model ) ).to.equal(
  28. '<paragraph>foo</paragraph><paragraph>bar</paragraph><paragraph>bom[]</paragraph>'
  29. );
  30. } );
  31. } );
  32. // Explainer: the heading feature is configured to handle h1-h4 elements, so h5 has no handler.
  33. it( 'pastes h1+h2+h5+p as h1+h2+p+p when heading feature is present', () => {
  34. return VirtualTestEditor
  35. .create( { plugins: [ Paragraph, Clipboard, HeadingEditing ] } )
  36. .then( newEditor => {
  37. const editor = newEditor;
  38. const clipboard = editor.plugins.get( 'Clipboard' );
  39. setModelData( editor.model, '<paragraph>[]</paragraph>' );
  40. clipboard.fire( 'inputTransformation', {
  41. content: parseView( '<h1>foo</h1><h2>bar</h2><h5>baz</h5><p>bom</p>' )
  42. } );
  43. expect( getModelData( editor.model ) ).to.equal(
  44. '<heading1>foo</heading1><heading1>bar</heading1><paragraph>baz</paragraph><paragraph>bom[]</paragraph>'
  45. );
  46. } );
  47. } );
  48. it( 'pastes ul>li+li as p+p when list feature is not present', () => {
  49. return VirtualTestEditor
  50. .create( { plugins: [ Paragraph, Clipboard ] } )
  51. .then( newEditor => {
  52. const editor = newEditor;
  53. const clipboard = editor.plugins.get( 'Clipboard' );
  54. setModelData( editor.model, '<paragraph>[]</paragraph>' );
  55. clipboard.fire( 'inputTransformation', {
  56. content: parseView( '<ul><li>foo</li><li>bar</li></ul>' )
  57. } );
  58. expect( getModelData( editor.model ) ).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
  65. .create( { plugins: [ Paragraph, Clipboard, HeadingEditing ] } )
  66. .then( newEditor => {
  67. const editor = newEditor;
  68. const clipboard = editor.plugins.get( 'Clipboard' );
  69. setModelData( editor.model, '<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( editor.model ) ).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
  83. .create( { plugins: [ Paragraph, Clipboard ] } )
  84. .then( newEditor => {
  85. const editor = newEditor;
  86. const clipboard = editor.plugins.get( 'Clipboard' );
  87. setModelData( editor.model, '<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( editor.model ) ).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
  101. .create( { plugins: [ Paragraph, Clipboard ] } )
  102. .then( newEditor => {
  103. const editor = newEditor;
  104. const clipboard = editor.plugins.get( 'Clipboard' );
  105. setModelData( editor.model, '<paragraph>[]</paragraph>' );
  106. clipboard.fire( 'inputTransformation', {
  107. content: parseView( '<ul><li><p>a</p>b</li></ul>' )
  108. } );
  109. expect( getModelData( editor.model ) ).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
  119. .create( { plugins: [ Paragraph, UndoEditing ] } )
  120. .then( newEditor => {
  121. const editor = newEditor;
  122. const doc = editor.model.document;
  123. const root = doc.getRoot();
  124. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  125. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  126. editor.setData( '<p>Foobar.</p>' );
  127. editor.model.change( writer => {
  128. writer.remove( root.getChild( 0 ) );
  129. } );
  130. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  131. editor.execute( 'undo' );
  132. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>Foobar.</p>' );
  133. editor.execute( 'redo' );
  134. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  135. editor.execute( 'undo' );
  136. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>Foobar.</p>' );
  137. } );
  138. } );
  139. it( 'fixing empty roots should be transparent to undo - multiple roots', () => {
  140. return VirtualTestEditor
  141. .create( { plugins: [ Paragraph, UndoEditing ] } )
  142. .then( newEditor => {
  143. const editor = newEditor;
  144. const doc = editor.model.document;
  145. const root = doc.getRoot();
  146. const otherRoot = doc.createRoot( '$root', 'otherRoot' );
  147. editor.data.set( { main: '<p>Foobar.</p>' } );
  148. editor.data.set( { otherRoot: '<p>Foobar.</p>' } );
  149. editor.model.change( writer => {
  150. writer.remove( root.getChild( 0 ) );
  151. } );
  152. editor.model.change( writer => {
  153. writer.remove( otherRoot.getChild( 0 ) );
  154. } );
  155. expect( editor.data.get( { rootName: 'main', trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  156. expect( editor.data.get( { rootName: 'otherRoot', trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  157. editor.execute( 'undo' );
  158. expect( editor.data.get( { rootName: 'main', trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  159. expect( editor.data.get( { rootName: 'otherRoot', trim: 'none' } ) ).to.equal( '<p>Foobar.</p>' );
  160. editor.execute( 'undo' );
  161. expect( editor.data.get( { rootName: 'main', trim: 'none' } ) ).to.equal( '<p>Foobar.</p>' );
  162. expect( editor.data.get( { rootName: 'otherRoot', trim: 'none' } ) ).to.equal( '<p>Foobar.</p>' );
  163. } );
  164. } );
  165. } );
  166. } );