table-integration.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  12. import {
  13. getData as getModelData,
  14. setData as setModelData
  15. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import { parse as parseView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  17. import TableEditing from '../src/tableediting';
  18. import { formatTable, formattedModelTable, modelTable, viewTable } from './_utils/utils';
  19. describe( 'Table feature – integration', () => {
  20. describe( 'with clipboard', () => {
  21. let editor, clipboard;
  22. beforeEach( () => {
  23. return VirtualTestEditor
  24. .create( { plugins: [ Paragraph, TableEditing, ListEditing, BlockQuoteEditing, Widget, Clipboard ] } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. clipboard = editor.plugins.get( 'Clipboard' );
  28. } );
  29. } );
  30. it( 'pastes td as p when pasting into the table', () => {
  31. setModelData( editor.model, modelTable( [ [ 'foo[]' ] ] ) );
  32. clipboard.fire( 'inputTransformation', {
  33. content: parseView( '<td>bar</td>' )
  34. } );
  35. expect( formatTable( getModelData( editor.model ) ) ).to.equal( formattedModelTable( [
  36. [ 'foobar[]' ]
  37. ] ) );
  38. } );
  39. it( 'pastes td as p when pasting into the p', () => {
  40. setModelData( editor.model, '<paragraph>foo[]</paragraph>' );
  41. clipboard.fire( 'inputTransformation', {
  42. content: parseView( '<td>bar</td>' )
  43. } );
  44. expect( formatTable( getModelData( editor.model ) ) ).to.equal( '<paragraph>foobar[]</paragraph>' );
  45. } );
  46. it( 'pastes list into the td', () => {
  47. setModelData( editor.model, modelTable( [ [ '[]' ] ] ) );
  48. clipboard.fire( 'inputTransformation', {
  49. content: parseView( '<li>bar</li>' )
  50. } );
  51. expect( formatTable( getModelData( editor.model ) ) ).to.equal( formattedModelTable( [
  52. [ '<listItem listIndent="0" listType="bulleted">bar[]</listItem>' ]
  53. ] ) );
  54. } );
  55. it( 'pastes blockquote into the td', () => {
  56. setModelData( editor.model, modelTable( [ [ '[]' ] ] ) );
  57. clipboard.fire( 'inputTransformation', {
  58. content: parseView( '<blockquote>bar</blockquote>' )
  59. } );
  60. expect( formatTable( getModelData( editor.model ) ) ).to.equal( formattedModelTable( [
  61. [ '<blockQuote><paragraph>bar[]</paragraph></blockQuote>' ]
  62. ] ) );
  63. } );
  64. } );
  65. describe( 'with undo', () => {
  66. let editor, doc, root;
  67. beforeEach( () => {
  68. return VirtualTestEditor
  69. .create( { plugins: [ Paragraph, TableEditing, Widget, UndoEditing ] } )
  70. .then( newEditor => {
  71. editor = newEditor;
  72. doc = editor.model.document;
  73. root = doc.getRoot();
  74. } );
  75. } );
  76. it( 'fixing empty roots should be transparent to undo', () => {
  77. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  78. expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false;
  79. editor.data.set( viewTable( [ [ 'foo' ] ] ) );
  80. expect( editor.getData() ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  81. editor.model.change( writer => {
  82. writer.remove( root.getChild( 0 ) );
  83. } );
  84. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  85. editor.execute( 'undo' );
  86. expect( editor.getData() ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  87. editor.execute( 'redo' );
  88. expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
  89. editor.execute( 'undo' );
  90. expect( editor.getData() ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  91. } );
  92. it( 'fixing empty roots should be transparent to undo - multiple roots', () => {
  93. const otherRoot = doc.createRoot( '$root', 'otherRoot' );
  94. editor.data.set( viewTable( [ [ 'foo' ] ] ), 'main' );
  95. editor.data.set( viewTable( [ [ 'foo' ] ] ), 'otherRoot' );
  96. editor.model.change( writer => {
  97. writer.remove( root.getChild( 0 ) );
  98. } );
  99. editor.model.change( writer => {
  100. writer.remove( otherRoot.getChild( 0 ) );
  101. } );
  102. expect( editor.data.get( 'main' ) ).to.equal( '<p>&nbsp;</p>' );
  103. expect( editor.data.get( 'otherRoot' ) ).to.equal( '<p>&nbsp;</p>' );
  104. editor.execute( 'undo' );
  105. expect( editor.data.get( 'main' ) ).to.equal( '<p>&nbsp;</p>' );
  106. expect( editor.data.get( 'otherRoot' ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  107. editor.execute( 'undo' );
  108. expect( editor.data.get( 'main' ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  109. expect( editor.data.get( 'otherRoot' ) ).to.equal( viewTable( [ [ 'foo' ] ] ) );
  110. } );
  111. } );
  112. } );