insert.js 5.0 KB

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