tablecell-post-fixer.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  7. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { defaultConversion, defaultSchema, formatTable, formattedViewTable, modelTable } from '../_utils/utils';
  9. import injectTableCellPostFixer from '../../src/converters/tablecell-post-fixer';
  10. import env from '@ckeditor/ckeditor5-utils/src/env';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. describe( 'TableCell post-fixer', () => {
  13. let editor, model, doc, root, viewDocument;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  17. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  18. return VirtualTestEditor.create()
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. doc = model.document;
  23. root = doc.getRoot( 'main' );
  24. viewDocument = editor.editing.view;
  25. defaultSchema( model.schema );
  26. defaultConversion( editor.conversion, true );
  27. editor.model.schema.register( 'block', {
  28. inheritAllFrom: '$block'
  29. } );
  30. editor.conversion.elementToElement( { model: 'block', view: 'div' } );
  31. model.schema.extend( '$block', { allowAttributes: 'foo' } );
  32. editor.conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  33. injectTableCellPostFixer( model, editor.editing );
  34. } );
  35. } );
  36. it( 'should rename <span> to <p> when adding more <paragraph> elements to the same table cell', () => {
  37. setModelData( model, modelTable( [ [ '00[]' ] ] ) );
  38. const table = root.getChild( 0 );
  39. model.change( writer => {
  40. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  41. const paragraph = writer.createElement( 'paragraph' );
  42. writer.insert( paragraph, nodeByPath, 'after' );
  43. writer.setSelection( nodeByPath.nextSibling, 0 );
  44. } );
  45. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  46. [ '<p>00</p><p></p>' ]
  47. ], { asWidget: true } ) );
  48. } );
  49. it( 'should rename <span> to <p> on adding other block element to the same table cell', () => {
  50. setModelData( model, modelTable( [ [ '00[]' ] ] ) );
  51. const table = root.getChild( 0 );
  52. model.change( writer => {
  53. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  54. const paragraph = writer.createElement( 'block' );
  55. writer.insert( paragraph, nodeByPath, 'after' );
  56. writer.setSelection( nodeByPath.nextSibling, 0 );
  57. } );
  58. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  59. [ '<p>00</p><div></div>' ]
  60. ], { asWidget: true } ) );
  61. } );
  62. it( 'should properly rename the same element on consecutive changes', () => {
  63. setModelData( model, modelTable( [ [ '00[]' ] ] ) );
  64. const table = root.getChild( 0 );
  65. model.change( writer => {
  66. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  67. writer.insertElement( 'paragraph', nodeByPath, 'after' );
  68. writer.setSelection( nodeByPath.nextSibling, 0 );
  69. } );
  70. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  71. [ '<p>00</p><p></p>' ]
  72. ], { asWidget: true } ) );
  73. model.change( writer => {
  74. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  75. } );
  76. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  77. [ '00' ]
  78. ], { asWidget: true } ) );
  79. } );
  80. it( 'should rename <span> to <p> when setting attribute on <paragraph>', () => {
  81. setModelData( model, modelTable( [ [ '<paragraph>00[]</paragraph>' ] ] ) );
  82. const table = root.getChild( 0 );
  83. model.change( writer => {
  84. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  85. } );
  86. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  87. [ '<p foo="bar">00</p>' ]
  88. ], { asWidget: true } ) );
  89. } );
  90. it( 'should rename <p> to <span> when removing all but one paragraph inside table cell', () => {
  91. setModelData( model, modelTable( [ [ '<paragraph>00[]</paragraph><paragraph>foo</paragraph>' ] ] ) );
  92. const table = root.getChild( 0 );
  93. model.change( writer => {
  94. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  95. } );
  96. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  97. [ '00' ]
  98. ], { asWidget: true } ) );
  99. } );
  100. it( 'should rename <p> to <span> when removing attribute from <paragraph>', () => {
  101. setModelData( model, modelTable( [ [ '<paragraph foo="bar">00[]</paragraph>' ] ] ) );
  102. const table = root.getChild( 0 );
  103. model.change( writer => {
  104. writer.removeAttribute( 'foo', table.getNodeByPath( [ 0, 0, 0 ] ) );
  105. } );
  106. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  107. [ '<span>00</span>' ]
  108. ], { asWidget: true } ) );
  109. } );
  110. it( 'should keep <p> in the view when <paragraph> attribute value is changed', () => {
  111. setModelData( model, modelTable( [ [ '<paragraph foo="bar">00[]</paragraph>' ] ] ) );
  112. const table = root.getChild( 0 );
  113. model.change( writer => {
  114. writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
  115. } );
  116. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  117. [ '<p foo="baz">00</p>' ]
  118. ], { asWidget: true } ) );
  119. } );
  120. it( 'should keep <p> in the view when <paragraph> attribute value is changed (table cell with multiple blocks)', () => {
  121. setModelData( model, modelTable( [ [ '<paragraph foo="bar">00[]</paragraph><paragraph>00</paragraph>' ] ] ) );
  122. const table = root.getChild( 0 );
  123. model.change( writer => {
  124. writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
  125. } );
  126. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  127. [ '<p foo="baz">00</p><p>00</p>' ]
  128. ], { asWidget: true } ) );
  129. } );
  130. it( 'should do nothing on rename <paragraph> to other block', () => {
  131. setModelData( model, modelTable( [ [ '00' ] ] ) );
  132. const table = root.getChild( 0 );
  133. model.change( writer => {
  134. writer.rename( table.getNodeByPath( [ 0, 0, 0 ] ), 'block' );
  135. } );
  136. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  137. [ '<div>00</div>' ]
  138. ], { asWidget: true } ) );
  139. } );
  140. it( 'should do nothing when setting attribute on block item other then <paragraph>', () => {
  141. setModelData( model, modelTable( [ [ '<block>foo</block>' ] ] ) );
  142. const table = root.getChild( 0 );
  143. model.change( writer => {
  144. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  145. } );
  146. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  147. [ '<div foo="bar">foo</div>' ]
  148. ], { asWidget: true } ) );
  149. } );
  150. } );