8
0

breakcontainer.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Writer 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. describe( 'breakContainer()', () => {
  12. let writer;
  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. function test( input, expected ) {
  19. const { view, selection } = parse( input );
  20. const newPosition = writer.breakContainer( selection.getFirstPosition() );
  21. expect( stringify( view.root, newPosition, { showType: true, showPriority: false } ) ).to.equal( expected );
  22. }
  23. before( () => {
  24. writer = new Writer();
  25. } );
  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. const { selection } = parse( '<container:div>foo{}bar</container:div>' );
  57. expect( () => {
  58. writer.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. writer.breakContainer( position );
  66. } ).to.throw( CKEditorError, /view-writer-break-root/ );
  67. } );
  68. } );
  69. } );