|
|
@@ -15,12 +15,9 @@ import utils from '/ckeditor5/utils/utils.js';
|
|
|
describe( 'CharacterProxy', () => {
|
|
|
let text, element, char;
|
|
|
|
|
|
- before( () => {
|
|
|
+ beforeEach( () => {
|
|
|
text = new Text( 'abc', { foo: true } );
|
|
|
element = new Element( 'div', [], [ new Element( 'p' ), text, new Element( 'p' ) ] );
|
|
|
- } );
|
|
|
-
|
|
|
- beforeEach( () => {
|
|
|
char = element.getChild( 2 );
|
|
|
} );
|
|
|
|
|
|
@@ -43,4 +40,96 @@ describe( 'CharacterProxy', () => {
|
|
|
it( 'should return correct index in parent node', () => {
|
|
|
expect( char.getIndex() ).to.equal( 2 );
|
|
|
} );
|
|
|
+
|
|
|
+ describe( 'attributes interface', () => {
|
|
|
+ describe( 'hasAttribute', () => {
|
|
|
+ it( 'should return true if text fragment has attribute with given key', () => {
|
|
|
+ expect( char.hasAttribute( 'foo' ) ).to.be.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return false if text fragment does not have attribute with given key', () => {
|
|
|
+ expect( char.hasAttribute( 'abc' ) ).to.be.false;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'getAttribute', () => {
|
|
|
+ it( 'should return attribute with given key if text fragment has given attribute', () => {
|
|
|
+ expect( char.getAttribute( 'foo' ) ).to.be.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return undefined if text fragment does not have given attribute', () => {
|
|
|
+ expect( char.getAttribute( 'bar' ) ).to.be.undefined;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'getAttributes', () => {
|
|
|
+ it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
|
|
|
+ let attrs = Array.from( char.getAttributes() );
|
|
|
+
|
|
|
+ expect( attrs ).to.deep.equal( [ [ 'foo', true ] ] );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'setAttribute', () => {
|
|
|
+ it( 'should set attribute on given character', () => {
|
|
|
+ char.setAttribute( 'abc', 'xyz' );
|
|
|
+
|
|
|
+ expect( element.getChild( 0 ).getAttribute( 'abc' ) ).to.be.undefined;
|
|
|
+ expect( element.getChild( 1 ).getAttribute( 'abc' ) ).to.be.undefined;
|
|
|
+ expect( element.getChild( 2 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
|
|
|
+ expect( element.getChild( 3 ).getAttribute( 'abc' ) ).to.be.undefined;
|
|
|
+ expect( element.getChild( 4 ).getAttribute( 'abc' ) ).to.be.undefined;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should remove attribute when passed attribute value is null', () => {
|
|
|
+ char.setAttribute( 'foo', null );
|
|
|
+
|
|
|
+ expect( element.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
|
|
|
+ expect( element.getChild( 1 ).hasAttribute( 'foo' ) ).to.be.true;
|
|
|
+ expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
|
|
|
+ expect( element.getChild( 3 ).hasAttribute( 'foo' ) ).to.be.true;
|
|
|
+ expect( element.getChild( 4 ).hasAttribute( 'foo' ) ).to.be.false;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should correctly split and merge characters', () => {
|
|
|
+ char.setAttribute( 'abc', 'xyz' );
|
|
|
+ char.nextSibling.setAttribute( 'abc', 'xyz' );
|
|
|
+
|
|
|
+ expect( element._children._nodes.length ).to.equal( 4 );
|
|
|
+ expect( element._children._nodes[ 1 ].text ).to.equal( 'a' );
|
|
|
+ expect( element._children._nodes[ 2 ].text ).to.equal( 'bc' );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'setAttributesTo', () => {
|
|
|
+ it( 'should remove all attributes from character and set given ones', () => {
|
|
|
+ char.setAttributesTo( { abc: 'xyz' } );
|
|
|
+
|
|
|
+ expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
|
|
|
+ expect( element.getChild( 2 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'removeAttribute', () => {
|
|
|
+ it( 'should remove given attribute from character', () => {
|
|
|
+ char.removeAttribute( 'foo' );
|
|
|
+
|
|
|
+ expect( element.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
|
|
|
+ expect( element.getChild( 1 ).hasAttribute( 'foo' ) ).to.be.true;
|
|
|
+ expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
|
|
|
+ expect( element.getChild( 3 ).hasAttribute( 'foo' ) ).to.be.true;
|
|
|
+ expect( element.getChild( 4 ).hasAttribute( 'foo' ) ).to.be.false;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'clearAttributes', () => {
|
|
|
+ it( 'should remove all attributes from text fragment', () => {
|
|
|
+ char.setAttribute( 'abc', 'xyz' );
|
|
|
+ char.clearAttributes();
|
|
|
+
|
|
|
+ expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
|
|
|
+ expect( element.getChild( 2 ).hasAttribute( 'abc' ) ).to.be.false;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
} );
|