tablecell-post-fixer.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  7. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { defaultConversion, defaultSchema, formatTable, formattedViewTable, modelTable } from '../_utils/utils';
  9. import injectTableCellPostFixer from '../../src/converters/tablecell-post-fixer';
  10. import env from '@ckeditor/ckeditor5-utils/src/env';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. describe( 'TableCell post-fixer', () => {
  13. let editor, model, doc, root, viewDocument;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  17. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  18. return VirtualTestEditor.create()
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. doc = model.document;
  23. root = doc.getRoot( 'main' );
  24. viewDocument = editor.editing.view;
  25. defaultSchema( model.schema );
  26. defaultConversion( editor.conversion, true );
  27. injectTableCellPostFixer( model, editor.editing );
  28. } );
  29. } );
  30. it( 'should create <span> element for single paragraph inside table cell', () => {
  31. setModelData( model, modelTable( [ [ '00[]' ] ] ) );
  32. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formatTable(
  33. '<figure class="ck-widget ck-widget_selectable table" contenteditable="false">' +
  34. '<div class="ck ck-widget__selection-handler"></div>' +
  35. '<table>' +
  36. '<tbody>' +
  37. '<tr>' +
  38. '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">' +
  39. '<span>00</span>' +
  40. '</td>' +
  41. '</tr>' +
  42. '</tbody>' +
  43. '</table>' +
  44. '</figure>'
  45. ) );
  46. } );
  47. it( 'should rename <span> to <p> when more then one block content inside table cell', () => {
  48. setModelData( model, modelTable( [ [ '00[]' ] ] ) );
  49. const table = root.getChild( 0 );
  50. model.change( writer => {
  51. const nodeByPath = table.getNodeByPath( [ 0, 0, 0 ] );
  52. const paragraph = writer.createElement( 'paragraph' );
  53. writer.insert( paragraph, nodeByPath, 'after' );
  54. writer.setSelection( nodeByPath.nextSibling, 0 );
  55. } );
  56. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  57. [ '<p>00</p><p></p>' ]
  58. ], { asWidget: true } ) );
  59. } );
  60. it( 'should rename <p> to <span> when removing all but one paragraph inside table cell', () => {
  61. setModelData( model, modelTable( [ [ '<paragraph>00[]</paragraph><paragraph>foo</paragraph>' ] ] ) );
  62. const table = root.getChild( 0 );
  63. model.change( writer => {
  64. writer.remove( table.getNodeByPath( [ 0, 0, 1 ] ) );
  65. } );
  66. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  67. [ '00' ]
  68. ], { asWidget: true } ) );
  69. } );
  70. it( 'should rename <span> to <p> when setting attribute on paragraph', () => {
  71. model.schema.extend( '$block', { allowAttributes: 'foo' } );
  72. editor.conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  73. setModelData( model, modelTable( [ [ '<paragraph>00[]</paragraph>' ] ] ) );
  74. const table = root.getChild( 0 );
  75. model.change( writer => {
  76. writer.setAttribute( 'foo', 'bar', table.getNodeByPath( [ 0, 0, 0 ] ) );
  77. } );
  78. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  79. [ '<p foo="bar">00</p>' ]
  80. ], { asWidget: true } ) );
  81. } );
  82. it( 'should rename <p> to <span> when removing attribute from paragraph', () => {
  83. model.schema.extend( '$block', { allowAttributes: 'foo' } );
  84. editor.conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  85. setModelData( model, modelTable( [ [ '<paragraph foo="bar">00[]</paragraph>' ] ] ) );
  86. const table = root.getChild( 0 );
  87. model.change( writer => {
  88. writer.removeAttribute( 'foo', table.getNodeByPath( [ 0, 0, 0 ] ) );
  89. } );
  90. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  91. [ '<span>00</span>' ]
  92. ], { asWidget: true } ) );
  93. } );
  94. it( 'should do nothing on rename <paragraph> to <heading1>', () => {
  95. setModelData( model, modelTable( [ [ '00' ] ] ) );
  96. const table = root.getChild( 0 );
  97. editor.conversion.elementToElement( { model: 'heading1', view: 'h1' } );
  98. model.change( writer => {
  99. writer.rename( table.getNodeByPath( [ 0, 0, 0 ] ), 'heading1' );
  100. } );
  101. expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
  102. [ '<h1>00</h1>' ]
  103. ], { asWidget: true } ) );
  104. } );
  105. } );