breakrange.js 3.7 KB

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