table-cell-content-post-fixer.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import TableEditing from '../../src/tableediting';
  9. import { formatTable } from './../_utils/utils';
  10. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  11. describe( 'Table cell content post-fixer', () => {
  12. let editor, model, root;
  13. beforeEach( () => {
  14. return VirtualTestEditor
  15. .create( {
  16. plugins: [ TableEditing, Paragraph, UndoEditing ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. model = editor.model;
  21. root = model.document.getRoot();
  22. } );
  23. } );
  24. afterEach( () => {
  25. editor.destroy();
  26. } );
  27. it( 'should add a paragraph to an empty table cell (on table insert)', () => {
  28. setModelData( model,
  29. '<table>' +
  30. '<tableRow>' +
  31. '<tableCell></tableCell>' +
  32. '</tableRow>' +
  33. '</table>'
  34. );
  35. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  36. '<table>' +
  37. '<tableRow>' +
  38. '<tableCell><paragraph></paragraph></tableCell>' +
  39. '</tableRow>' +
  40. '</table>'
  41. ) );
  42. } );
  43. it( 'should add a paragraph to an empty table cell (on row insert)', () => {
  44. setModelData( model,
  45. '<table>' +
  46. '<tableRow>' +
  47. '<tableCell><paragraph></paragraph></tableCell>' +
  48. '</tableRow>' +
  49. '</table>'
  50. );
  51. // Insert table row with one table cell
  52. model.change( writer => {
  53. writer.insertElement( 'tableRow', writer.createPositionAfter( root.getNodeByPath( [ 0, 0 ] ) ) );
  54. writer.insertElement( 'tableCell', writer.createPositionAt( root.getNodeByPath( [ 0, 1 ] ), 0 ) );
  55. } );
  56. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  57. '<table>' +
  58. '<tableRow>' +
  59. '<tableCell><paragraph></paragraph></tableCell>' +
  60. '</tableRow>' +
  61. '<tableRow>' +
  62. '<tableCell><paragraph></paragraph></tableCell>' +
  63. '</tableRow>' +
  64. '</table>'
  65. ) );
  66. } );
  67. it( 'should add a paragraph to an empty table cell (on table cell insert)', () => {
  68. setModelData( model,
  69. '<table>' +
  70. '<tableRow>' +
  71. '<tableCell><paragraph></paragraph></tableCell>' +
  72. '</tableRow>' +
  73. '</table>'
  74. );
  75. // Insert table row with one table cell
  76. model.change( writer => {
  77. writer.insertElement( 'tableCell', writer.createPositionAt( root.getNodeByPath( [ 0, 0 ] ), 'end' ) );
  78. } );
  79. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  80. '<table>' +
  81. '<tableRow>' +
  82. '<tableCell><paragraph></paragraph></tableCell>' +
  83. '<tableCell><paragraph></paragraph></tableCell>' +
  84. '</tableRow>' +
  85. '</table>'
  86. ) );
  87. } );
  88. it( 'should add a paragraph to an empty table cell (after remove)', () => {
  89. setModelData( model,
  90. '<table>' +
  91. '<tableRow>' +
  92. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  93. '</tableRow>' +
  94. '</table>'
  95. );
  96. // Remove paragraph from table cell.
  97. model.change( writer => {
  98. writer.remove( writer.createRangeIn( root.getNodeByPath( [ 0, 0, 0 ] ) ) );
  99. } );
  100. expect( formatTable( getModelData( model, { withoutSelection: true } ) ) ).to.equal( formatTable(
  101. '<table>' +
  102. '<tableRow>' +
  103. '<tableCell><paragraph></paragraph></tableCell>' +
  104. '</tableRow>' +
  105. '</table>'
  106. ) );
  107. } );
  108. } );