insert.js 6.8 KB

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