8
0

mergeattributes.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { mergeAttributes } from '../../../src/view/writer';
  6. import ContainerElement from '../../../src/view/containerelement';
  7. import Text from '../../../src/view/text';
  8. import Position from '../../../src/view/position';
  9. import { stringify, parse } from '../../../src/dev-utils/view';
  10. describe( 'writer', () => {
  11. /**
  12. * Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
  13. * test merge position.
  14. *
  15. * @param {String} input
  16. * @param {String} expected
  17. */
  18. function test( input, expected ) {
  19. const { view, selection } = parse( input );
  20. const newPosition = mergeAttributes( selection.getFirstPosition() );
  21. expect( stringify( view, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected );
  22. }
  23. describe( 'mergeAttributes', () => {
  24. it( 'should not merge if inside text node', () => {
  25. test( '<container:p>fo{}bar</container:p>', '<container:p>fo{}bar</container:p>' );
  26. } );
  27. it( 'should not merge if between containers', () => {
  28. test(
  29. '<container:div><container:p>foo</container:p>[]<container:p>bar</container:p></container:div>',
  30. '<container:div><container:p>foo</container:p>[]<container:p>bar</container:p></container:div>'
  31. );
  32. } );
  33. it( 'should return same position when inside empty container', () => {
  34. test(
  35. '<container:p>[]</container:p>',
  36. '<container:p>[]</container:p>'
  37. );
  38. } );
  39. it( 'should not merge when position is placed at the beginning of the container', () => {
  40. test(
  41. '<container:p>[]<attribute:b view-priority="1"></attribute:b></container:p>',
  42. '<container:p>[]<attribute:b view-priority="1"></attribute:b></container:p>'
  43. );
  44. } );
  45. it( 'should not merge when position is placed at the end of the container', () => {
  46. test(
  47. '<container:p><attribute:b view-priority="1"></attribute:b>[]</container:p>',
  48. '<container:p><attribute:b view-priority="1"></attribute:b>[]</container:p>'
  49. );
  50. } );
  51. it( 'should merge when placed between two text nodes', () => {
  52. // <p>foobar</p> -> <p>foo|bar</p>
  53. const t1 = new Text( 'foo' );
  54. const t2 = new Text( 'bar' );
  55. const p = new ContainerElement( 'p', null, [ t1, t2 ] );
  56. const position = new Position( p, 1 );
  57. const newPosition = mergeAttributes( position );
  58. expect( stringify( p, newPosition ) ).to.equal( '<p>foo{}bar</p>' );
  59. } );
  60. it( 'should merge when placed between similar attribute nodes', () => {
  61. test(
  62. '<container:p>' +
  63. '<attribute:b view-priority="1" foo="bar">baz</attribute:b>[]' +
  64. '<attribute:b view-priority="1" foo="bar">qux</attribute:b>' +
  65. '</container:p>',
  66. '<container:p><attribute:b view-priority="1" foo="bar">baz{}qux</attribute:b></container:p>'
  67. );
  68. } );
  69. it( 'should not merge when placed between non-similar attribute nodes', () => {
  70. test(
  71. '<container:p>' +
  72. '<attribute:b view-priority="1" foo="bar"></attribute:b>[]<attribute:b view-priority="1" foo="baz"></attribute:b>' +
  73. '</container:p>',
  74. '<container:p>' +
  75. '<attribute:b view-priority="1" foo="bar"></attribute:b>[]<attribute:b view-priority="1" foo="baz"></attribute:b>' +
  76. '</container:p>'
  77. );
  78. } );
  79. it( 'should not merge when placed between similar attribute nodes with different priority', () => {
  80. test(
  81. '<container:p>' +
  82. '<attribute:b view-priority="1" foo="bar"></attribute:b>[]<attribute:b view-priority="2" foo="bar"></attribute:b>' +
  83. '</container:p>',
  84. '<container:p>' +
  85. '<attribute:b view-priority="1" foo="bar"></attribute:b>[]<attribute:b view-priority="2" foo="bar"></attribute:b>' +
  86. '</container:p>'
  87. );
  88. } );
  89. it( 'should merge attribute nodes and their contents if possible', () => {
  90. test(
  91. '<container:p>' +
  92. '<attribute:b view-priority="1" foo="bar">foo</attribute:b>[]' +
  93. '<attribute:b view-priority="1" foo="bar">bar</attribute:b>' +
  94. '</container:p>',
  95. '<container:p><attribute:b view-priority="1" foo="bar">foo{}bar</attribute:b></container:p>'
  96. );
  97. } );
  98. it( 'should remove empty attributes after merge #1', () => {
  99. test(
  100. '<container:p><attribute:b>[]</attribute:b></container:p>',
  101. '<container:p>[]</container:p>'
  102. );
  103. } );
  104. it( 'should remove empty attributes after merge #2', () => {
  105. test(
  106. '<container:p><attribute:b>foo</attribute:b><attribute:i>[]</attribute:i><attribute:b>bar</attribute:b></container:p>',
  107. '<container:p><attribute:b view-priority="10">foo{}bar</attribute:b></container:p>'
  108. );
  109. } );
  110. it( 'should remove empty attributes after merge #3', () => {
  111. test(
  112. '<container:p><attribute:b></attribute:b><attribute:i>[]</attribute:i><attribute:b></attribute:b></container:p>',
  113. '<container:p>[]</container:p>'
  114. );
  115. } );
  116. it( 'should not merge when placed between EmptyElements', () => {
  117. test(
  118. '<container:p><empty:img></empty:img>[]<empty:img></empty:img></container:p>',
  119. '<container:p><empty:img></empty:img>[]<empty:img></empty:img></container:p>'
  120. );
  121. } );
  122. it( 'should not merge when placed between UIElements', () => {
  123. test(
  124. '<container:p><ui:span></ui:span>[]<ui:span></ui:span></container:p>',
  125. '<container:p><ui:span></ui:span>[]<ui:span></ui:span></container:p>'
  126. );
  127. } );
  128. } );
  129. } );