breakrange.js 3.7 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. 'use strict';
  7. import { breakRange } from '/ckeditor5/engine/view/writer.js';
  8. import DocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  9. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  10. import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  11. import Text from '/ckeditor5/engine/view/text.js';
  12. import Range from '/ckeditor5/engine/view/range.js';
  13. import { stringify, parse } from '/tests/engine/_utils/view.js';
  14. describe( 'writer', () => {
  15. /**
  16. * Executes test using `parse` and `stringify` utils functions.
  17. *
  18. * @param {String} input
  19. * @param {String} expected
  20. */
  21. function test( input, expected ) {
  22. let { view, selection } = parse( input );
  23. if ( view instanceof AttributeElement || view instanceof Text ) {
  24. view = new DocumentFragment( view );
  25. }
  26. const newRange = breakRange( selection.getFirstRange() );
  27. expect( stringify( view, newRange, { showType: true } ) ).to.equal( expected );
  28. }
  29. describe( 'breakRange', () => {
  30. it( 'should throw when range placed in two containers', () => {
  31. const p1 = new ContainerElement( 'p' );
  32. const p2 = new ContainerElement( 'p' );
  33. expect( () => {
  34. breakRange( Range.createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  35. } ).to.throw( '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. } );