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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Delete from '@ckeditor/ckeditor5-typing/src/delete';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  10. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. import TableEditing from '../../src/tableediting';
  13. import { viewTable } from '../_utils/utils';
  14. describe( 'Table cell refresh post-fixer', () => {
  15. let editor, model, doc, root, view, refreshItemSpy, element;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. element = document.createElement( 'div' );
  19. document.body.appendChild( element );
  20. return ClassicTestEditor.create( element, { plugins: [ Paragraph, TableEditing, Delete ] } )
  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. editor.model.schema.register( 'block', {
  28. inheritAllFrom: '$block'
  29. } );
  30. editor.conversion.elementToElement( { model: 'block', view: 'div' } );
  31. model.schema.extend( '$block', { allowAttributes: [ 'foo', 'bar' ] } );
  32. editor.conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  33. editor.conversion.attributeToAttribute( { model: 'bar', view: 'bar' } );
  34. refreshItemSpy = sinon.spy( model.document.differ, 'refreshItem' );
  35. } );
  36. } );
  37. afterEach( () => {
  38. element.remove();
  39. return editor.destroy();
  40. } );
  41. it( 'should rename <span> to <p> when adding <paragraph> element to the same table cell', () => {
  42. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  43. const table = root.getChild( 0 );
  44. model.change( writer => {
  45. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  46. const paragraph = writer.createElement( 'paragraph' );
  47. writer.insert( paragraph, nodeByPath, 'after' );
  48. writer.setSelection( nodeByPath.nextSibling, 0 );
  49. } );
  50. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  51. [ '<p>00</p><p></p>' ]
  52. ], { asWidget: true } ) );
  53. sinon.assert.calledOnce( refreshItemSpy );
  54. } );
  55. it( 'should rename <span> to <p> when adding more <paragraph> elements to the same table cell', () => {
  56. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  57. const table = root.getChild( 0 );
  58. model.change( writer => {
  59. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  60. const paragraph1 = writer.createElement( 'paragraph' );
  61. const paragraph2 = writer.createElement( 'paragraph' );
  62. writer.insert( paragraph1, nodeByPath, 'after' );
  63. writer.insert( paragraph2, nodeByPath, 'after' );
  64. writer.setSelection( nodeByPath.nextSibling, 0 );
  65. } );
  66. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  67. [ '<p>00</p><p></p><p></p>' ]
  68. ], { asWidget: true } ) );
  69. sinon.assert.calledOnce( refreshItemSpy );
  70. } );
  71. it( 'should rename <span> to <p> on adding other block element to the same table cell', () => {
  72. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  73. const table = root.getChild( 0 );
  74. model.change( writer => {
  75. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  76. const block = writer.createElement( 'block' );
  77. writer.insert( block, nodeByPath, 'after' );
  78. writer.setSelection( nodeByPath.nextSibling, 0 );
  79. } );
  80. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  81. [ '<p>00</p><div></div>' ]
  82. ], { asWidget: true } ) );
  83. sinon.assert.calledOnce( refreshItemSpy );
  84. } );
  85. it( 'should rename <span> to <p> on adding multiple other block elements to the same table cell', () => {
  86. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  87. const table = root.getChild( 0 );
  88. model.change( writer => {
  89. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  90. const block1 = writer.createElement( 'block' );
  91. const block2 = writer.createElement( 'block' );
  92. writer.insert( block1, nodeByPath, 'after' );
  93. writer.insert( block2, nodeByPath, 'after' );
  94. writer.setSelection( nodeByPath.nextSibling, 0 );
  95. } );
  96. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  97. [ '<p>00</p><div></div><div></div>' ]
  98. ], { asWidget: true } ) );
  99. sinon.assert.calledOnce( refreshItemSpy );
  100. } );
  101. it( 'should properly rename the same element on consecutive changes', () => {
  102. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  103. const table = root.getChild( 0 );
  104. model.change( writer => {
  105. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  106. writer.insertElement( 'paragraph', nodeByPath, 'after' );
  107. writer.setSelection( nodeByPath.nextSibling, 0 );
  108. } );
  109. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  110. [ '<p>00</p><p></p>' ]
  111. ], { asWidget: true } ) );
  112. sinon.assert.calledOnce( refreshItemSpy );
  113. model.change( writer => {
  114. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  115. } );
  116. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  117. [ '00' ]
  118. ], { asWidget: true } ) );
  119. sinon.assert.calledTwice( refreshItemSpy );
  120. } );
  121. it( 'should rename <span> to <p> when setting attribute on <paragraph>', () => {
  122. editor.setData( '<table><tr><td><p>00</p></td></tr></table>' );
  123. const table = root.getChild( 0 );
  124. model.change( writer => {
  125. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  126. } );
  127. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  128. [ '<p foo="bar">00</p>' ]
  129. ], { asWidget: true } ) );
  130. sinon.assert.calledOnce( refreshItemSpy );
  131. } );
  132. it( 'should rename <p> to <span> when removing one of two paragraphs inside table cell', () => {
  133. editor.setData( viewTable( [ [ '<p>00</p><p>foo</p>' ] ] ) );
  134. const table = root.getChild( 0 );
  135. model.change( writer => {
  136. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  137. } );
  138. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  139. [ '00' ]
  140. ], { asWidget: true } ) );
  141. sinon.assert.calledOnce( refreshItemSpy );
  142. } );
  143. it( 'should rename <p> to <span> when removing all but one paragraph inside table cell', () => {
  144. editor.setData( viewTable( [ [ '<p>00</p><p>foo</p><p>bar</p>' ] ] ) );
  145. const table = root.getChild( 0 );
  146. model.change( writer => {
  147. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  148. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  149. } );
  150. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  151. [ '00' ]
  152. ], { asWidget: true } ) );
  153. sinon.assert.calledOnce( refreshItemSpy );
  154. } );
  155. it( 'should rename <p> to <span> when removing attribute from <paragraph>', () => {
  156. editor.setData( '<table><tr><td><p foo="bar">00</p></td></tr></table>' );
  157. const table = root.getChild( 0 );
  158. model.change( writer => {
  159. writer.removeAttribute( 'foo', table.getNodeByPath( [ 0, 0, 0 ] ) );
  160. } );
  161. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  162. [ '<span style="display:inline-block">00</span>' ]
  163. ], { asWidget: true } ) );
  164. sinon.assert.calledOnce( refreshItemSpy );
  165. } );
  166. it( 'should keep <p> in the view when <paragraph> attribute value is changed', () => {
  167. editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
  168. const table = root.getChild( 0 );
  169. model.change( writer => {
  170. writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
  171. } );
  172. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  173. [ '<p foo="baz">00</p>' ]
  174. ], { asWidget: true } ) );
  175. // False positive: should not be called.
  176. sinon.assert.calledOnce( refreshItemSpy );
  177. } );
  178. it( 'should keep <p> in the view when adding another attribute to a <paragraph> with other attributes', () => {
  179. editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
  180. const table = root.getChild( 0 );
  181. model.change( writer => {
  182. writer.setAttribute( 'bar', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  183. } );
  184. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  185. [ '<p bar="bar" foo="bar">00</p>' ]
  186. ], { asWidget: true } ) );
  187. // False positive
  188. sinon.assert.notCalled( refreshItemSpy );
  189. } );
  190. it( 'should keep <p> in the view when adding another attribute to a <paragraph> and removing attribute that is already set', () => {
  191. editor.setData( viewTable( [ [ '<p foo="bar">00</p>' ] ] ) );
  192. const table = root.getChild( 0 );
  193. model.change( writer => {
  194. writer.setAttribute( 'bar', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  195. writer.removeAttribute( 'foo', table.getNodeByPath( [ 0, 0, 0 ] ) );
  196. } );
  197. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  198. [ '<p bar="bar">00</p>' ]
  199. ], { asWidget: true } ) );
  200. // False positive: should not be called.
  201. sinon.assert.calledOnce( refreshItemSpy );
  202. } );
  203. it( 'should keep <p> in the view when <paragraph> attribute value is changed (table cell with multiple blocks)', () => {
  204. editor.setData( viewTable( [ [ '<p foo="bar">00</p><p>00</p>' ] ] ) );
  205. const table = root.getChild( 0 );
  206. model.change( writer => {
  207. writer.setAttribute( 'foo', 'baz', table.getNodeByPath( [ 0, 0, 0 ] ) );
  208. } );
  209. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  210. [ '<p foo="baz">00</p><p>00</p>' ]
  211. ], { asWidget: true } ) );
  212. sinon.assert.notCalled( refreshItemSpy );
  213. } );
  214. it( 'should do nothing on rename <paragraph> to other block', () => {
  215. editor.setData( viewTable( [ [ '<p>00</p>' ] ] ) );
  216. const table = root.getChild( 0 );
  217. model.change( writer => {
  218. writer.rename( table.getNodeByPath( [ 0, 0, 0 ] ), 'block' );
  219. } );
  220. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  221. [ '<div>00</div>' ]
  222. ], { asWidget: true } ) );
  223. sinon.assert.notCalled( refreshItemSpy );
  224. } );
  225. it( 'should do nothing on adding <paragraph> to existing paragraphs', () => {
  226. editor.setData( viewTable( [ [ '<p>a</p><p>b</p>' ] ] ) );
  227. const table = root.getChild( 0 );
  228. model.change( writer => {
  229. writer.insertElement( 'paragraph', table.getNodeByPath( [ 0, 0, 1 ] ), 'after' );
  230. } );
  231. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  232. [ '<p>a</p><p>b</p><p></p>' ]
  233. ], { asWidget: true } ) );
  234. sinon.assert.notCalled( refreshItemSpy );
  235. } );
  236. it( 'should do nothing when setting attribute on block item other then <paragraph>', () => {
  237. editor.setData( viewTable( [ [ '<div>foo</div>' ] ] ) );
  238. const table = root.getChild( 0 );
  239. model.change( writer => {
  240. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  241. } );
  242. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  243. [ '<div foo="bar">foo</div>' ]
  244. ], { asWidget: true } ) );
  245. sinon.assert.notCalled( refreshItemSpy );
  246. } );
  247. it( 'should rename <p> in to <span> when removing <paragraph> (table cell with 2 paragraphs)', () => {
  248. editor.setData( viewTable( [ [ '<p>00</p><p>00</p>' ] ] ) );
  249. const table = root.getChild( 0 );
  250. model.change( writer => {
  251. writer.remove( writer.createRangeOn( table.getNodeByPath( [ 0, 0, 1 ] ) ) );
  252. } );
  253. assertEqualMarkup( getViewData( view, { withoutSelection: true } ), viewTable( [
  254. [ '<span style="display:inline-block">00</span>' ]
  255. ], { asWidget: true } ) );
  256. sinon.assert.calledOnce( refreshItemSpy );
  257. } );
  258. it( 'should update view selection after deleting content', () => {
  259. editor.setData( viewTable( [ [ '<p>foo</p><p>bar</p>' ] ] ) );
  260. const tableCell = root.getNodeByPath( [ 0, 0, 0 ] );
  261. // Replace table cell contents with paragraph - as model.deleteContent() does.
  262. model.change( writer => {
  263. writer.remove( writer.createRangeIn( tableCell ) );
  264. const paragraph = writer.createElement( 'paragraph' );
  265. writer.insert( paragraph, writer.createPositionAt( tableCell, 0 ) );
  266. // Set selection to newly created paragraph.
  267. writer.setSelection( paragraph, 0 );
  268. } );
  269. const viewRange = view.document.selection.getFirstRange();
  270. // Trying to map view selection to DOM range shouldn't throw after post-fixer will fix inserted <p> to <span>.
  271. expect( () => view.domConverter.viewRangeToDom( viewRange ) ).to.not.throw();
  272. } );
  273. it( 'should not update view selection after other feature set selection', () => {
  274. editor.model.schema.register( 'widget', {
  275. isObject: true,
  276. isBlock: true,
  277. allowWhere: '$block'
  278. } );
  279. editor.conversion.elementToElement( {
  280. model: 'widget',
  281. view: 'widget'
  282. } );
  283. editor.setData( viewTable( [ [ '<p>foo[]</p>' ] ] ) );
  284. const spy = sinon.spy();
  285. view.document.selection.on( 'change', spy );
  286. // Insert a widget in table cell and select it.
  287. model.change( writer => {
  288. const widgetElement = writer.createElement( 'widget' );
  289. const tableCell = root.getNodeByPath( [ 0, 0, 0, 0 ] );
  290. writer.insert( widgetElement, writer.createPositionAfter( tableCell ) );
  291. // Update the selection so it will be set on the widget and not in renamed paragraph.
  292. writer.setSelection( widgetElement, 'on' );
  293. } );
  294. // View selection should be updated only twice - will be set to null and then to widget.
  295. // If called thrice the selection post fixer for table cell was also called.
  296. sinon.assert.calledTwice( spy );
  297. } );
  298. // https://github.com/ckeditor/ckeditor5-table/issues/191.
  299. it( 'should not fire (and crash) for removed view elements', () => {
  300. editor.setData( viewTable( [ [ '<p>foo</p>' ] ] ) );
  301. const p = root.getNodeByPath( [ 0, 0, 0, 0 ] );
  302. // Replace table cell contents with paragraph - as model.deleteContent() does.
  303. model.change( writer => {
  304. writer.setSelection( writer.createRangeIn( root ) );
  305. editor.execute( 'delete' ); // For some reason it didn't crash with `writer.remove()`.
  306. writer.setAttribute( 'foo', 'bar', p );
  307. } );
  308. // Trying to map view selection to DOM range shouldn't throw after post-fixer will fix inserted <p> to <span>.
  309. expect( editor.getData() ).to.equal( '' );
  310. } );
  311. } );