clear.js 5.3 KB

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