| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: document */
- 'use strict';
- var modules = bender.amd.require(
- 'document/element',
- 'document/character',
- 'document/attribute',
- 'document/nodelist' );
- describe( 'tree', function() {
- var Element, Character;
- var root;
- var one, two, three;
- var charB, charA, charR, img;
- before( function() {
- Element = modules[ 'document/element' ];
- Character = modules[ 'document/character' ];
- charB = new Character( 'b' );
- charA = new Character( 'a' );
- img = new Element( 'img' );
- charR = new Character( 'r' );
- one = new Element( 'one' );
- two = new Element( 'two', null, [ charB, charA, img, charR ] );
- three = new Element( 'three' );
- root = new Element( null, null, [ one, two, three ] );
- } );
- it( 'should have proper positionInParent', function() {
- expect( root ).to.have.property( 'positionInParent' ).that.is.null;
- expect( one ).to.have.property( 'positionInParent' ).that.equals( 0 );
- expect( two ).to.have.property( 'positionInParent' ).that.equals( 1 );
- expect( three ).to.have.property( 'positionInParent' ).that.equals( 2 );
- expect( charB ).to.have.property( 'positionInParent' ).that.equals( 0 );
- expect( charA ).to.have.property( 'positionInParent' ).that.equals( 1 );
- expect( img ).to.have.property( 'positionInParent' ).that.equals( 2 );
- expect( charR ).to.have.property( 'positionInParent' ).that.equals( 3 );
- } );
- it( 'should have proper depth', function() {
- expect( root ).to.have.property( 'depth' ).that.equals( 0 );
- expect( one ).to.have.property( 'depth' ).that.equals( 1 );
- expect( two ).to.have.property( 'depth' ).that.equals( 1 );
- expect( three ).to.have.property( 'depth' ).that.equals( 1 );
- expect( charB ).to.have.property( 'depth' ).that.equals( 2 );
- expect( charA ).to.have.property( 'depth' ).that.equals( 2 );
- expect( img ).to.have.property( 'depth' ).that.equals( 2 );
- expect( charR ).to.have.property( 'depth' ).that.equals( 2 );
- } );
- it( 'should have proper root', function() {
- expect( root ).to.have.property( 'root' ).that.equals( root );
- expect( one ).to.have.property( 'root' ).that.equals( root );
- expect( two ).to.have.property( 'root' ).that.equals( root );
- expect( three ).to.have.property( 'root' ).that.equals( root );
- expect( charB ).to.have.property( 'root' ).that.equals( root );
- expect( charA ).to.have.property( 'root' ).that.equals( root );
- expect( img ).to.have.property( 'root' ).that.equals( root );
- expect( charR ).to.have.property( 'root' ).that.equals( root );
- } );
- it( 'should have proper nextSibling', function() {
- expect( root ).to.have.property( 'nextSibling' ).that.is.null;
- expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
- expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
- expect( three ).to.have.property( 'nextSibling' ).that.is.null;
- expect( charB ).to.have.property( 'nextSibling' ).that.equals( charA );
- expect( charA ).to.have.property( 'nextSibling' ).that.equals( img );
- expect( img ).to.have.property( 'nextSibling' ).that.equals( charR );
- expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
- } );
- it( 'should have proper previousSibling', function() {
- expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
- expect( one ).to.have.property( 'previousSibling' ).that.is.null;
- expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
- expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
- expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
- expect( charA ).to.have.property( 'previousSibling' ).that.equals( charB );
- expect( img ).to.have.property( 'previousSibling' ).that.equals( charA );
- expect( charR ).to.have.property( 'previousSibling' ).that.equals( img );
- } );
- } );
- describe( 'constructor', function() {
- it( 'should copy attributes, not pass by reference', function() {
- var Element = modules[ 'document/element' ];
- var Attribute = modules[ 'document/attribute' ];
- var attrs = [ new Attribute( 'attr', true ) ];
- var foo = new Element( 'foo', attrs );
- var bar = new Element( 'bar', attrs );
- foo.removeAttr( 'attr' );
- expect( foo._getAttrCount() ).to.equals( 0 );
- expect( bar._getAttrCount() ).to.equals( 1 );
- } );
- } );
- describe( 'getAttr', function() {
- it( 'should be possible to get attribute by key', function() {
- var Element = modules[ 'document/element' ];
- var Attribute = modules[ 'document/attribute' ];
- var fooAttr = new Attribute( 'foo', true );
- var element = new Element( 'foo', [ fooAttr ] );
- expect( element.getAttr( 'foo' ) ).to.equals( fooAttr.value );
- } );
- it( 'should return null if attribute was not found by key', function() {
- var Element = modules[ 'document/element' ];
- var Attribute = modules[ 'document/attribute' ];
- var fooAttr = new Attribute( 'foo', true );
- var element = new Element( 'foo', [ fooAttr ] );
- expect( element.getAttr( 'bar' ) ).to.be.null;
- } );
- } );
- describe( 'setAttr', function() {
- it( 'should insert an attribute', function() {
- var Element = modules[ 'document/element' ];
- var Attribute = modules[ 'document/attribute' ];
- var element = new Element( 'elem' );
- var attr = new Attribute( 'foo', 'bar' );
- element.setAttr( attr );
- expect( element._getAttrCount() ).to.equals( 1 );
- expect( element.getAttr( attr.key ) ).to.equals( attr.value );
- } );
- it( 'should overwrite attribute with the same key', function() {
- var Element = modules[ 'document/element' ];
- var Attribute = modules[ 'document/attribute' ];
- var oldAttr = new Attribute( 'foo', 'bar' );
- var newAttr = new Attribute( 'foo', 'bar' );
- var element = new Element( 'elem', [ oldAttr ] );
- element.setAttr( newAttr );
- expect( element._getAttrCount() ).to.equals( 1 );
- expect( element.getAttr( newAttr.key ) ).to.equals( newAttr.value );
- } );
- } );
- describe( 'removeAttr', function() {
- it( 'should remove an attribute', function() {
- var Element = modules[ 'document/element' ];
- var Attribute = modules[ 'document/attribute' ];
- var attrA = new Attribute( 'a', 'A' );
- var attrB = new Attribute( 'b', 'b' );
- var attrC = new Attribute( 'c', 'C' );
- var element = new Element( 'elem', [ attrA, attrB, attrC ] );
- element.removeAttr( attrB.key );
- expect( element._getAttrCount() ).to.equals( 2 );
- expect( element.getAttr( attrA.key ) ).to.equals( attrA.value );
- expect( element.getAttr( attrC.key ) ).to.equals( attrC.value );
- expect( element.getAttr( attrB.key ) ).to.be.null;
- } );
- } );
- describe( 'hasAttr', function() {
- it( 'should check attribute by key', function() {
- var Element = modules[ 'document/element' ];
- var Attribute = modules[ 'document/attribute' ];
- var fooAttr = new Attribute( 'foo', true );
- var element = new Element( 'foo', [ fooAttr ] );
- expect( element.hasAttr( 'foo' ) ).to.be.true;
- } );
- it( 'should return false if attribute was not found by key', function() {
- var Element = modules[ 'document/element' ];
- var Attribute = modules[ 'document/attribute' ];
- var fooAttr = new Attribute( 'foo', true );
- var element = new Element( 'foo', [ fooAttr ] );
- expect( element.hasAttr( 'bar' ) ).to.be.false;
- } );
- it( 'should check attribute by object', function() {
- var Element = modules[ 'document/element' ];
- var Attribute = modules[ 'document/attribute' ];
- var fooAttr = new Attribute( 'foo', true );
- var foo2Attr = new Attribute( 'foo', true );
- var element = new Element( 'foo', [ fooAttr ] );
- expect( element.hasAttr( foo2Attr ) ).to.be.true;
- } );
- it( 'should return false if attribute was not found by object', function() {
- var Element = modules[ 'document/element' ];
- var Attribute = modules[ 'document/attribute' ];
- var fooAttr = new Attribute( 'foo', true );
- var element = new Element( 'foo' );
- expect( element.hasAttr( fooAttr ) ).to.be.false;
- } );
- it( 'should create proper JSON string using toJSON method', function() {
- var Element = modules[ 'document/element' ];
- var Character = modules[ 'document/character' ];
- var b = new Character( 'b' );
- var foo = new Element( 'foo', [], [ b ] );
- var parsedFoo = JSON.parse( JSON.stringify( foo ) );
- var parsedBar = JSON.parse( JSON.stringify( b ) );
- expect( parsedFoo.parent ).to.be.equals( null );
- expect( parsedBar.parent ).to.be.equals( 'foo' );
- } );
- } );
|