breakrange.js 3.8 KB

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