table-integration.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 Widget from '@ckeditor/ckeditor5-widget/src/widget';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  8. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  9. import ListEditing from '@ckeditor/ckeditor5-list/src/listediting';
  10. import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
  11. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  12. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  13. import {
  14. getData as getModelData,
  15. setData as setModelData
  16. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  17. import { parse as parseView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  18. import TableEditing from '../src/tableediting';
  19. import { modelTable, viewTable } from './_utils/utils';
  20. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  21. describe( 'Table feature – integration', () => {
  22. describe( 'with clipboard', () => {
  23. let editor, clipboard;
  24. beforeEach( () => {
  25. return ClassicTestEditor
  26. .create( '', { plugins: [ Paragraph, TableEditing, ListEditing, BlockQuoteEditing, Widget, Clipboard ] } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. clipboard = editor.plugins.get( 'Clipboard' );
  30. } );
  31. } );
  32. afterEach( () => {
  33. editor.destroy();
  34. } );
  35. it( 'pastes td as p when pasting into the table', () => {
  36. setModelData( editor.model, modelTable( [ [ 'foo[]' ] ] ) );
  37. clipboard.fire( 'inputTransformation', {
  38. content: parseView( '<td>bar</td>' )
  39. } );
  40. assertEqualMarkup( getModelData( editor.model ), modelTable( [
  41. [ 'foobar[]' ]
  42. ] ) );
  43. } );
  44. it( 'pastes td as p when pasting into the p', () => {
  45. setModelData( editor.model, '<paragraph>foo[]</paragraph>' );
  46. clipboard.fire( 'inputTransformation', {
  47. content: parseView( '<td>bar</td>' )
  48. } );
  49. assertEqualMarkup( getModelData( editor.model ), '<paragraph>foobar[]</paragraph>' );
  50. } );
  51. it( 'pastes list into the td', () => {
  52. setModelData( editor.model, modelTable( [ [ '[]' ] ] ) );
  53. clipboard.fire( 'inputTransformation', {
  54. content: parseView( '<li>bar</li>' )
  55. } );
  56. assertEqualMarkup( getModelData( editor.model ), modelTable( [
  57. [ '<listItem listIndent="0" listType="bulleted">bar[]</listItem>' ]
  58. ] ) );
  59. } );
  60. it( 'pastes blockquote into the td', () => {
  61. setModelData( editor.model, modelTable( [ [ '[]' ] ] ) );
  62. clipboard.fire( 'inputTransformation', {
  63. content: parseView( '<blockquote>bar</blockquote>' )
  64. } );
  65. assertEqualMarkup( getModelData( editor.model ), modelTable( [
  66. [ '<blockQuote><paragraph>bar[]</paragraph></blockQuote>' ]
  67. ] ) );
  68. } );
  69. } );
  70. describe( 'with undo', () => {
  71. let editor, doc, root;
  72. beforeEach( () => {
  73. return ClassicTestEditor
  74. .create( '', { plugins: [ Paragraph, TableEditing, Widget, UndoEditing ] } )
  75. .then( newEditor => {
  76. editor = newEditor;
  77. doc = editor.model.document;
  78. root = doc.getRoot();
  79. } );
  80. } );
  81. afterEach( () => {
  82. editor.destroy();
  83. } );
  84. it( 'fixing empty roots should be transparent to undo', () => {
  85. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  86. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  87. editor.data.set( viewTable( [ [ 'foo' ] ] ) );
  88. expect( editor.getData( { trim: 'none' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  89. editor.model.change( writer => {
  90. writer.remove( root.getChild( 0 ) );
  91. } );
  92. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  93. editor.execute( 'undo' );
  94. expect( editor.getData( { trim: 'none' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  95. editor.execute( 'redo' );
  96. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  97. editor.execute( 'undo' );
  98. expect( editor.getData( { trim: 'none' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  99. } );
  100. it( 'fixing empty roots should be transparent to undo - multiple roots', () => {
  101. const otherRoot = doc.createRoot( '$root', 'otherRoot' );
  102. editor.data.set( { main: viewTable( [ [ 'foo' ] ] ) } );
  103. editor.data.set( { otherRoot: viewTable( [ [ 'foo' ] ] ) } );
  104. editor.model.change( writer => {
  105. writer.remove( root.getChild( 0 ) );
  106. } );
  107. editor.model.change( writer => {
  108. writer.remove( otherRoot.getChild( 0 ) );
  109. } );
  110. expect( editor.data.get( { trim: 'none', rootName: 'main' } ) ).to.equal( '<p>&nbsp;</p>' );
  111. expect( editor.data.get( { trim: 'none', rootName: 'otherRoot' } ) ).to.equal( '<p>&nbsp;</p>' );
  112. editor.execute( 'undo' );
  113. expect( editor.data.get( { trim: 'none', rootName: 'main' } ) ).to.equal( '<p>&nbsp;</p>' );
  114. expect( editor.data.get( { trim: 'none', rootName: 'otherRoot' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  115. editor.execute( 'undo' );
  116. expect( editor.data.get( { trim: 'none', rootName: 'main' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  117. expect( editor.data.get( { trim: 'none', rootName: 'otherRoot' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  118. } );
  119. } );
  120. describe( 'other', () => {
  121. let editor;
  122. beforeEach( () => {
  123. return ClassicTestEditor
  124. .create( '', { plugins: [ Paragraph, TableEditing, ListEditing, BlockQuoteEditing, Widget, Typing ] } )
  125. .then( newEditor => {
  126. editor = newEditor;
  127. } );
  128. } );
  129. afterEach( () => {
  130. editor.destroy();
  131. } );
  132. it( 'merges elements without throwing errors', () => {
  133. setModelData( editor.model, modelTable( [
  134. [ '<blockQuote><paragraph>Foo</paragraph></blockQuote><paragraph>[]Bar</paragraph>' ]
  135. ] ) );
  136. editor.execute( 'delete' );
  137. assertEqualMarkup( getModelData( editor.model ), modelTable( [
  138. [ '<blockQuote><paragraph>Foo[]Bar</paragraph></blockQuote>' ]
  139. ] ) );
  140. } );
  141. it( 'should not make the Model#hasContent() method return "true" when an empty table cell is selected', () => {
  142. setModelData( editor.model, '<table>' +
  143. '<tableRow>' +
  144. '[<tableCell><paragraph></paragraph></tableCell>]' +
  145. '</tableRow>' +
  146. '</table>' );
  147. expect( editor.model.hasContent( editor.model.document.selection.getFirstRange() ) ).to.be.false;
  148. } );
  149. } );
  150. } );