8
0

characterproxy.js 4.7 KB

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