8
0

mergeattributes.js 5.5 KB

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