8
0

characterproxy.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Node from '/ckeditor5/engine/model/node.js';
  7. import Element from '/ckeditor5/engine/model/element.js';
  8. import Text from '/ckeditor5/engine/model/text.js';
  9. import mapsEqual from '/ckeditor5/utils/mapsequal.js';
  10. describe( 'CharacterProxy', () => {
  11. let text, element, char;
  12. beforeEach( () => {
  13. text = new Text( 'abc', { foo: true } );
  14. element = new Element( 'div', [], [ new Element( 'p' ), text, new Element( 'p' ) ] );
  15. char = element.getChild( 2 );
  16. } );
  17. it( 'should extend Node class', () => {
  18. expect( char ).to.be.instanceof( Node );
  19. } );
  20. it( 'should have correct character property', () => {
  21. expect( char ).to.have.property( 'character' ).that.equals( 'b' );
  22. } );
  23. it( 'should have correct parent property', () => {
  24. expect( char ).to.have.property( 'parent' ).that.equals( element );
  25. } );
  26. it( 'should have attributes list equal to passed to Text instance', () => {
  27. expect( mapsEqual( char._attrs, text._attrs ) ).to.be.true;
  28. } );
  29. it( 'should return correct index in parent node', () => {
  30. expect( char.getIndex() ).to.equal( 2 );
  31. } );
  32. describe( 'attributes interface', () => {
  33. describe( 'hasAttribute', () => {
  34. it( 'should return true if text fragment has attribute with given key', () => {
  35. expect( char.hasAttribute( 'foo' ) ).to.be.true;
  36. } );
  37. it( 'should return false if text fragment does not have attribute with given key', () => {
  38. expect( char.hasAttribute( 'abc' ) ).to.be.false;
  39. } );
  40. } );
  41. describe( 'getAttribute', () => {
  42. it( 'should return attribute with given key if text fragment has given attribute', () => {
  43. expect( char.getAttribute( 'foo' ) ).to.be.true;
  44. } );
  45. it( 'should return undefined if text fragment does not have given attribute', () => {
  46. expect( char.getAttribute( 'bar' ) ).to.be.undefined;
  47. } );
  48. } );
  49. describe( 'getAttributes', () => {
  50. it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
  51. let attrs = Array.from( char.getAttributes() );
  52. expect( attrs ).to.deep.equal( [ [ 'foo', true ] ] );
  53. } );
  54. } );
  55. describe( 'setAttribute', () => {
  56. it( 'should set attribute on given character', () => {
  57. char.setAttribute( 'abc', 'xyz' );
  58. expect( element.getChild( 0 ).getAttribute( 'abc' ) ).to.be.undefined;
  59. expect( element.getChild( 1 ).getAttribute( 'abc' ) ).to.be.undefined;
  60. expect( element.getChild( 2 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
  61. expect( element.getChild( 3 ).getAttribute( 'abc' ) ).to.be.undefined;
  62. expect( element.getChild( 4 ).getAttribute( 'abc' ) ).to.be.undefined;
  63. } );
  64. it( 'should remove attribute when passed attribute value is null', () => {
  65. char.setAttribute( 'foo', null );
  66. expect( element.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
  67. expect( element.getChild( 1 ).hasAttribute( 'foo' ) ).to.be.true;
  68. expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
  69. expect( element.getChild( 3 ).hasAttribute( 'foo' ) ).to.be.true;
  70. expect( element.getChild( 4 ).hasAttribute( 'foo' ) ).to.be.false;
  71. } );
  72. it( 'should correctly split and merge characters', () => {
  73. char.setAttribute( 'abc', 'xyz' );
  74. char.nextSibling.setAttribute( 'abc', 'xyz' );
  75. expect( element._children._nodes.length ).to.equal( 4 );
  76. expect( element._children._nodes[ 1 ].text ).to.equal( 'a' );
  77. expect( element._children._nodes[ 2 ].text ).to.equal( 'bc' );
  78. } );
  79. } );
  80. describe( 'setAttributesTo', () => {
  81. it( 'should remove all attributes from character and set given ones', () => {
  82. char.setAttributesTo( { abc: 'xyz' } );
  83. expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
  84. expect( element.getChild( 2 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
  85. } );
  86. } );
  87. describe( 'removeAttribute', () => {
  88. it( 'should remove given attribute from character', () => {
  89. char.removeAttribute( 'foo' );
  90. expect( element.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
  91. expect( element.getChild( 1 ).hasAttribute( 'foo' ) ).to.be.true;
  92. expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
  93. expect( element.getChild( 3 ).hasAttribute( 'foo' ) ).to.be.true;
  94. expect( element.getChild( 4 ).hasAttribute( 'foo' ) ).to.be.false;
  95. } );
  96. } );
  97. describe( 'clearAttributes', () => {
  98. it( 'should remove all attributes from text fragment', () => {
  99. char.setAttribute( 'abc', 'xyz' );
  100. char.clearAttributes();
  101. expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
  102. expect( element.getChild( 2 ).hasAttribute( 'abc' ) ).to.be.false;
  103. } );
  104. } );
  105. } );
  106. } );