8
0

mergeattributes.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, document;
  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 test( 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. document = new Document();
  26. writer = new DowncastWriter( document );
  27. } );
  28. it( 'should not merge if inside text node', () => {
  29. test( '<container:p>fo{}bar</container:p>', '<container:p>fo{}bar</container:p>' );
  30. } );
  31. it( 'should not merge if between containers', () => {
  32. test(
  33. '<container:div><container:p>foo</container:p>[]<container:p>bar</container:p></container:div>',
  34. '<container:div><container:p>foo</container:p>[]<container:p>bar</container:p></container:div>'
  35. );
  36. } );
  37. it( 'should return same position when inside empty container', () => {
  38. test(
  39. '<container:p>[]</container:p>',
  40. '<container:p>[]</container:p>'
  41. );
  42. } );
  43. it( 'should not merge when position is placed at the beginning of the container', () => {
  44. test(
  45. '<container:p>[]<attribute:b view-priority="1"></attribute:b></container:p>',
  46. '<container:p>[]<attribute:b view-priority="1"></attribute:b></container:p>'
  47. );
  48. } );
  49. it( 'should not merge when position is placed at the end of the container', () => {
  50. test(
  51. '<container:p><attribute:b view-priority="1"></attribute:b>[]</container:p>',
  52. '<container:p><attribute:b view-priority="1"></attribute:b>[]</container:p>'
  53. );
  54. } );
  55. it( 'should merge when placed between two text nodes', () => {
  56. // <p>foobar</p> -> <p>foo|bar</p>
  57. const t1 = new Text( document, 'foo' );
  58. const t2 = new Text( document, 'bar' );
  59. const p = new ContainerElement( document, 'p', null, [ t1, t2 ] );
  60. const position = new Position( p, 1 );
  61. const newPosition = writer.mergeAttributes( position );
  62. expect( stringify( p, newPosition ) ).to.equal( '<p>foo{}bar</p>' );
  63. } );
  64. it( 'should merge when placed between similar attribute nodes', () => {
  65. test(
  66. '<container:p>' +
  67. '<attribute:b view-priority="1" foo="bar">baz</attribute:b>[]' +
  68. '<attribute:b view-priority="1" foo="bar">qux</attribute:b>' +
  69. '</container:p>',
  70. '<container:p><attribute:b view-priority="1" foo="bar">baz{}qux</attribute:b></container:p>'
  71. );
  72. } );
  73. it( 'should not merge when placed between non-similar attribute nodes', () => {
  74. test(
  75. '<container:p>' +
  76. '<attribute:b view-priority="1" foo="bar"></attribute:b>[]<attribute:b view-priority="1" foo="baz"></attribute:b>' +
  77. '</container:p>',
  78. '<container:p>' +
  79. '<attribute:b view-priority="1" foo="bar"></attribute:b>[]<attribute:b view-priority="1" foo="baz"></attribute:b>' +
  80. '</container:p>'
  81. );
  82. } );
  83. it( 'should not merge when placed between similar attribute nodes with different priority', () => {
  84. test(
  85. '<container:p>' +
  86. '<attribute:b view-priority="1" foo="bar"></attribute:b>[]<attribute:b view-priority="2" foo="bar"></attribute:b>' +
  87. '</container:p>',
  88. '<container:p>' +
  89. '<attribute:b view-priority="1" foo="bar"></attribute:b>[]<attribute:b view-priority="2" foo="bar"></attribute:b>' +
  90. '</container:p>'
  91. );
  92. } );
  93. it( 'should merge attribute nodes and their contents if possible', () => {
  94. test(
  95. '<container:p>' +
  96. '<attribute:b view-priority="1" foo="bar">foo</attribute:b>[]' +
  97. '<attribute:b view-priority="1" foo="bar">bar</attribute:b>' +
  98. '</container:p>',
  99. '<container:p><attribute:b view-priority="1" foo="bar">foo{}bar</attribute:b></container:p>'
  100. );
  101. } );
  102. it( 'should remove empty attributes after merge #1', () => {
  103. test(
  104. '<container:p><attribute:b>[]</attribute:b></container:p>',
  105. '<container:p>[]</container:p>'
  106. );
  107. } );
  108. it( 'should remove empty attributes after merge #2', () => {
  109. test(
  110. '<container:p><attribute:b>foo</attribute:b><attribute:i>[]</attribute:i><attribute:b>bar</attribute:b></container:p>',
  111. '<container:p><attribute:b view-priority="10">foo{}bar</attribute:b></container:p>'
  112. );
  113. } );
  114. it( 'should remove empty attributes after merge #3', () => {
  115. test(
  116. '<container:p><attribute:b></attribute:b><attribute:i>[]</attribute:i><attribute:b></attribute:b></container:p>',
  117. '<container:p>[]</container:p>'
  118. );
  119. } );
  120. it( 'should not merge when placed between EmptyElements', () => {
  121. test(
  122. '<container:p><empty:img></empty:img>[]<empty:img></empty:img></container:p>',
  123. '<container:p><empty:img></empty:img>[]<empty:img></empty:img></container:p>'
  124. );
  125. } );
  126. it( 'should not merge when placed between UIElements', () => {
  127. test(
  128. '<container:p><ui:span></ui:span>[]<ui:span></ui:span></container:p>',
  129. '<container:p><ui:span></ui:span>[]<ui:span></ui:span></container:p>'
  130. );
  131. } );
  132. } );
  133. } );