insert.js 7.2 KB

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