8
0

breakcontainer.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2018, 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. import Document from '../../../src/view/document';
  11. describe( 'Writer', () => {
  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 Writer( 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. expect( () => {
  59. writer.breakContainer( selection.getFirstPosition() );
  60. } ).to.throw( CKEditorError, /view-writer-break-non-container-element/ );
  61. } );
  62. it( 'should throw if position parent is root', () => {
  63. const element = new ContainerElement( 'div' );
  64. const position = Position.createAt( element, 0 );
  65. expect( () => {
  66. writer.breakContainer( position );
  67. } ).to.throw( CKEditorError, /view-writer-break-root/ );
  68. } );
  69. } );
  70. } );