tablecell-post-fixer.js 9.2 KB

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