mergecontainers.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. describe( 'Writer', () => {
  9. describe( 'mergeContainers()', () => {
  10. let writer;
  11. // Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  12. // test break position.
  13. //
  14. // @param {String} input
  15. // @param {String} expected
  16. function test( input, expected ) {
  17. const { view, selection } = parse( input );
  18. const newPosition = writer.mergeContainers( selection.getFirstPosition() );
  19. expect( stringify( view.root, newPosition, { showType: true, showPriority: false } ) ).to.equal( expected );
  20. }
  21. before( () => {
  22. writer = new Writer();
  23. } );
  24. it( 'should merge two container elements - position between elements', () => {
  25. test(
  26. '<container:div>' +
  27. '<attribute:b>foo</attribute:b>' +
  28. '</container:div>' +
  29. '[]<container:div>' +
  30. '<attribute:u>bar</attribute:u>' +
  31. '</container:div>',
  32. '<container:div><attribute:b>foo</attribute:b>[]<attribute:u>bar</attribute:u></container:div>'
  33. );
  34. } );
  35. it( 'should merge two container elements - position in text', () => {
  36. test(
  37. '<container:div>foo</container:div>[]<container:div>bar</container:div>',
  38. '<container:div>foo{}bar</container:div>'
  39. );
  40. } );
  41. it( 'should merge two different container elements', () => {
  42. test(
  43. '<container:div>foo</container:div>[]<container:p>bar</container:p>',
  44. '<container:div>foo{}bar</container:div>'
  45. );
  46. } );
  47. it( 'should throw if there is no element before position', () => {
  48. const { selection } = parse( '[]<container:div>foobar</container:div>' );
  49. expect( () => {
  50. writer.mergeContainers( selection.getFirstPosition() );
  51. } ).to.throw( CKEditorError, /view-writer-merge-containers-invalid-position/ );
  52. } );
  53. it( 'should throw if there is no element after position', () => {
  54. const { selection } = parse( '<container:div>foobar</container:div>[]' );
  55. expect( () => {
  56. writer.mergeContainers( selection.getFirstPosition() );
  57. } ).to.throw( CKEditorError, /view-writer-merge-containers-invalid-position/ );
  58. } );
  59. it( 'should throw if element before position is not a container element', () => {
  60. const { selection } = parse( '<attribute:u>foo</attribute:u>[]<container:div>bar</container:div>' );
  61. expect( () => {
  62. writer.mergeContainers( selection.getFirstPosition() );
  63. } ).to.throw( CKEditorError, /view-writer-merge-containers-invalid-position/ );
  64. } );
  65. it( 'should throw if element after position is not a container element', () => {
  66. const { selection } = parse( '<container:div>foo</container:div>[]<attribute:u>bar</attribute:u>' );
  67. expect( () => {
  68. writer.mergeContainers( selection.getFirstPosition() );
  69. } ).to.throw( CKEditorError, /view-writer-merge-containers-invalid-position/ );
  70. } );
  71. } );
  72. } );