8
0

insert.js 6.7 KB

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