insert.js 4.9 KB

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