8
0

pagebreakediting.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import PageBreakEditing from '../src/pagebreakediting';
  7. import PageBreakCommand from '../src/pagebreakcommand';
  8. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  10. import { isWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  11. import env from '@ckeditor/ckeditor5-utils/src/env';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. describe( 'PageBreakEditing', () => {
  14. let editor, model, view, viewDocument;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  18. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  19. return VirtualTestEditor
  20. .create( {
  21. plugins: [ PageBreakEditing ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. view = editor.editing.view;
  27. viewDocument = view.document;
  28. } );
  29. } );
  30. it( 'should be loaded', () => {
  31. expect( editor.plugins.get( PageBreakEditing ) ).to.be.instanceOf( PageBreakEditing );
  32. } );
  33. it( 'should set proper schema rules', () => {
  34. expect( model.schema.checkChild( [ '$root' ], 'pageBreak' ) ).to.be.true;
  35. expect( model.schema.isObject( 'pageBreak' ) ).to.be.true;
  36. expect( model.schema.checkChild( [ '$root', 'pageBreak' ], '$text' ) ).to.be.false;
  37. expect( model.schema.checkChild( [ '$root', '$block' ], 'pageBreak' ) ).to.be.false;
  38. } );
  39. it( 'should register imageInsert command', () => {
  40. expect( editor.commands.get( 'pageBreak' ) ).to.be.instanceOf( PageBreakCommand );
  41. } );
  42. describe( 'conversion in data pipeline', () => {
  43. describe( 'model to view', () => {
  44. it( 'should convert', () => {
  45. setModelData( model, '<pageBreak></pageBreak>' );
  46. expect( editor.getData() ).to.equal(
  47. '<div style="page-break-after:always;"><span style="display:none;">&nbsp;</span></div>'
  48. );
  49. } );
  50. } );
  51. describe( 'view to model', () => {
  52. it( 'should convert the page break code element', () => {
  53. editor.setData( '<div style="page-break-after:always;"><span style="display:none;">&nbsp;</span></div>' );
  54. expect( getModelData( model, { withoutSelection: true } ) )
  55. .to.equal( '<pageBreak></pageBreak>' );
  56. } );
  57. it( 'should not convert in wrong context', () => {
  58. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  59. model.schema.addChildCheck( ( ctx, childDef ) => {
  60. if ( ctx.endsWith( '$root' ) && childDef.name == 'pageBreak' ) {
  61. return false;
  62. }
  63. } );
  64. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  65. editor.setData( '<div><div style="page-break-after:always;"><span style="display:none;">&nbsp;</span></div></div>' );
  66. expect( getModelData( model, { withoutSelection: true } ) )
  67. .to.equal( '<div> </div>' );
  68. } );
  69. it( 'should not convert if outer div has wrong styles', () => {
  70. editor.setData( '<div style="page-break-after:auto;"><span style="display:none;">&nbsp;</span></div>' );
  71. expect( getModelData( model, { withoutSelection: true } ) )
  72. .to.equal( '' );
  73. } );
  74. it( 'should not convert if outer div has no children', () => {
  75. editor.setData( '<div style="page-break-after:always;"></div>' );
  76. expect( getModelData( model, { withoutSelection: true } ) )
  77. .to.equal( '' );
  78. } );
  79. it( 'should not convert if outer div has too many children', () => {
  80. editor.setData(
  81. '<div style="page-break-after:always;">' +
  82. '<span style="display:none;">&nbsp;</span>' +
  83. '<span style="display:none;">&nbsp;</span>' +
  84. '</div>'
  85. );
  86. expect( getModelData( model, { withoutSelection: true } ) )
  87. .to.equal( '' );
  88. } );
  89. it( 'should not convert if inner span has wrong styles', () => {
  90. editor.setData(
  91. '<div style="page-break-after:always;">' +
  92. '<span style="display:inline-block;">&nbsp;</span>' +
  93. '</div>'
  94. );
  95. expect( getModelData( model, { withoutSelection: true } ) )
  96. .to.equal( '' );
  97. } );
  98. it( 'should not convert if inner span has wrong styles', () => {
  99. editor.setData(
  100. '<div style="page-break-after:always;">' +
  101. '<span style="display:inline-block;">&nbsp;</span>' +
  102. '</div>'
  103. );
  104. expect( getModelData( model, { withoutSelection: true } ) )
  105. .to.equal( '' );
  106. } );
  107. it( 'should not convert if inner span has any children', () => {
  108. editor.setData(
  109. '<div style="page-break-after:always;">' +
  110. '<span style="display:none;">' +
  111. '<span>Foo</span>' +
  112. '</span>' +
  113. '</div>'
  114. );
  115. expect( getModelData( model, { withoutSelection: true } ) )
  116. .to.equal( '' );
  117. } );
  118. it( 'should not convert if inner span has text', () => {
  119. editor.setData(
  120. '<div style="page-break-after:always;">' +
  121. '<span style="display:none;">Foo</span>' +
  122. '</div>'
  123. );
  124. expect( getModelData( model, { withoutSelection: true } ) )
  125. .to.equal( '' );
  126. } );
  127. it( 'should not convert inner span if outer div was already consumed', () => {
  128. model.schema.register( 'section', { inheritAllFrom: '$block' } );
  129. editor.conversion.elementToElement( { model: 'section', view: 'section' } );
  130. model.schema.register( 'span', { inheritAllFrom: '$block' } );
  131. editor.model.schema.extend( '$text', { allowAttributes: 'foo' } );
  132. editor.conversion.attributeToElement( {
  133. model: 'foo',
  134. view: {
  135. name: 'span',
  136. styles: {
  137. display: 'none'
  138. }
  139. }
  140. } );
  141. editor.setData(
  142. '<section>' +
  143. '<div style="page-break-after:always;">' +
  144. '<span style="display:none;">&nbsp;</span>' +
  145. '</div>' +
  146. '<span style="display:none;">Foo</span>' +
  147. '</section>'
  148. );
  149. expect( getModelData( model, { withoutSelection: true } ) )
  150. .to.equal( '<pageBreak></pageBreak><section><$text foo="true">Foo</$text></section>' );
  151. } );
  152. it( 'should not convert if inner span has no children', () => {
  153. editor.setData( '<div style="page-break-after:always;"><span style="display:none;"></span></div>' );
  154. expect( getModelData( model, { withoutSelection: true } ) )
  155. .to.equal( '' );
  156. } );
  157. it( 'should not convert if inner span has other element as a child', () => {
  158. editor.setData( '<div style="page-break-after:always;"><span style="display:none;"><span></span></span></div>' );
  159. expect( getModelData( model, { withoutSelection: true } ) )
  160. .to.equal( '' );
  161. } );
  162. } );
  163. } );
  164. describe( 'conversion in editing pipeline', () => {
  165. describe( 'model to view', () => {
  166. it( 'should convert', () => {
  167. setModelData( model, '<pageBreak></pageBreak>' );
  168. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  169. '<div class="ck-page-break ck-widget" contenteditable="false"></div>'
  170. );
  171. } );
  172. it( 'converted element should be widgetized', () => {
  173. setModelData( model, '<pageBreak></pageBreak>' );
  174. const widget = viewDocument.getRoot().getChild( 0 );
  175. expect( widget.name ).to.equal( 'div' );
  176. expect( isPageBreakWidget( widget ) ).to.be.true;
  177. } );
  178. } );
  179. } );
  180. function isPageBreakWidget( viewElement ) {
  181. return !!viewElement.getCustomProperty( 'pageBreak' ) && isWidget( viewElement );
  182. }
  183. } );