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

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