breakContainer.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 WidgetElement from 'ckeditor5/engine/view/widgetelement.js';
  11. import Position from 'ckeditor5/engine/view/position.js';
  12. describe( 'writer', () => {
  13. /**
  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. */
  20. function test( input, expected ) {
  21. let { view, selection } = parse( input );
  22. const newPosition = breakContainer( selection.getFirstPosition() );
  23. expect( stringify( view.root, newPosition, { showType: true, showPriority: false } ) ).to.equal( expected );
  24. }
  25. describe( 'breakContainer', () => {
  26. it( 'break inside element - should break container element at given position', () => {
  27. test(
  28. '<container:div>' +
  29. '<container:p>' +
  30. '<attribute:b>foo</attribute:b>[]<attribute:u>bar</attribute:u>' +
  31. '</container:p>' +
  32. '</container:div>',
  33. '<container:div>' +
  34. '<container:p>' +
  35. '<attribute:b>foo</attribute:b>' +
  36. '</container:p>' +
  37. '[]<container:p>' +
  38. '<attribute:u>bar</attribute:u>' +
  39. '</container:p>' +
  40. '</container:div>'
  41. );
  42. } );
  43. it( 'break at start of element - should not break container and place returned position before element', () => {
  44. test(
  45. '<container:div><container:p>[]foobar</container:p></container:div>',
  46. '<container:div>[]<container:p>foobar</container:p></container:div>'
  47. );
  48. } );
  49. it( 'break at the end of element - should not break container and place returned position after element', () => {
  50. test(
  51. '<container:div><container:p>foobar[]</container:p></container:div>',
  52. '<container:div><container:p>foobar</container:p>[]</container:div>'
  53. );
  54. } );
  55. it( 'should throw if position parent is not container', () => {
  56. let { selection } = parse( '<container:div>foo{}bar</container:div>' );
  57. expect( () => {
  58. breakContainer( selection.getFirstPosition() );
  59. } ).to.throw( CKEditorError, /view-writer-break-non-container-element/ );
  60. } );
  61. it( 'should throw if position parent is root', () => {
  62. const element = new ContainerElement( 'div' );
  63. const position = Position.createAt( element, 0 );
  64. expect( () => {
  65. breakContainer( position );
  66. } ).to.throw( CKEditorError, /view-writer-break-root/ );
  67. } );
  68. it( 'should not allow to break WidgetElement', () => {
  69. const widget = new WidgetElement( 'figure' );
  70. const position = new Position( widget, 0 );
  71. expect( () => {
  72. breakContainer( position );
  73. } ).to.throw( CKEditorError, 'view-writer-break-non-container-element' );
  74. } );
  75. } );
  76. } );