mergeattributes.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Writer from '/ckeditor5/engine/treeview/writer.js';
  8. import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
  9. import Text from '/ckeditor5/engine/treeview/text.js';
  10. import Position from '/ckeditor5/engine/treeview/position.js';
  11. import { stringify, parse } from '/tests/engine/_utils/view.js';
  12. describe( 'Writer', () => {
  13. let writer;
  14. /**
  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. */
  21. function test( input, expected ) {
  22. const { view, selection } = parse( input );
  23. const newPosition = writer.mergeAttributes( selection.getFirstPosition() );
  24. expect( stringify( view, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected );
  25. }
  26. beforeEach( () => {
  27. writer = new Writer();
  28. } );
  29. describe( 'mergeAttributes', () => {
  30. it( 'should not merge if inside text node', () => {
  31. test( '<container:p>fo{}bar</container:p>', '<container:p>fo{}bar</container:p>' );
  32. } );
  33. it( 'should not merge if between containers', () => {
  34. test(
  35. '<container:div><container:p>foo</container:p>[]<container:p>bar</container:p></container:div>',
  36. '<container:div><container:p>foo</container:p>[]<container:p>bar</container:p></container:div>'
  37. );
  38. } );
  39. it( 'should return same position when inside empty container', () => {
  40. test(
  41. '<container:p>[]</container:p>',
  42. '<container:p>[]</container:p>'
  43. );
  44. } );
  45. it( 'should not merge when position is placed at the beginning of the container', () => {
  46. test(
  47. '<container:p>[]<attribute:b:1></attribute:b:1></container:p>',
  48. '<container:p>[]<attribute:b:1></attribute:b:1></container:p>'
  49. );
  50. } );
  51. it( 'should not merge when position is placed at the end of the container', () => {
  52. test(
  53. '<container:p><attribute:b:1></attribute:b:1>[]</container:p>',
  54. '<container:p><attribute:b:1></attribute:b:1>[]</container:p>'
  55. );
  56. } );
  57. it( 'should merge when placed between two text nodes', () => {
  58. // <p>foobar</p> -> <p>foo|bar</p>
  59. const t1 = new Text( 'foo' );
  60. const t2 = new Text( 'bar' );
  61. const p = new ContainerElement( 'p', null, [ t1, t2 ] );
  62. const position = new Position( p, 1 );
  63. const newPosition = writer.mergeAttributes( position );
  64. expect( stringify( p, newPosition ) ).to.equal( '<p>foo{}bar</p>' );
  65. } );
  66. it( 'should merge when placed between similar attribute nodes', () => {
  67. test(
  68. '<container:p><attribute:b:1 foo="bar"></attribute:b:1>[]<attribute:b:1 foo="bar"></attribute:b:1></container:p>',
  69. '<container:p><attribute:b:1 foo="bar">[]</attribute:b:1></container:p>'
  70. );
  71. } );
  72. it( 'should not merge when placed between non-similar attribute nodes', () => {
  73. test(
  74. '<container:p><attribute:b:1 foo="bar"></attribute:b:1>[]<attribute:b:1 foo="baz"></attribute:b:1></container:p>',
  75. '<container:p><attribute:b:1 foo="bar"></attribute:b:1>[]<attribute:b:1 foo="baz"></attribute:b:1></container:p>'
  76. );
  77. } );
  78. it( 'should not merge when placed between similar attribute nodes with different priority', () => {
  79. test(
  80. '<container:p><attribute:b:1 foo="bar"></attribute:b:1>[]<attribute:b:2 foo="bar"></attribute:b:2></container:p>',
  81. '<container:p><attribute:b:1 foo="bar"></attribute:b:1>[]<attribute:b:2 foo="bar"></attribute:b:2></container:p>'
  82. );
  83. } );
  84. it( 'should merge attribute nodes and their contents if possible', () => {
  85. test(
  86. '<container:p><attribute:b:1 foo="bar">foo</attribute:b:1>[]<attribute:b:1 foo="bar">bar</attribute:b:1></container:p>',
  87. '<container:p><attribute:b:1 foo="bar">foo{}bar</attribute:b:1></container:p>'
  88. );
  89. } );
  90. } );
  91. } );