insert.js 7.2 KB

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