breakrange.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view, browser-only */
  6. import { breakRange } from '/ckeditor5/engine/view/writer.js';
  7. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  8. import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  9. import Range from '/ckeditor5/engine/view/range.js';
  10. import { stringify, parse } from '/tests/engine/_utils/view.js';
  11. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. describe( 'writer', () => {
  13. /**
  14. * Executes test using `parse` and `stringify` utils functions.
  15. *
  16. * @param {String} input
  17. * @param {String} expected
  18. */
  19. function test( input, expected ) {
  20. let { view, selection } = parse( input );
  21. const newRange = breakRange( selection.getFirstRange() );
  22. expect( stringify( view.root, newRange, { showType: true } ) ).to.equal( expected );
  23. }
  24. describe( 'breakRange', () => {
  25. it( 'should throw when range placed in two containers', () => {
  26. const p1 = new ContainerElement( 'p' );
  27. const p2 = new ContainerElement( 'p' );
  28. expect( () => {
  29. breakRange( Range.createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  30. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  31. } );
  32. it( 'should throw when range has no parent container', () => {
  33. const el = new AttributeElement( 'b' );
  34. expect( () => {
  35. breakRange( Range.createFromParentsAndOffsets( el, 0, el, 0 ) );
  36. } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
  37. } );
  38. it( 'should not break text nodes if they are not in attribute elements', () => {
  39. test(
  40. '<container:p>foo{}bar</container:p>',
  41. '<container:p>foo{}bar</container:p>'
  42. );
  43. } );
  44. it( 'should break at collapsed range and return collapsed one', () => {
  45. test(
  46. '<container:p><attribute:b>foo{}bar</attribute:b></container:p>',
  47. '<container:p><attribute:b>foo</attribute:b>[]<attribute:b>bar</attribute:b></container:p>'
  48. );
  49. } );
  50. it( 'should break inside text node #1', () => {
  51. test(
  52. '<container:p><attribute:b>foo{bar}baz</attribute:b></container:p>',
  53. '<container:p><attribute:b>foo</attribute:b>[<attribute:b>bar</attribute:b>]<attribute:b>baz</attribute:b></container:p>'
  54. );
  55. } );
  56. it( 'should break inside text node #2', () => {
  57. test(
  58. '<container:p><attribute:b>foo{barbaz}</attribute:b></container:p>',
  59. '<container:p><attribute:b>foo</attribute:b>[<attribute:b>barbaz</attribute:b>]</container:p>'
  60. );
  61. } );
  62. it( 'should break inside text node #3', () => {
  63. test(
  64. '<container:p><attribute:b>foo{barbaz]</attribute:b></container:p>',
  65. '<container:p><attribute:b>foo</attribute:b>[<attribute:b>barbaz</attribute:b>]</container:p>'
  66. );
  67. } );
  68. it( 'should break inside text node #4', () => {
  69. test(
  70. '<container:p><attribute:b>{foo}barbaz</attribute:b></container:p>',
  71. '<container:p>[<attribute:b>foo</attribute:b>]<attribute:b>barbaz</attribute:b></container:p>'
  72. );
  73. } );
  74. it( 'should break inside text node #5', () => {
  75. test(
  76. '<container:p><attribute:b>[foo}barbaz</attribute:b></container:p>',
  77. '<container:p>[<attribute:b>foo</attribute:b>]<attribute:b>barbaz</attribute:b></container:p>'
  78. );
  79. } );
  80. it( 'should break placed inside different nodes', () => {
  81. test(
  82. '<container:p>foo{bar<attribute:b>baz}qux</attribute:b></container:p>',
  83. '<container:p>foo{bar<attribute:b>baz</attribute:b>]<attribute:b>qux</attribute:b></container:p>'
  84. );
  85. } );
  86. it( 'should split attribute element directly in document fragment', () => {
  87. test(
  88. '<attribute:b>fo{ob}ar</attribute:b>',
  89. '<attribute:b>fo</attribute:b>[<attribute:b>ob</attribute:b>]<attribute:b>ar</attribute:b>'
  90. );
  91. } );
  92. it( 'should not split text directly in document fragment', () => {
  93. test(
  94. 'foo{}bar',
  95. 'foo{}bar'
  96. );
  97. } );
  98. } );
  99. } );