clear.js 6.2 KB

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