breakrange.js 3.8 KB

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