insert.js 7.0 KB

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