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

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