8
0

mergeAt.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. import { mergeAt } from '/ckeditor5/engine/view/writer.js';
  7. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  8. import Text from '/ckeditor5/engine/view/text.js';
  9. import Position from '/ckeditor5/engine/view/position.js';
  10. import { stringify, parse } from '/tests/engine/_utils/view.js';
  11. describe( 'writer', () => {
  12. /**
  13. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  14. * test merge position.
  15. *
  16. * @param {String} input
  17. * @param {String} expected
  18. */
  19. function test( input, expected ) {
  20. const { view, selection } = parse( input );
  21. const newPosition = mergeAt( selection.getFirstPosition() );
  22. expect( stringify( view, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected );
  23. }
  24. describe( 'mergeAt', () => {
  25. it( 'should not merge if inside text node', () => {
  26. test( '<container:p>fo{}bar</container:p>', '<container:p>fo{}bar</container:p>' );
  27. } );
  28. it( 'should not merge if between containers', () => {
  29. test(
  30. '<container:div><container:p>foo</container:p>[]<container:p>bar</container:p></container:div>',
  31. '<container:div><container:p>foo</container:p>[]<container:p>bar</container:p></container:div>'
  32. );
  33. } );
  34. it( 'should return same position when inside empty container', () => {
  35. test(
  36. '<container:p>[]</container:p>',
  37. '<container:p>[]</container:p>'
  38. );
  39. } );
  40. it( 'should not merge when position is placed at the beginning of the container', () => {
  41. test(
  42. '<container:p>[]<attribute:b:1></attribute:b:1></container:p>',
  43. '<container:p>[]<attribute:b:1></attribute:b:1></container:p>'
  44. );
  45. } );
  46. it( 'should not merge when position is placed at the end of the container', () => {
  47. test(
  48. '<container:p><attribute:b:1></attribute:b:1>[]</container:p>',
  49. '<container:p><attribute:b:1></attribute:b:1>[]</container:p>'
  50. );
  51. } );
  52. it( 'should merge when placed between two text nodes', () => {
  53. // <p>foobar</p> -> <p>foo|bar</p>
  54. const t1 = new Text( 'foo' );
  55. const t2 = new Text( 'bar' );
  56. const p = new ContainerElement( 'p', null, [ t1, t2 ] );
  57. const position = new Position( p, 1 );
  58. const newPosition = mergeAt( position );
  59. expect( stringify( p, newPosition ) ).to.equal( '<p>foo{}bar</p>' );
  60. } );
  61. it( 'should merge when placed between similar attribute nodes', () => {
  62. test(
  63. '<container:p><attribute:b:1 foo="bar">baz</attribute:b:1>[]<attribute:b:1 foo="bar">qux</attribute:b:1></container:p>',
  64. '<container:p><attribute:b:1 foo="bar">baz{}qux</attribute:b:1></container:p>'
  65. );
  66. } );
  67. it( 'should not merge when placed between non-similar attribute nodes', () => {
  68. test(
  69. '<container:p><attribute:b:1 foo="bar"></attribute:b:1>[]<attribute:b:1 foo="baz"></attribute:b:1></container:p>',
  70. '<container:p><attribute:b:1 foo="bar"></attribute:b:1>[]<attribute:b:1 foo="baz"></attribute:b:1></container:p>'
  71. );
  72. } );
  73. it( 'should not merge when placed between similar attribute nodes with different priority', () => {
  74. test(
  75. '<container:p><attribute:b:1 foo="bar"></attribute:b:1>[]<attribute:b:2 foo="bar"></attribute:b:2></container:p>',
  76. '<container:p><attribute:b:1 foo="bar"></attribute:b:1>[]<attribute:b:2 foo="bar"></attribute:b:2></container:p>'
  77. );
  78. } );
  79. it( 'should merge attribute nodes and their contents if possible', () => {
  80. test(
  81. '<container:p><attribute:b:1 foo="bar">foo</attribute:b:1>[]<attribute:b:1 foo="bar">bar</attribute:b:1></container:p>',
  82. '<container:p><attribute:b:1 foo="bar">foo{}bar</attribute:b:1></container:p>'
  83. );
  84. } );
  85. it( 'should remove empty attributes after merge #1', () => {
  86. test(
  87. '<container:p><attribute:b>[]</attribute:b></container:p>',
  88. '<container:p>[]</container:p>'
  89. );
  90. } );
  91. it( 'should remove empty attributes after merge #2', () => {
  92. test(
  93. '<container:p><attribute:b>foo</attribute:b><attribute:i>[]</attribute:i><attribute:b>bar</attribute:b></container:p>',
  94. '<container:p><attribute:b:10>foo{}bar</attribute:b:10></container:p>'
  95. );
  96. } );
  97. it( 'should remove empty attributes after merge #3', () => {
  98. test(
  99. '<container:p><attribute:b></attribute:b><attribute:i>[]</attribute:i><attribute:b></attribute:b></container:p>',
  100. '<container:p>[]</container:p>'
  101. );
  102. } );
  103. } );
  104. } );