insert.js 3.3 KB

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