insert.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view, browser-only */
  6. import { insert } from '/ckeditor5/engine/view/writer.js';
  7. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  8. import Element from '/ckeditor5/engine/view/element.js';
  9. import Position from '/ckeditor5/engine/view/position.js';
  10. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  11. import { stringify, parse } from '/tests/engine/_utils/view.js';
  12. import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  13. describe( 'writer', () => {
  14. /**
  15. * Executes test using `parse` and `stringify` utils functions.
  16. *
  17. * @param {String} input
  18. * @param {Array.<String>} nodesToInsert
  19. * @param {String} expected
  20. */
  21. function test( input, nodesToInsert, expected ) {
  22. nodesToInsert = nodesToInsert.map( node => parse( node ) );
  23. let { view, selection } = parse( input );
  24. const newRange = insert( selection.getFirstPosition(), nodesToInsert );
  25. expect( stringify( view.root, newRange, { showType: true, showPriority: true } ) ).to.equal( expected );
  26. }
  27. describe( 'insert', () => {
  28. it( 'should return collapsed range in insertion position when using empty array', () => {
  29. test(
  30. '<container:p>foo{}bar</container:p>',
  31. [],
  32. '<container:p>foo{}bar</container:p>'
  33. );
  34. } );
  35. it( 'should insert text into another text node #1', () => {
  36. test(
  37. '<container:p>foo{}bar</container:p>',
  38. [ 'baz' ],
  39. '<container:p>foo{baz}bar</container:p>'
  40. );
  41. } );
  42. it( 'should insert text into another text node #2', () => {
  43. test(
  44. '<container:p>foobar{}</container:p>',
  45. [ 'baz' ],
  46. '<container:p>foobar{baz]</container:p>'
  47. );
  48. } );
  49. it( 'should insert text into another text node #3', () => {
  50. test(
  51. '<container:p>{}foobar</container:p>',
  52. [ 'baz' ],
  53. '<container:p>[baz}foobar</container:p>'
  54. );
  55. } );
  56. it( 'should break attributes when inserting into text node', () => {
  57. test(
  58. '<container:p>foo{}bar</container:p>',
  59. [ '<attribute:b:1>baz</attribute:b:1>' ],
  60. '<container:p>foo[<attribute:b:1>baz</attribute:b:1>]bar</container:p>'
  61. );
  62. } );
  63. it( 'should merge text nodes', () => {
  64. test(
  65. '<container:p>[]foobar</container:p>',
  66. [ 'baz' ],
  67. '<container:p>[baz}foobar</container:p>'
  68. );
  69. } );
  70. it( 'should merge same attribute nodes', () => {
  71. test(
  72. '<container:p><attribute:b:1>foo{}bar</attribute:b:1></container:p>',
  73. [ '<attribute:b:1>baz</attribute:b:1>' ],
  74. '<container:p><attribute:b:1>foo{baz}bar</attribute:b:1></container:p>'
  75. );
  76. } );
  77. it( 'should not merge different attributes', () => {
  78. test(
  79. '<container:p><attribute:b:1>foo{}bar</attribute:b:1></container:p>',
  80. [ '<attribute:b:2>baz</attribute:b:2>' ],
  81. '<container:p>' +
  82. '<attribute:b:1>' +
  83. 'foo' +
  84. '</attribute:b:1>' +
  85. '[' +
  86. '<attribute:b:2>' +
  87. 'baz' +
  88. '</attribute:b:2>' +
  89. ']' +
  90. '<attribute:b:1>' +
  91. 'bar' +
  92. '</attribute:b:1>' +
  93. '</container:p>'
  94. );
  95. } );
  96. it( 'should allow to insert multiple nodes', () => {
  97. test(
  98. '<container:p>[]</container:p>',
  99. [ '<attribute:b:1>foo</attribute:b:1>', 'bar' ],
  100. '<container:p>[<attribute:b:1>foo</attribute:b:1>bar]</container:p>'
  101. );
  102. } );
  103. it( 'should merge after inserting multiple nodes', () => {
  104. test(
  105. '<container:p><attribute:b:1>qux</attribute:b:1>[]baz</container:p>',
  106. [ '<attribute:b:1>foo</attribute:b:1>', 'bar' ],
  107. '<container:p><attribute:b:1>qux{foo</attribute:b:1>bar}baz</container:p>'
  108. );
  109. } );
  110. it( 'should insert text into in document fragment', () => {
  111. test(
  112. 'foo{}bar',
  113. [ 'baz' ],
  114. 'foo{baz}bar'
  115. );
  116. } );
  117. it( 'should merge same attribute nodes in document fragment', () => {
  118. test(
  119. '<attribute:b:2>foo</attribute:b:2>[]',
  120. [ '<attribute:b:1>bar</attribute:b:1>' ],
  121. '<attribute:b:2>foo</attribute:b:2>[<attribute:b:1>bar</attribute:b:1>]'
  122. );
  123. } );
  124. it( 'should insert unicode text into unicode text', () => {
  125. test(
  126. 'நி{}க்கு',
  127. [ 'லை' ],
  128. 'நி{லை}க்கு'
  129. );
  130. } );
  131. it( 'should throw when inserting Element', () => {
  132. const element = new Element( 'b' );
  133. const container = new ContainerElement( 'p' );
  134. const position = new Position( container, 0 );
  135. expect( () => {
  136. insert( position, element );
  137. } ).to.throw( CKEditorError, 'view-writer-insert-invalid-node' );
  138. } );
  139. it( 'should throw when Element is inserted as child node', () => {
  140. const element = new Element( 'b' );
  141. const root = new ContainerElement( 'p', null, element );
  142. const container = new ContainerElement( 'p' );
  143. const position = new Position( container, 0 );
  144. expect( () => {
  145. insert( position, root );
  146. } ).to.throw( CKEditorError, 'view-writer-insert-invalid-node' );
  147. } );
  148. it( 'should throw when position is not placed inside container', () => {
  149. const element = new Element( 'b' );
  150. const position = new Position( element, 0 );
  151. const attributeElement = new AttributeElement( 'i' );
  152. expect( () => {
  153. insert( position, attributeElement );
  154. } ).to.throw( CKEditorError, 'view-writer-invalid-position-container' );
  155. } );
  156. } );
  157. } );