breakcontainer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { breakContainer } from '../../../src/view/writer';
  6. import { stringify, parse } from '../../../src/dev-utils/view';
  7. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  8. import ContainerElement from '../../../src/view/containerelement';
  9. import Position from '../../../src/view/position';
  10. describe( 'writer', () => {
  11. /**
  12. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  13. * test break position.
  14. *
  15. * @param {String} input
  16. * @param {String} expected
  17. */
  18. function test( input, expected ) {
  19. const { view, selection } = parse( input );
  20. const newPosition = breakContainer( selection.getFirstPosition() );
  21. expect( stringify( view.root, newPosition, { showType: true, showPriority: false } ) ).to.equal( expected );
  22. }
  23. describe( 'breakContainer', () => {
  24. it( 'break inside element - should break container element at given position', () => {
  25. test(
  26. '<container:div>' +
  27. '<container:p>' +
  28. '<attribute:b>foo</attribute:b>[]<attribute:u>bar</attribute:u>' +
  29. '</container:p>' +
  30. '</container:div>',
  31. '<container:div>' +
  32. '<container:p>' +
  33. '<attribute:b>foo</attribute:b>' +
  34. '</container:p>' +
  35. '[]<container:p>' +
  36. '<attribute:u>bar</attribute:u>' +
  37. '</container:p>' +
  38. '</container:div>'
  39. );
  40. } );
  41. it( 'break at start of element - should not break container and place returned position before element', () => {
  42. test(
  43. '<container:div><container:p>[]foobar</container:p></container:div>',
  44. '<container:div>[]<container:p>foobar</container:p></container:div>'
  45. );
  46. } );
  47. it( 'break at the end of element - should not break container and place returned position after element', () => {
  48. test(
  49. '<container:div><container:p>foobar[]</container:p></container:div>',
  50. '<container:div><container:p>foobar</container:p>[]</container:div>'
  51. );
  52. } );
  53. it( 'should throw if position parent is not container', () => {
  54. const { selection } = parse( '<container:div>foo{}bar</container:div>' );
  55. expect( () => {
  56. breakContainer( selection.getFirstPosition() );
  57. } ).to.throw( CKEditorError, /view-writer-break-non-container-element/ );
  58. } );
  59. it( 'should throw if position parent is root', () => {
  60. const element = new ContainerElement( 'div' );
  61. const position = Position.createAt( element, 0 );
  62. expect( () => {
  63. breakContainer( position );
  64. } ).to.throw( CKEditorError, /view-writer-break-root/ );
  65. } );
  66. } );
  67. } );