insert.js 4.3 KB

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