mergeattributes.js 5.7 KB

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