clear.js 5.9 KB

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