insert.js 5.4 KB

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