insert.js 5.3 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. 'use strict';
  7. import { insert } from '/ckeditor5/engine/view/writer.js';
  8. import DocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  9. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  10. import Element from '/ckeditor5/engine/view/element.js';
  11. import Position from '/ckeditor5/engine/view/position.js';
  12. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  13. import { stringify, parse } from '/tests/engine/_utils/view.js';
  14. import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  15. import Text from '/ckeditor5/engine/view/text.js';
  16. describe( 'writer', () => {
  17. /**
  18. * Executes test using `parse` and `stringify` utils functions.
  19. *
  20. * @param {String} input
  21. * @param {Array.<String>} nodesToInsert
  22. * @param {String} expected
  23. */
  24. function test( input, nodesToInsert, expected ) {
  25. nodesToInsert = nodesToInsert.map( node => parse( node ) );
  26. let { view, selection } = parse( input );
  27. if ( view instanceof AttributeElement || view instanceof Text ) {
  28. view = new DocumentFragment( view );
  29. }
  30. const newRange = insert( selection.getFirstPosition(), nodesToInsert );
  31. expect( stringify( view, newRange, { showType: true, showPriority: true } ) ).to.equal( expected );
  32. }
  33. describe( 'insert', () => {
  34. it( 'should return collapsed range in insertion position when using empty array', () => {
  35. test(
  36. '<container:p>foo{}bar</container:p>',
  37. [],
  38. '<container:p>foo{}bar</container:p>'
  39. );
  40. } );
  41. it( 'should insert text into another text node #1', () => {
  42. test(
  43. '<container:p>foo{}bar</container:p>',
  44. [ 'baz' ],
  45. '<container:p>foo{baz}bar</container:p>'
  46. );
  47. } );
  48. it( 'should insert text into another text node #2', () => {
  49. test(
  50. '<container:p>foobar{}</container:p>',
  51. [ 'baz' ],
  52. '<container:p>foobar{baz]</container:p>'
  53. );
  54. } );
  55. it( 'should insert text into another text node #3', () => {
  56. test(
  57. '<container:p>{}foobar</container:p>',
  58. [ 'baz' ],
  59. '<container:p>[baz}foobar</container:p>'
  60. );
  61. } );
  62. it( 'should break attributes when inserting into text node', () => {
  63. test(
  64. '<container:p>foo{}bar</container:p>',
  65. [ '<attribute:b:1>baz</attribute:b:1>' ],
  66. '<container:p>foo[<attribute:b:1>baz</attribute:b:1>]bar</container:p>'
  67. );
  68. } );
  69. it( 'should merge text nodes', () => {
  70. test(
  71. '<container:p>[]foobar</container:p>',
  72. [ 'baz' ],
  73. '<container:p>[baz}foobar</container:p>'
  74. );
  75. } );
  76. it( 'should merge same attribute nodes', () => {
  77. test(
  78. '<container:p><attribute:b:1>foo{}bar</attribute:b:1></container:p>',
  79. [ '<attribute:b:1>baz</attribute:b:1>' ],
  80. '<container:p><attribute:b:1>foo{baz}bar</attribute:b:1></container:p>'
  81. );
  82. } );
  83. it( 'should not merge different attributes', () => {
  84. test(
  85. '<container:p><attribute:b:1>foo{}bar</attribute:b:1></container:p>',
  86. [ '<attribute:b:2>baz</attribute:b:2>' ],
  87. '<container:p>' +
  88. '<attribute:b:1>' +
  89. 'foo' +
  90. '</attribute:b:1>' +
  91. '[' +
  92. '<attribute:b:2>' +
  93. 'baz' +
  94. '</attribute:b:2>' +
  95. ']' +
  96. '<attribute:b:1>' +
  97. 'bar' +
  98. '</attribute:b:1>' +
  99. '</container:p>'
  100. );
  101. } );
  102. it( 'should allow to insert multiple nodes', () => {
  103. test(
  104. '<container:p>[]</container:p>',
  105. [ '<attribute:b:1>foo</attribute:b:1>', 'bar' ],
  106. '<container:p>[<attribute:b:1>foo</attribute:b:1>bar]</container:p>'
  107. );
  108. } );
  109. it( 'should merge after inserting multiple nodes', () => {
  110. test(
  111. '<container:p><attribute:b:1>qux</attribute:b:1>[]baz</container:p>',
  112. [ '<attribute:b:1>foo</attribute:b:1>', 'bar' ],
  113. '<container:p><attribute:b:1>qux{foo</attribute:b:1>bar}baz</container:p>'
  114. );
  115. } );
  116. it( 'should insert text into in document fragment', () => {
  117. test(
  118. 'foo{}bar',
  119. [ 'baz' ],
  120. 'foo{baz}bar'
  121. );
  122. } );
  123. it( 'should merge same attribute nodes in document fragment', () => {
  124. test(
  125. '<attribute:b:2>foo</attribute:b:2>[]',
  126. [ '<attribute:b:1>bar</attribute:b:1>' ],
  127. '<attribute:b:2>foo</attribute:b:2>[<attribute:b:1>bar</attribute:b:1>]'
  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.throw( CKEditorError, '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.throw( CKEditorError, '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.throw( CKEditorError, 'view-writer-invalid-position-container' );
  154. } );
  155. } );
  156. } );