tablecell-post-fixer.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  7. import { defaultConversion, defaultSchema, formatTable, formattedViewTable, viewTable } from '../_utils/utils';
  8. import injectTableCellPostFixer from '../../src/converters/tablecell-post-fixer';
  9. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  10. import env from '@ckeditor/ckeditor5-utils/src/env';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  13. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  14. describe( 'TableCell post-fixer', () => {
  15. let editor, model, doc, root, view;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. const element = document.createElement( 'div' );
  19. document.body.appendChild( element );
  20. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  21. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  22. return ClassicTestEditor.create( element )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. doc = model.document;
  27. root = doc.getRoot( 'main' );
  28. view = editor.editing.view;
  29. defaultSchema( model.schema );
  30. defaultConversion( editor.conversion, true );
  31. editor.model.schema.register( 'block', {
  32. inheritAllFrom: '$block'
  33. } );
  34. editor.conversion.elementToElement( { model: 'block', view: 'div' } );
  35. model.schema.extend( '$block', { allowAttributes: 'foo' } );
  36. editor.conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  37. injectTableCellPostFixer( model, editor.editing );
  38. } );
  39. } );
  40. it( 'should rename <span> to <p> when adding more <paragraph> elements to the same table cell', () => {
  41. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  42. const table = root.getChild( 0 );
  43. model.change( writer => {
  44. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  45. const paragraph = writer.createElement( 'paragraph' );
  46. writer.insert( paragraph, nodeByPath, 'after' );
  47. writer.setSelection( nodeByPath.nextSibling, 0 );
  48. } );
  49. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  50. [ '<p>00</p><p></p>' ]
  51. ], { asWidget: true } ) );
  52. } );
  53. it( 'should rename <span> to <p> on adding other block element to the same table cell', () => {
  54. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  55. const table = root.getChild( 0 );
  56. model.change( writer => {
  57. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  58. const paragraph = writer.createElement( 'block' );
  59. writer.insert( paragraph, nodeByPath, 'after' );
  60. writer.setSelection( nodeByPath.nextSibling, 0 );
  61. } );
  62. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  63. [ '<p>00</p><div></div>' ]
  64. ], { asWidget: true } ) );
  65. } );
  66. it( 'should properly rename the same element on consecutive changes', () => {
  67. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  68. const table = root.getChild( 0 );
  69. model.change( writer => {
  70. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  71. writer.insertElement( 'paragraph', nodeByPath, 'after' );
  72. writer.setSelection( nodeByPath.nextSibling, 0 );
  73. } );
  74. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  75. [ '<p>00</p><p></p>' ]
  76. ], { asWidget: true } ) );
  77. model.change( writer => {
  78. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  79. } );
  80. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  81. [ '00' ]
  82. ], { asWidget: true } ) );
  83. } );
  84. it( 'should rename <span> to <p> when setting attribute on <paragraph>', () => {
  85. editor.setData( '<table><tr><td><p>00</p></td></tr></table>' );
  86. const table = root.getChild( 0 );
  87. model.change( writer => {
  88. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  89. } );
  90. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  91. [ '<p foo="bar">00</p>' ]
  92. ], { asWidget: true } ) );
  93. } );
  94. it( 'should rename <p> to <span> when removing all but one paragraph inside table cell', () => {
  95. editor.setData( viewTable( [ [ '<p>00</p><p>foo</p>' ] ] ) );
  96. const table = root.getChild( 0 );
  97. model.change( writer => {
  98. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  99. } );
  100. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  101. [ '00' ]
  102. ], { asWidget: true } ) );
  103. } );
  104. it( 'should rename <p> to <span> when removing attribute from <paragraph>', () => {
  105. editor.setData( '<table><tr><td><p foo="bar">00</p></td></tr></table>' );
  106. const table = root.getChild( 0 );
  107. model.change( writer => {
  108. writer.removeAttribute( 'foo', table.getNodeByPath( [ 0, 0, 0 ] ) );
  109. } );
  110. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  111. [ '<span>00</span>' ]
  112. ], { asWidget: true } ) );
  113. } );
  114. it( 'should keep <p> in the view when <paragraph> attribute value is changed', () => {
  115. editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
  116. const table = root.getChild( 0 );
  117. model.change( writer => {
  118. writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
  119. } );
  120. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  121. [ '<p foo="baz">00</p>' ]
  122. ], { asWidget: true } ) );
  123. } );
  124. it( 'should keep <p> in the view when <paragraph> attribute value is changed (table cell with multiple blocks)', () => {
  125. editor.setData( viewTable( [ [ '<p foo="bar">00</p><p>00</p>' ] ] ) );
  126. const table = root.getChild( 0 );
  127. model.change( writer => {
  128. writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
  129. } );
  130. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  131. [ '<p foo="baz">00</p><p>00</p>' ]
  132. ], { asWidget: true } ) );
  133. } );
  134. it( 'should do nothing on rename <paragraph> to other block', () => {
  135. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  136. const table = root.getChild( 0 );
  137. model.change( writer => {
  138. writer.rename( table.getNodeByPath( [ 0, 0, 0 ] ), 'block' );
  139. } );
  140. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  141. [ '<div>00</div>' ]
  142. ], { asWidget: true } ) );
  143. } );
  144. it( 'should do nothing when setting attribute on block item other then <paragraph>', () => {
  145. editor.setData( viewTable( [ [ '<div>foo</div>' ] ] ) );
  146. const table = root.getChild( 0 );
  147. model.change( writer => {
  148. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  149. } );
  150. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  151. [ '<div foo="bar">foo</div>' ]
  152. ], { asWidget: true } ) );
  153. } );
  154. it( 'should not crash when view.change() block was called in model.change()', () => {
  155. editor.setData( viewTable( [ [ '<p>foobar</p>' ] ] ) );
  156. const table = root.getChild( 0 );
  157. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) )
  158. .to.equal( formattedViewTable( [ [ 'foobar' ] ], { asWidget: true } ) );
  159. expect( () => {
  160. model.change( writer => {
  161. const tableCell = table.getNodeByPath( [ 0, 0 ] );
  162. writer.insertElement( 'paragraph', null, Position.createAt( tableCell, 'end' ) );
  163. writer.setSelection( Range.createIn( tableCell ) );
  164. // Do some change in the view while inside model change.
  165. editor.editing.view.change( writer => {
  166. writer.addClass( 'foo', editor.editing.mapper.toViewElement( tableCell ) );
  167. } );
  168. } );
  169. } ).to.not.throw();
  170. expect( formatTable( getViewData( view ) ) ).to.equal( formattedViewTable( [
  171. [ { class: 'foo', contents: '<p>{foobar</p><p>]</p>' } ]
  172. ], { asWidget: true } ) );
  173. } );
  174. it( 'should keep <p> in the view when <paragraph> attribute value is changed (table cell with multiple blocks)', () => {
  175. editor.setData( viewTable( [ [ '<p>00</p><p>00</p>' ] ] ) );
  176. const table = root.getChild( 0 );
  177. model.change( writer => {
  178. writer.remove( Range.createOn( table.getNodeByPath( [ 0, 0, 1 ] ) ) );
  179. } );
  180. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  181. [ '<span>00</span>' ]
  182. ], { asWidget: true } ) );
  183. } );
  184. it( 'should update view selection after deleting content', () => {
  185. editor.setData( viewTable( [ [ '<p>foo</p><p>bar</p>' ] ] ) );
  186. const tableCell = root.getNodeByPath( [ 0, 0, 0 ] );
  187. // Replace table cell contents with paragraph - as model.deleteContent() does.
  188. model.change( writer => {
  189. writer.remove( Range.createIn( tableCell ) );
  190. const paragraph = writer.createElement( 'paragraph' );
  191. writer.insert( paragraph, Position.createAt( tableCell ) );
  192. // Set selection to newly created paragraph.
  193. writer.setSelection( paragraph, 0 );
  194. } );
  195. const viewRange = view.document.selection.getFirstRange();
  196. // Trying to map view selection to DOM range shouldn't throw after post-fixer will fix inserted <p> to <span>.
  197. expect( () => view.domConverter.viewRangeToDom( viewRange ) ).to.not.throw();
  198. } );
  199. } );