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

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