8
0

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

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