8
0

insert.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { insert } from '../../../src/view/writer';
  6. import ContainerElement from '../../../src/view/containerelement';
  7. import Element from '../../../src/view/element';
  8. import EmptyElement from '../../../src/view/emptyelement';
  9. import UIElement from '../../../src/view/uielement';
  10. import Position from '../../../src/view/position';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. import { stringify, parse } from '../../../src/dev-utils/view';
  13. import AttributeElement from '../../../src/view/attributeelement';
  14. describe( 'writer', () => {
  15. /**
  16. * Executes test using `parse` and `stringify` utils functions.
  17. *
  18. * @param {String} input
  19. * @param {Array.<String>} nodesToInsert
  20. * @param {String} expected
  21. */
  22. function test( input, nodesToInsert, expected ) {
  23. nodesToInsert = nodesToInsert.map( node => parse( node ) );
  24. const { view, selection } = parse( input );
  25. const newRange = insert( selection.getFirstPosition(), nodesToInsert );
  26. expect( stringify( view.root, newRange, { showType: true, showPriority: true } ) ).to.equal( expected );
  27. }
  28. describe( 'insert', () => {
  29. it( 'should return collapsed range in insertion position when using empty array', () => {
  30. test(
  31. '<container:p>foo{}bar</container:p>',
  32. [],
  33. '<container:p>foo{}bar</container:p>'
  34. );
  35. } );
  36. it( 'should insert text into another text node #1', () => {
  37. test(
  38. '<container:p>foo{}bar</container:p>',
  39. [ 'baz' ],
  40. '<container:p>foo{baz}bar</container:p>'
  41. );
  42. } );
  43. it( 'should insert text into another text node #2', () => {
  44. test(
  45. '<container:p>foobar{}</container:p>',
  46. [ 'baz' ],
  47. '<container:p>foobar{baz]</container:p>'
  48. );
  49. } );
  50. it( 'should insert text into another text node #3', () => {
  51. test(
  52. '<container:p>{}foobar</container:p>',
  53. [ 'baz' ],
  54. '<container:p>[baz}foobar</container:p>'
  55. );
  56. } );
  57. it( 'should break attributes when inserting into text node', () => {
  58. test(
  59. '<container:p>foo{}bar</container:p>',
  60. [ '<attribute:b view-priority="1">baz</attribute:b>' ],
  61. '<container:p>foo[<attribute:b view-priority="1">baz</attribute:b>]bar</container:p>'
  62. );
  63. } );
  64. it( 'should merge text nodes', () => {
  65. test(
  66. '<container:p>[]foobar</container:p>',
  67. [ 'baz' ],
  68. '<container:p>[baz}foobar</container:p>'
  69. );
  70. } );
  71. it( 'should merge same attribute nodes', () => {
  72. test(
  73. '<container:p><attribute:b view-priority="1">foo{}bar</attribute:b></container:p>',
  74. [ '<attribute:b view-priority="1">baz</attribute:b>' ],
  75. '<container:p><attribute:b view-priority="1">foo{baz}bar</attribute:b></container:p>'
  76. );
  77. } );
  78. it( 'should not merge different attributes', () => {
  79. test(
  80. '<container:p><attribute:b view-priority="1">foo{}bar</attribute:b></container:p>',
  81. [ '<attribute:b view-priority="2">baz</attribute:b>' ],
  82. '<container:p>' +
  83. '<attribute:b view-priority="1">' +
  84. 'foo' +
  85. '</attribute:b>' +
  86. '[' +
  87. '<attribute:b view-priority="2">' +
  88. 'baz' +
  89. '</attribute:b>' +
  90. ']' +
  91. '<attribute:b view-priority="1">' +
  92. 'bar' +
  93. '</attribute:b>' +
  94. '</container:p>'
  95. );
  96. } );
  97. it( 'should allow to insert multiple nodes', () => {
  98. test(
  99. '<container:p>[]</container:p>',
  100. [ '<attribute:b view-priority="1">foo</attribute:b>', 'bar' ],
  101. '<container:p>[<attribute:b view-priority="1">foo</attribute:b>bar]</container:p>'
  102. );
  103. } );
  104. it( 'should merge after inserting multiple nodes', () => {
  105. test(
  106. '<container:p><attribute:b view-priority="1">qux</attribute:b>[]baz</container:p>',
  107. [ '<attribute:b view-priority="1">foo</attribute:b>', 'bar' ],
  108. '<container:p><attribute:b view-priority="1">qux{foo</attribute:b>bar}baz</container:p>'
  109. );
  110. } );
  111. it( 'should insert text into in document fragment', () => {
  112. test(
  113. 'foo{}bar',
  114. [ 'baz' ],
  115. 'foo{baz}bar'
  116. );
  117. } );
  118. it( 'should merge same attribute nodes in document fragment', () => {
  119. test(
  120. '<attribute:b view-priority="2">foo</attribute:b>[]',
  121. [ '<attribute:b view-priority="1">bar</attribute:b>' ],
  122. '<attribute:b view-priority="2">foo</attribute:b>[<attribute:b view-priority="1">bar</attribute:b>]'
  123. );
  124. } );
  125. it( 'should insert unicode text into unicode text', () => {
  126. test(
  127. 'நி{}க்கு',
  128. [ 'லை' ],
  129. 'நி{லை}க்கு'
  130. );
  131. } );
  132. it( 'should throw when inserting Element', () => {
  133. const element = new Element( 'b' );
  134. const container = new ContainerElement( 'p' );
  135. const position = new Position( container, 0 );
  136. expect( () => {
  137. insert( position, element );
  138. } ).to.throw( CKEditorError, 'view-writer-insert-invalid-node' );
  139. } );
  140. it( 'should throw when Element is inserted as child node', () => {
  141. const element = new Element( 'b' );
  142. const root = new ContainerElement( 'p', null, element );
  143. const container = new ContainerElement( 'p' );
  144. const position = new Position( container, 0 );
  145. expect( () => {
  146. insert( position, root );
  147. } ).to.throw( CKEditorError, 'view-writer-insert-invalid-node' );
  148. } );
  149. it( 'should throw when position is not placed inside container', () => {
  150. const element = new Element( 'b' );
  151. const position = new Position( element, 0 );
  152. const attributeElement = new AttributeElement( 'i' );
  153. expect( () => {
  154. insert( position, attributeElement );
  155. } ).to.throw( CKEditorError, 'view-writer-invalid-position-container' );
  156. } );
  157. it( 'should allow to insert EmptyElement into container', () => {
  158. test(
  159. '<container:p>[]</container:p>',
  160. [ '<empty:img></empty:img>' ],
  161. '<container:p>[<empty:img></empty:img>]</container:p>'
  162. );
  163. } );
  164. it( 'should throw if trying to insert inside EmptyElement', () => {
  165. const emptyElement = new EmptyElement( 'img' );
  166. new ContainerElement( 'p', null, emptyElement ); // eslint-disable-line no-new
  167. const position = new Position( emptyElement, 0 );
  168. const attributeElement = new AttributeElement( 'i' );
  169. expect( () => {
  170. insert( position, attributeElement );
  171. } ).to.throw( CKEditorError, 'view-writer-cannot-break-empty-element' );
  172. } );
  173. it( 'should throw if trying to insert inside UIElement', () => {
  174. const uiElement = new UIElement( 'span' );
  175. new ContainerElement( 'p', null, uiElement ); // eslint-disable-line no-new
  176. const position = new Position( uiElement, 0 );
  177. const attributeElement = new AttributeElement( 'i' );
  178. expect( () => {
  179. insert( position, attributeElement );
  180. } ).to.throw( CKEditorError, 'view-writer-cannot-break-ui-element' );
  181. } );
  182. } );
  183. } );