breakcontainer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import DowncastWriter from '../../../src/view/downcastwriter';
  6. import { stringify, parse } from '../../../src/dev-utils/view';
  7. import ContainerElement from '../../../src/view/containerelement';
  8. import Position from '../../../src/view/position';
  9. import Document from '../../../src/view/document';
  10. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'DowncastWriter', () => {
  12. describe( 'breakContainer()', () => {
  13. let writer;
  14. // Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  15. // test break position.
  16. //
  17. // @param {String} input
  18. // @param {String} expected
  19. function test( input, expected ) {
  20. const { view, selection } = parse( input );
  21. const newPosition = writer.breakContainer( selection.getFirstPosition() );
  22. expect( stringify( view.root, newPosition, { showType: true, showPriority: false } ) ).to.equal( expected );
  23. }
  24. before( () => {
  25. writer = new DowncastWriter( new Document() );
  26. } );
  27. it( 'break inside element - should break container element at given position', () => {
  28. test(
  29. '<container:div>' +
  30. '<container:p>' +
  31. '<attribute:b>foo</attribute:b>[]<attribute:u>bar</attribute:u>' +
  32. '</container:p>' +
  33. '</container:div>',
  34. '<container:div>' +
  35. '<container:p>' +
  36. '<attribute:b>foo</attribute:b>' +
  37. '</container:p>' +
  38. '[]<container:p>' +
  39. '<attribute:u>bar</attribute:u>' +
  40. '</container:p>' +
  41. '</container:div>'
  42. );
  43. } );
  44. it( 'break at start of element - should not break container and place returned position before element', () => {
  45. test(
  46. '<container:div><container:p>[]foobar</container:p></container:div>',
  47. '<container:div>[]<container:p>foobar</container:p></container:div>'
  48. );
  49. } );
  50. it( 'break at the end of element - should not break container and place returned position after element', () => {
  51. test(
  52. '<container:div><container:p>foobar[]</container:p></container:div>',
  53. '<container:div><container:p>foobar</container:p>[]</container:div>'
  54. );
  55. } );
  56. it( 'should throw if position parent is not container', () => {
  57. const { selection } = parse( '<container:div>foo{}bar</container:div>' );
  58. expectToThrowCKEditorError( () => {
  59. writer.breakContainer( selection.getFirstPosition() );
  60. }, /view-writer-break-non-container-element/, writer );
  61. } );
  62. it( 'should throw if position parent is root', () => {
  63. const element = new ContainerElement( 'div' );
  64. const position = Position._createAt( element, 0 );
  65. expectToThrowCKEditorError( () => {
  66. writer.breakContainer( position );
  67. }, /view-writer-break-root/, writer );
  68. } );
  69. } );
  70. } );