8
0

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

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