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

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