clear.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import DowncastWriter from '../../../src/view/downcastwriter';
  6. import Range from '../../../src/view/range';
  7. import { stringify, parse } from '../../../src/dev-utils/view';
  8. import ContainerElement from '../../../src/view/containerelement';
  9. import AttributeElement from '../../../src/view/attributeelement';
  10. import EmptyElement from '../../../src/view/emptyelement';
  11. import UIElement from '../../../src/view/uielement';
  12. import Document from '../../../src/view/document';
  13. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  14. describe( 'DowncastWriter', () => {
  15. describe( 'clear()', () => {
  16. let writer, document;
  17. // Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create ranges.
  18. //
  19. // @param {Object} elementToRemove
  20. // @param {String} input
  21. // @param {String} expectedResult
  22. function test( elementToRemove, input, expectedResult ) {
  23. const { view, selection } = parse( input );
  24. writer.clear( selection.getFirstRange(), elementToRemove );
  25. expect( stringify( view, null, { showType: true } ) ).to.equal( expectedResult );
  26. }
  27. beforeEach( () => {
  28. document = new Document();
  29. writer = new DowncastWriter( document );
  30. } );
  31. it( 'should throw when range placed in two containers', () => {
  32. const p1 = new ContainerElement( 'p' );
  33. const p2 = new ContainerElement( 'p' );
  34. expectToThrowCKEditorError( () => {
  35. writer.clear( Range._createFromParentsAndOffsets( p1, 0, p2, 0 ) );
  36. }, 'view-writer-invalid-range-container', document );
  37. } );
  38. it( 'should throw when range has no parent container', () => {
  39. const el = new AttributeElement( 'b' );
  40. expectToThrowCKEditorError( () => {
  41. writer.clear( Range._createFromParentsAndOffsets( el, 0, el, 0 ) );
  42. }, 'view-writer-invalid-range-container', document );
  43. } );
  44. it( 'should remove matched element from range', () => {
  45. const elementToRemove = new AttributeElement( 'b' );
  46. test(
  47. elementToRemove,
  48. '<container:p>[b<attribute:b>a</attribute:b>r]</container:p>',
  49. '<container:p>br</container:p>'
  50. );
  51. } );
  52. it( 'should remove matched element from range when range is inside text node', () => {
  53. const elementToRemove = new AttributeElement( 'b' );
  54. test(
  55. elementToRemove,
  56. '<container:p>F{o<attribute:b>o</attribute:b> ba}r</container:p>',
  57. '<container:p>Fo bar</container:p>'
  58. );
  59. } );
  60. it( 'should remove multiple matched elements from range', () => {
  61. const elementToRemove = new AttributeElement( 'b' );
  62. test(
  63. elementToRemove,
  64. '<container:p>[Fo<attribute:b>o</attribute:b> b<attribute:b>a</attribute:b>r]</container:p>',
  65. '<container:p>Fo br</container:p>'
  66. );
  67. } );
  68. it( 'should remove multiple matched elements from range when range is inside text node', () => {
  69. const elementToRemove = new AttributeElement( 'b' );
  70. test(
  71. elementToRemove,
  72. '<container:p>F{o<attribute:b>o</attribute:b> <attribute:b>b</attribute:b>a}r</container:p>',
  73. '<container:p>Fo ar</container:p>'
  74. );
  75. } );
  76. it( 'should remove only matched element', () => {
  77. const elementToRemove = new AttributeElement( 'b' );
  78. test(
  79. elementToRemove,
  80. '<container:p>F{o<attribute:i>o</attribute:i> <attribute:b>b</attribute:b>a}r</container:p>',
  81. '<container:p>Fo<attribute:i>o</attribute:i> ar</container:p>'
  82. );
  83. } );
  84. it( 'should remove part of node when range ends inside this node', () => {
  85. const elementToRemove = new AttributeElement( 'b' );
  86. test(
  87. elementToRemove,
  88. '<container:p>f{oo<attribute:b>ba}r</attribute:b></container:p>',
  89. '<container:p>foo<attribute:b>r</attribute:b></container:p>'
  90. );
  91. } );
  92. it( 'should remove part of node when range starts inside this node', () => {
  93. const elementToRemove = new AttributeElement( 'b' );
  94. test(
  95. elementToRemove,
  96. '<container:p>foo<attribute:b>b{ar</attribute:b>bi}z</container:p>',
  97. '<container:p>foo<attribute:b>b</attribute:b>biz</container:p>'
  98. );
  99. } );
  100. it( 'should remove part of node when range starts and ends inside this node', () => {
  101. const elementToRemove = new AttributeElement( 'b' );
  102. test(
  103. elementToRemove,
  104. '<container:p>foo<attribute:b>b{a}r</attribute:b>biz</container:p>',
  105. '<container:p>foo<attribute:b>br</attribute:b>biz</container:p>'
  106. );
  107. } );
  108. it( 'should merge after removing', () => {
  109. const elementToRemove = new AttributeElement( 'b' );
  110. test(
  111. elementToRemove,
  112. '<container:p>' +
  113. '<attribute:a>fo{o</attribute:a><attribute:b>a</attribute:b><attribute:a>b}iz</attribute:a>' +
  114. '</container:p>',
  115. '<container:p><attribute:a>foobiz</attribute:a></container:p>'
  116. );
  117. } );
  118. it( 'should remove EmptyElement', () => {
  119. const elementToRemove = new EmptyElement( 'img' );
  120. test(
  121. elementToRemove,
  122. '<container:p>f{oo<empty:img></empty:img>ba}r</container:p>',
  123. '<container:p>foobar</container:p>'
  124. );
  125. } );
  126. it( 'should remove UIElement', () => {
  127. const elementToRemove = new UIElement( 'span' );
  128. test(
  129. elementToRemove,
  130. '<container:p>f{oo<ui:span></ui:span>ba}r</container:p>',
  131. '<container:p>foobar</container:p>'
  132. );
  133. } );
  134. it( 'should remove ContainerElement', () => {
  135. const elementToRemove = new ContainerElement( 'p' );
  136. test(
  137. elementToRemove,
  138. '[<container:div>foo</container:div><container:p>bar</container:p><container:div>biz</container:div>]',
  139. '<container:div>foo</container:div><container:div>biz</container:div>'
  140. );
  141. } );
  142. } );
  143. } );