table-cell-refresh-post-fixer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 injectTableCellRefreshPostFixer from '../../src/converters/table-cell-refresh-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. import Delete from '@ckeditor/ckeditor5-typing/src/delete';
  13. describe( 'Table cell refresh post-fixer', () => {
  14. let editor, model, doc, root, view, refreshItemSpy, element;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. element = document.createElement( 'div' );
  18. document.body.appendChild( element );
  19. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  20. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  21. return ClassicTestEditor.create( element, { extraPlugins: [ Delete ] } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. model = editor.model;
  25. doc = model.document;
  26. root = doc.getRoot( 'main' );
  27. view = editor.editing.view;
  28. defaultSchema( model.schema );
  29. defaultConversion( editor.conversion, true );
  30. editor.model.schema.register( 'block', {
  31. inheritAllFrom: '$block'
  32. } );
  33. editor.conversion.elementToElement( { model: 'block', view: 'div' } );
  34. model.schema.extend( '$block', { allowAttributes: [ 'foo', 'bar' ] } );
  35. editor.conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  36. editor.conversion.attributeToAttribute( { model: 'bar', view: 'bar' } );
  37. injectTableCellRefreshPostFixer( model );
  38. refreshItemSpy = sinon.spy( model.document.differ, 'refreshItem' );
  39. } );
  40. } );
  41. afterEach( () => {
  42. element.remove();
  43. return editor.destroy();
  44. } );
  45. it( 'should rename <span> to <p> when adding more <paragraph> elements to the same table cell', () => {
  46. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  47. const table = root.getChild( 0 );
  48. model.change( writer => {
  49. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  50. const paragraph = writer.createElement( 'paragraph' );
  51. writer.insert( paragraph, nodeByPath, 'after' );
  52. writer.setSelection( nodeByPath.nextSibling, 0 );
  53. } );
  54. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  55. [ '<p>00</p><p></p>' ]
  56. ], { asWidget: true } ) );
  57. sinon.assert.calledOnce( refreshItemSpy );
  58. } );
  59. it( 'should rename <span> to <p> on adding other block element to the same table cell', () => {
  60. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  61. const table = root.getChild( 0 );
  62. model.change( writer => {
  63. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  64. const paragraph = writer.createElement( 'block' );
  65. writer.insert( paragraph, nodeByPath, 'after' );
  66. writer.setSelection( nodeByPath.nextSibling, 0 );
  67. } );
  68. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  69. [ '<p>00</p><div></div>' ]
  70. ], { asWidget: true } ) );
  71. sinon.assert.calledOnce( refreshItemSpy );
  72. } );
  73. it( 'should properly rename the same element on consecutive changes', () => {
  74. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  75. const table = root.getChild( 0 );
  76. model.change( writer => {
  77. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  78. writer.insertElement( 'paragraph', nodeByPath, 'after' );
  79. writer.setSelection( nodeByPath.nextSibling, 0 );
  80. } );
  81. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  82. [ '<p>00</p><p></p>' ]
  83. ], { asWidget: true } ) );
  84. sinon.assert.calledOnce( refreshItemSpy );
  85. model.change( writer => {
  86. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  87. } );
  88. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  89. [ '00' ]
  90. ], { asWidget: true } ) );
  91. sinon.assert.calledTwice( refreshItemSpy );
  92. } );
  93. it( 'should rename <span> to <p> when setting attribute on <paragraph>', () => {
  94. editor.setData( '<table><tr><td><p>00</p></td></tr></table>' );
  95. const table = root.getChild( 0 );
  96. model.change( writer => {
  97. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  98. } );
  99. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  100. [ '<p foo="bar">00</p>' ]
  101. ], { asWidget: true } ) );
  102. sinon.assert.calledOnce( refreshItemSpy );
  103. } );
  104. it( 'should rename <p> to <span> when removing all but one paragraph inside table cell', () => {
  105. editor.setData( viewTable( [ [ '<p>00</p><p>foo</p>' ] ] ) );
  106. const table = root.getChild( 0 );
  107. model.change( writer => {
  108. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  109. } );
  110. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  111. [ '00' ]
  112. ], { asWidget: true } ) );
  113. sinon.assert.calledOnce( refreshItemSpy );
  114. } );
  115. it( 'should rename <p> to <span> when removing attribute from <paragraph>', () => {
  116. editor.setData( '<table><tr><td><p foo="bar">00</p></td></tr></table>' );
  117. const table = root.getChild( 0 );
  118. model.change( writer => {
  119. writer.removeAttribute( 'foo', table.getNodeByPath( [ 0, 0, 0 ] ) );
  120. } );
  121. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  122. [ '<span>00</span>' ]
  123. ], { asWidget: true } ) );
  124. sinon.assert.calledOnce( refreshItemSpy );
  125. } );
  126. it( 'should keep <p> in the view when <paragraph> attribute value is changed', () => {
  127. editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
  128. const table = root.getChild( 0 );
  129. model.change( writer => {
  130. writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
  131. } );
  132. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  133. [ '<p foo="baz">00</p>' ]
  134. ], { asWidget: true } ) );
  135. // False positive: should not be called.
  136. sinon.assert.calledOnce( refreshItemSpy );
  137. } );
  138. it( 'should keep <p> in the view when adding another attribute to a <paragraph> with other attributes', () => {
  139. editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
  140. const table = root.getChild( 0 );
  141. model.change( writer => {
  142. writer.setAttribute( 'bar', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  143. } );
  144. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  145. [ '<p bar="bar" foo="bar">00</p>' ]
  146. ], { asWidget: true } ) );
  147. // False positive
  148. sinon.assert.notCalled( refreshItemSpy );
  149. } );
  150. it( 'should keep <p> in the view when adding another attribute to a <paragraph> and removing attribute that is already set', () => {
  151. editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
  152. const table = root.getChild( 0 );
  153. model.change( writer => {
  154. writer.setAttribute( 'bar', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  155. writer.removeAttribute( 'foo', table.getNodeByPath( [ 0, 0, 0 ] ) );
  156. } );
  157. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  158. [ '<p bar="bar">00</p>' ]
  159. ], { asWidget: true } ) );
  160. // False positive: should not be called.
  161. sinon.assert.calledOnce( refreshItemSpy );
  162. } );
  163. it( 'should keep <p> in the view when <paragraph> attribute value is changed (table cell with multiple blocks)', () => {
  164. editor.setData( viewTable( [ [ '<p foo="bar">00</p><p>00</p>' ] ] ) );
  165. const table = root.getChild( 0 );
  166. model.change( writer => {
  167. writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
  168. } );
  169. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  170. [ '<p foo="baz">00</p><p>00</p>' ]
  171. ], { asWidget: true } ) );
  172. sinon.assert.notCalled( refreshItemSpy );
  173. } );
  174. it( 'should do nothing on rename <paragraph> to other block', () => {
  175. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  176. const table = root.getChild( 0 );
  177. model.change( writer => {
  178. writer.rename( table.getNodeByPath( [ 0, 0, 0 ] ), 'block' );
  179. } );
  180. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  181. [ '<div>00</div>' ]
  182. ], { asWidget: true } ) );
  183. sinon.assert.notCalled( refreshItemSpy );
  184. } );
  185. it( 'should do nothing when setting attribute on block item other then <paragraph>', () => {
  186. editor.setData( viewTable( [ [ '<div>foo</div>' ] ] ) );
  187. const table = root.getChild( 0 );
  188. model.change( writer => {
  189. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  190. } );
  191. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  192. [ '<div foo="bar">foo</div>' ]
  193. ], { asWidget: true } ) );
  194. sinon.assert.notCalled( refreshItemSpy );
  195. } );
  196. it( 'should rename <p> in to <span> when removing <paragraph> (table cell with 2 paragraphs)', () => {
  197. editor.setData( viewTable( [ [ '<p>00</p><p>00</p>' ] ] ) );
  198. const table = root.getChild( 0 );
  199. model.change( writer => {
  200. writer.remove( writer.createRangeOn( table.getNodeByPath( [ 0, 0, 1 ] ) ) );
  201. } );
  202. expect( formatTable( getViewData( view, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  203. [ '<span>00</span>' ]
  204. ], { asWidget: true } ) );
  205. sinon.assert.calledOnce( refreshItemSpy );
  206. } );
  207. it( 'should update view selection after deleting content', () => {
  208. editor.setData( viewTable( [ [ '<p>foo</p><p>bar</p>' ] ] ) );
  209. const tableCell = root.getNodeByPath( [ 0, 0, 0 ] );
  210. // Replace table cell contents with paragraph - as model.deleteContent() does.
  211. model.change( writer => {
  212. writer.remove( writer.createRangeIn( tableCell ) );
  213. const paragraph = writer.createElement( 'paragraph' );
  214. writer.insert( paragraph, writer.createPositionAt( tableCell, 0 ) );
  215. // Set selection to newly created paragraph.
  216. writer.setSelection( paragraph, 0 );
  217. } );
  218. const viewRange = view.document.selection.getFirstRange();
  219. // Trying to map view selection to DOM range shouldn't throw after post-fixer will fix inserted <p> to <span>.
  220. expect( () => view.domConverter.viewRangeToDom( viewRange ) ).to.not.throw();
  221. } );
  222. it( 'should not update view selection after other feature set selection', () => {
  223. editor.model.schema.register( 'widget', {
  224. isObject: true,
  225. isBlock: true,
  226. allowWhere: '$block'
  227. } );
  228. editor.conversion.elementToElement( {
  229. model: 'widget',
  230. view: 'widget'
  231. } );
  232. editor.setData( viewTable( [ [ '<p>foo[]</p>' ] ] ) );
  233. const spy = sinon.spy();
  234. view.document.selection.on( 'change', spy );
  235. // Insert a widget in table cell and select it.
  236. model.change( writer => {
  237. const widgetElement = writer.createElement( 'widget' );
  238. const tableCell = root.getNodeByPath( [ 0, 0, 0, 0 ] );
  239. writer.insert( widgetElement, writer.createPositionAfter( tableCell ) );
  240. // Update the selection so it will be set on the widget and not in renamed paragraph.
  241. writer.setSelection( widgetElement, 'on' );
  242. } );
  243. // View selection should be updated only twice - will be set to null and then to widget.
  244. // If called thrice the selection post fixer for table cell was also called.
  245. sinon.assert.calledTwice( spy );
  246. } );
  247. // https://github.com/ckeditor/ckeditor5-table/issues/191.
  248. it( 'should not fire (and crash) for removed view elements', () => {
  249. editor.setData( viewTable( [ [ '<p>foo</p>' ] ] ) );
  250. const p = root.getNodeByPath( [ 0, 0, 0, 0 ] );
  251. // Replace table cell contents with paragraph - as model.deleteContent() does.
  252. model.change( writer => {
  253. writer.setSelection( writer.createRangeIn( root ) );
  254. editor.execute( 'delete' ); // For some reason it didn't crash with `writer.remove()`.
  255. writer.setAttribute( 'foo', 'bar', p );
  256. } );
  257. // Trying to map view selection to DOM range shouldn't throw after post-fixer will fix inserted <p> to <span>.
  258. expect( editor.getData() ).to.equal( '' );
  259. } );
  260. } );