mergecontainers.js 3.1 KB

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