table-integration.js 5.5 KB

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