8
0

table-integration.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  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 VirtualTestEditor
  26. .create( { plugins: [ Paragraph, TableEditing, ListEditing, BlockQuoteEditing, Widget, Clipboard ] } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. clipboard = editor.plugins.get( 'Clipboard' );
  30. } );
  31. } );
  32. it( 'pastes td as p when pasting into the table', () => {
  33. setModelData( editor.model, modelTable( [ [ 'foo[]' ] ] ) );
  34. clipboard.fire( 'inputTransformation', {
  35. content: parseView( '<td>bar</td>' )
  36. } );
  37. assertEqualMarkup( getModelData( editor.model ), modelTable( [
  38. [ 'foobar[]' ]
  39. ] ) );
  40. } );
  41. it( 'pastes td as p when pasting into the p', () => {
  42. setModelData( editor.model, '<paragraph>foo[]</paragraph>' );
  43. clipboard.fire( 'inputTransformation', {
  44. content: parseView( '<td>bar</td>' )
  45. } );
  46. assertEqualMarkup( getModelData( editor.model ), '<paragraph>foobar[]</paragraph>' );
  47. } );
  48. it( 'pastes list into the td', () => {
  49. setModelData( editor.model, modelTable( [ [ '[]' ] ] ) );
  50. clipboard.fire( 'inputTransformation', {
  51. content: parseView( '<li>bar</li>' )
  52. } );
  53. assertEqualMarkup( getModelData( editor.model ), modelTable( [
  54. [ '<listItem listIndent="0" listType="bulleted">bar[]</listItem>' ]
  55. ] ) );
  56. } );
  57. it( 'pastes blockquote into the td', () => {
  58. setModelData( editor.model, modelTable( [ [ '[]' ] ] ) );
  59. clipboard.fire( 'inputTransformation', {
  60. content: parseView( '<blockquote>bar</blockquote>' )
  61. } );
  62. assertEqualMarkup( getModelData( editor.model ), modelTable( [
  63. [ '<blockQuote><paragraph>bar[]</paragraph></blockQuote>' ]
  64. ] ) );
  65. } );
  66. } );
  67. describe( 'with undo', () => {
  68. let editor, doc, root;
  69. beforeEach( () => {
  70. return VirtualTestEditor
  71. .create( { plugins: [ Paragraph, TableEditing, Widget, UndoEditing ] } )
  72. .then( newEditor => {
  73. editor = newEditor;
  74. doc = editor.model.document;
  75. root = doc.getRoot();
  76. } );
  77. } );
  78. it( 'fixing empty roots should be transparent to undo', () => {
  79. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  80. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  81. editor.data.set( viewTable( [ [ 'foo' ] ] ) );
  82. expect( editor.getData( { trim: 'none' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  83. editor.model.change( writer => {
  84. writer.remove( root.getChild( 0 ) );
  85. } );
  86. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  87. editor.execute( 'undo' );
  88. expect( editor.getData( { trim: 'none' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  89. editor.execute( 'redo' );
  90. expect( editor.getData( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
  91. editor.execute( 'undo' );
  92. expect( editor.getData( { trim: 'none' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  93. } );
  94. it( 'fixing empty roots should be transparent to undo - multiple roots', () => {
  95. const otherRoot = doc.createRoot( '$root', 'otherRoot' );
  96. editor.data.set( { main: viewTable( [ [ 'foo' ] ] ) } );
  97. editor.data.set( { otherRoot: viewTable( [ [ 'foo' ] ] ) } );
  98. editor.model.change( writer => {
  99. writer.remove( root.getChild( 0 ) );
  100. } );
  101. editor.model.change( writer => {
  102. writer.remove( otherRoot.getChild( 0 ) );
  103. } );
  104. expect( editor.data.get( { trim: 'none', rootName: 'main' } ) ).to.equal( '<p>&nbsp;</p>' );
  105. expect( editor.data.get( { trim: 'none', rootName: 'otherRoot' } ) ).to.equal( '<p>&nbsp;</p>' );
  106. editor.execute( 'undo' );
  107. expect( editor.data.get( { trim: 'none', rootName: 'main' } ) ).to.equal( '<p>&nbsp;</p>' );
  108. expect( editor.data.get( { trim: 'none', rootName: 'otherRoot' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  109. editor.execute( 'undo' );
  110. expect( editor.data.get( { trim: 'none', rootName: 'main' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  111. expect( editor.data.get( { trim: 'none', rootName: 'otherRoot' } ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  112. } );
  113. } );
  114. describe( 'other', () => {
  115. let editor;
  116. beforeEach( () => {
  117. return VirtualTestEditor
  118. .create( { plugins: [ Paragraph, TableEditing, ListEditing, BlockQuoteEditing, Widget, Typing ] } )
  119. .then( newEditor => {
  120. editor = newEditor;
  121. } );
  122. } );
  123. it( 'merges elements without throwing errors', () => {
  124. setModelData( editor.model, modelTable( [
  125. [ '<blockQuote><paragraph>Foo</paragraph></blockQuote><paragraph>[]Bar</paragraph>' ]
  126. ] ) );
  127. editor.execute( 'delete' );
  128. assertEqualMarkup( getModelData( editor.model ), modelTable( [
  129. [ '<blockQuote><paragraph>Foo[]Bar</paragraph></blockQuote>' ]
  130. ] ) );
  131. } );
  132. } );
  133. } );