clear.js 4.6 KB

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