8
0

breakcontainer.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import DowncastWriter from '../../../src/view/downcastwriter';
  6. import { stringify, parse } from '../../../src/dev-utils/view';
  7. import ContainerElement from '../../../src/view/containerelement';
  8. import Position from '../../../src/view/position';
  9. import Document from '../../../src/view/document';
  10. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'DowncastWriter', () => {
  12. describe( 'breakContainer()', () => {
  13. let writer, document;
  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. document = new Document();
  26. writer = new DowncastWriter( document );
  27. } );
  28. it( 'break inside element - should break container element at given position', () => {
  29. test(
  30. '<container:div>' +
  31. '<container:p>' +
  32. '<attribute:b>foo</attribute:b>[]<attribute:u>bar</attribute:u>' +
  33. '</container:p>' +
  34. '</container:div>',
  35. '<container:div>' +
  36. '<container:p>' +
  37. '<attribute:b>foo</attribute:b>' +
  38. '</container:p>' +
  39. '[]<container:p>' +
  40. '<attribute:u>bar</attribute:u>' +
  41. '</container:p>' +
  42. '</container:div>'
  43. );
  44. } );
  45. it( 'break at start of element - should not break container and place returned position before element', () => {
  46. test(
  47. '<container:div><container:p>[]foobar</container:p></container:div>',
  48. '<container:div>[]<container:p>foobar</container:p></container:div>'
  49. );
  50. } );
  51. it( 'break at the end of element - should not break container and place returned position after element', () => {
  52. test(
  53. '<container:div><container:p>foobar[]</container:p></container:div>',
  54. '<container:div><container:p>foobar</container:p>[]</container:div>'
  55. );
  56. } );
  57. it( 'should throw if position parent is not container', () => {
  58. const { selection } = parse( '<container:div>foo{}bar</container:div>' );
  59. expectToThrowCKEditorError( () => {
  60. writer.breakContainer( selection.getFirstPosition() );
  61. }, /view-writer-break-non-container-element/, writer );
  62. } );
  63. it( 'should throw if position parent is root', () => {
  64. const element = new ContainerElement( document, 'div' );
  65. const position = Position._createAt( element, 0 );
  66. expectToThrowCKEditorError( () => {
  67. writer.breakContainer( position );
  68. }, /view-writer-break-root/, writer );
  69. } );
  70. } );
  71. } );