table-integration.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, Typing ] } )
  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. it( 'merges elements without throwing errors', () => {
  66. setModelData( editor.model, modelTable( [
  67. [ '<blockQuote><paragraph>Foo</paragraph></blockQuote><paragraph>[]Bar</paragraph>' ]
  68. ] ) );
  69. editor.execute( 'delete' );
  70. expect( formatTable( getModelData( editor.model ) ) ).to.equal( formattedModelTable( [
  71. [ '<blockQuote><paragraph>Foo[]Bar</paragraph></blockQuote>' ]
  72. ] ) );
  73. } );
  74. } );
  75. describe( 'with undo', () => {
  76. let editor, doc, root;
  77. beforeEach( () => {
  78. return VirtualTestEditor
  79. .create( { plugins: [ Paragraph, TableEditing, Widget, UndoEditing ] } )
  80. .then( newEditor => {
  81. editor = newEditor;
  82. doc = editor.model.document;
  83. root = doc.getRoot();
  84. } );
  85. } );
  86. it( 'fixing empty roots should be transparent to undo', () => {
  87. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  88. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  89. editor.data.set( viewTable( [ [ 'foo' ] ] ) );
  90. expect( editor.getData() ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  91. editor.model.change( writer => {
  92. writer.remove( root.getChild( 0 ) );
  93. } );
  94. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  95. editor.execute( 'undo' );
  96. expect( editor.getData() ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  97. editor.execute( 'redo' );
  98. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  99. editor.execute( 'undo' );
  100. expect( editor.getData() ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  101. } );
  102. it( 'fixing empty roots should be transparent to undo - multiple roots', () => {
  103. const otherRoot = doc.createRoot( '$root', 'otherRoot' );
  104. editor.data.set( viewTable( [ [ 'foo' ] ] ), 'main' );
  105. editor.data.set( viewTable( [ [ 'foo' ] ] ), 'otherRoot' );
  106. editor.model.change( writer => {
  107. writer.remove( root.getChild( 0 ) );
  108. } );
  109. editor.model.change( writer => {
  110. writer.remove( otherRoot.getChild( 0 ) );
  111. } );
  112. expect( editor.data.get( 'main' ) ).to.equal( '<p>&nbsp;</p>' );
  113. expect( editor.data.get( 'otherRoot' ) ).to.equal( '<p>&nbsp;</p>' );
  114. editor.execute( 'undo' );
  115. expect( editor.data.get( 'main' ) ).to.equal( '<p>&nbsp;</p>' );
  116. expect( editor.data.get( 'otherRoot' ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  117. editor.execute( 'undo' );
  118. expect( editor.data.get( 'main' ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  119. expect( editor.data.get( 'otherRoot' ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  120. } );
  121. } );
  122. } );