breakContainer.js 2.6 KB

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