|
|
@@ -10,12 +10,13 @@
|
|
|
var modules = bender.amd.require(
|
|
|
'document/element',
|
|
|
'document/character',
|
|
|
- 'document/position' );
|
|
|
+ 'document/position',
|
|
|
+ 'document/document' );
|
|
|
|
|
|
describe( 'position', function() {
|
|
|
- var Element, Character;
|
|
|
+ var Element, Character, Document;
|
|
|
|
|
|
- var root, p, ul, li1, li2, f, o, z, b, a, r;
|
|
|
+ var doc, root, p, ul, li1, li2, f, o, z, b, a, r;
|
|
|
|
|
|
// root
|
|
|
// |- p Before: [ 0 ] After: [ 1 ]
|
|
|
@@ -31,8 +32,11 @@ describe( 'position', function() {
|
|
|
before( function() {
|
|
|
Element = modules[ 'document/element' ];
|
|
|
Character = modules[ 'document/character' ];
|
|
|
+ Document = modules[ 'document/document' ];
|
|
|
|
|
|
- root = new Element();
|
|
|
+ doc = new Document();
|
|
|
+
|
|
|
+ root = doc.root;
|
|
|
|
|
|
p = new Element( root, 'p' );
|
|
|
|
|
|
@@ -65,87 +69,181 @@ describe( 'position', function() {
|
|
|
li2.children.push( r );
|
|
|
} );
|
|
|
|
|
|
- it( 'should have path', function() {
|
|
|
+ it( 'should create a position with path and document', function() {
|
|
|
+ var Position = modules[ 'document/position' ];
|
|
|
+
|
|
|
+ var position = new Position( [ 0 ], doc );
|
|
|
+
|
|
|
+ expect( position ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
|
|
|
+ expect( position ).to.have.property( 'document' ).that.equals( doc );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should make positions form node and offset', function() {
|
|
|
var Position = modules[ 'document/position' ];
|
|
|
|
|
|
- expect( new Position( root, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
|
|
|
- expect( new Position( root, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
|
|
|
- expect( new Position( root, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( root, 0, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( root, 1, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( root, 2, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
|
|
|
|
|
|
- expect( new Position( p, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( p, 0, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
|
|
|
|
|
|
- expect( new Position( ul, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
|
|
|
- expect( new Position( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
|
|
|
- expect( new Position( ul, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( ul, 0, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( ul, 1, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( ul, 2, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
|
|
|
|
|
|
- expect( new Position( li1, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
|
|
|
- expect( new Position( li1, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
|
|
|
- expect( new Position( li1, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
|
|
|
- expect( new Position( li1, 3 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( li1, 0, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( li1, 1, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( li1, 2, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
|
|
|
+ expect( Position.makePositionFromParentAndOffset( li1, 3, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
|
|
|
} );
|
|
|
|
|
|
- it( 'should have nodeBefore', function() {
|
|
|
+ it( 'should make positions before elements', function() {
|
|
|
var Position = modules[ 'document/position' ];
|
|
|
|
|
|
- expect( new Position( root, 0 ) ).to.have.property( 'nodeBefore' ).that.is.null;
|
|
|
- expect( new Position( root, 1 ) ).to.have.property( 'nodeBefore' ).that.equals( p );
|
|
|
- expect( new Position( root, 2 ) ).to.have.property( 'nodeBefore' ).that.equals( ul );
|
|
|
+ expect( Position.makePositionBefore( p, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
|
|
|
+
|
|
|
+ expect( Position.makePositionBefore( ul, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
|
|
|
+
|
|
|
+ expect( Position.makePositionBefore( li1, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
|
|
|
|
|
|
- expect( new Position( p, 0 ) ).to.have.property( 'nodeBefore' ).that.is.null;
|
|
|
+ expect( Position.makePositionBefore( f, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
|
|
|
+ expect( Position.makePositionBefore( o, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
|
|
|
+ expect( Position.makePositionBefore( z, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
|
|
|
|
|
|
- expect( new Position( ul, 0 ) ).to.have.property( 'nodeBefore' ).that.is.null;
|
|
|
- expect( new Position( ul, 1 ) ).to.have.property( 'nodeBefore' ).that.equals( li1 );
|
|
|
- expect( new Position( ul, 2 ) ).to.have.property( 'nodeBefore' ).that.equals( li2 );
|
|
|
+ expect( Position.makePositionBefore( li2, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
|
|
|
|
|
|
- expect( new Position( li1, 0 ) ).to.have.property( 'nodeBefore' ).that.is.null;
|
|
|
- expect( new Position( li1, 1 ) ).to.have.property( 'nodeBefore' ).that.equals( f );
|
|
|
- expect( new Position( li1, 2 ) ).to.have.property( 'nodeBefore' ).that.equals( o );
|
|
|
- expect( new Position( li1, 3 ) ).to.have.property( 'nodeBefore' ).that.equals( z );
|
|
|
+ expect( Position.makePositionBefore( b, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 0 ] );
|
|
|
+ expect( Position.makePositionBefore( a, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
|
|
|
+ expect( Position.makePositionBefore( r, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
|
|
|
} );
|
|
|
|
|
|
- it( 'should have nodeAfter', function() {
|
|
|
+ it( 'should throw error if one try to make positions before root', function() {
|
|
|
var Position = modules[ 'document/position' ];
|
|
|
|
|
|
- expect( new Position( root, 0 ) ).to.have.property( 'nodeAfter' ).that.equals( p );
|
|
|
- expect( new Position( root, 1 ) ).to.have.property( 'nodeAfter' ).that.equals( ul );
|
|
|
- expect( new Position( root, 2 ) ).to.have.property( 'nodeAfter' ).that.is.null;
|
|
|
+ expect( function() {
|
|
|
+ Position.makePositionBefore( root, doc );
|
|
|
+ } ).to.throw( 'You can not make position before root.' );
|
|
|
+ } );
|
|
|
|
|
|
- expect( new Position( p, 0 ) ).to.have.property( 'nodeAfter' ).that.is.null;
|
|
|
+ it( 'should make positions after elements', function() {
|
|
|
+ var Position = modules[ 'document/position' ];
|
|
|
|
|
|
- expect( new Position( ul, 0 ) ).to.have.property( 'nodeAfter' ).that.equals( li1 );
|
|
|
- expect( new Position( ul, 1 ) ).to.have.property( 'nodeAfter' ).that.equals( li2 );
|
|
|
- expect( new Position( ul, 2 ) ).to.have.property( 'nodeAfter' ).that.is.null;
|
|
|
+ expect( Position.makePositionAfter( p, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
|
|
|
|
|
|
- expect( new Position( li1, 0 ) ).to.have.property( 'nodeAfter' ).that.equals( f );
|
|
|
- expect( new Position( li1, 1 ) ).to.have.property( 'nodeAfter' ).that.equals( o );
|
|
|
- expect( new Position( li1, 2 ) ).to.have.property( 'nodeAfter' ).that.equals( z );
|
|
|
- expect( new Position( li1, 3 ) ).to.have.property( 'nodeAfter' ).that.is.null;
|
|
|
+ expect( Position.makePositionAfter( ul, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
|
|
|
+
|
|
|
+ expect( Position.makePositionAfter( li1, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
|
|
|
+
|
|
|
+ expect( Position.makePositionAfter( f, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
|
|
|
+ expect( Position.makePositionAfter( o, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
|
|
|
+ expect( Position.makePositionAfter( z, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
|
|
|
+
|
|
|
+ expect( Position.makePositionAfter( li2, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
|
|
|
+
|
|
|
+ expect( Position.makePositionAfter( b, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
|
|
|
+ expect( Position.makePositionAfter( a, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
|
|
|
+ expect( Position.makePositionAfter( r, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 3 ] );
|
|
|
} );
|
|
|
|
|
|
- it( 'should equals another position with the same offset and node', function() {
|
|
|
+ it( 'should throw error if one try to make positions after root', function() {
|
|
|
var Position = modules[ 'document/position' ];
|
|
|
|
|
|
- var position = new Position( root, 0 );
|
|
|
- var samePosition = new Position( root, 0 );
|
|
|
+ expect( function() {
|
|
|
+ Position.makePositionAfter( root, doc );
|
|
|
+ } ).to.throw( 'You can not make position after root.' );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should have parent', function() {
|
|
|
+ var Position = modules[ 'document/position' ];
|
|
|
+
|
|
|
+ expect( new Position( [ 0 ], doc ) ).to.have.property( 'parent' ).that.equals( root );
|
|
|
+ expect( new Position( [ 1 ], doc ) ).to.have.property( 'parent' ).that.equals( root );
|
|
|
+ expect( new Position( [ 2 ], doc ) ).to.have.property( 'parent' ).that.equals( root );
|
|
|
+
|
|
|
+ expect( new Position( [ 0, 0 ], doc ) ).to.have.property( 'parent' ).that.equals( p );
|
|
|
+
|
|
|
+ expect( new Position( [ 1, 0 ], doc ) ).to.have.property( 'parent' ).that.equals( ul );
|
|
|
+ expect( new Position( [ 1, 1 ], doc ) ).to.have.property( 'parent' ).that.equals( ul );
|
|
|
+ expect( new Position( [ 1, 2 ], doc ) ).to.have.property( 'parent' ).that.equals( ul );
|
|
|
+
|
|
|
+ expect( new Position( [ 1, 0, 0 ], doc ) ).to.have.property( 'parent' ).that.equals( li1 );
|
|
|
+ expect( new Position( [ 1, 0, 1 ], doc ) ).to.have.property( 'parent' ).that.equals( li1 );
|
|
|
+ expect( new Position( [ 1, 0, 2 ], doc ) ).to.have.property( 'parent' ).that.equals( li1 );
|
|
|
+ expect( new Position( [ 1, 0, 3 ], doc ) ).to.have.property( 'parent' ).that.equals( li1 );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should have offset', function() {
|
|
|
+ var Position = modules[ 'document/position' ];
|
|
|
+
|
|
|
+ expect( new Position( [ 0 ], doc ) ).to.have.property( 'offset' ).that.equals( 0 );
|
|
|
+ expect( new Position( [ 1 ], doc ) ).to.have.property( 'offset' ).that.equals( 1 );
|
|
|
+ expect( new Position( [ 2 ], doc ) ).to.have.property( 'offset' ).that.equals( 2 );
|
|
|
+
|
|
|
+ expect( new Position( [ 0, 0 ], doc ) ).to.have.property( 'offset' ).that.equals( 0 );
|
|
|
+
|
|
|
+ expect( new Position( [ 1, 0 ], doc ) ).to.have.property( 'offset' ).that.equals( 0 );
|
|
|
+ expect( new Position( [ 1, 1 ], doc ) ).to.have.property( 'offset' ).that.equals( 1 );
|
|
|
+ expect( new Position( [ 1, 2 ], doc ) ).to.have.property( 'offset' ).that.equals( 2 );
|
|
|
+
|
|
|
+ expect( new Position( [ 1, 0, 0 ], doc ) ).to.have.property( 'offset' ).that.equals( 0 );
|
|
|
+ expect( new Position( [ 1, 0, 1 ], doc ) ).to.have.property( 'offset' ).that.equals( 1 );
|
|
|
+ expect( new Position( [ 1, 0, 2 ], doc ) ).to.have.property( 'offset' ).that.equals( 2 );
|
|
|
+ expect( new Position( [ 1, 0, 3 ], doc ) ).to.have.property( 'offset' ).that.equals( 3 );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should have nodeBefore', function() {
|
|
|
+ var Position = modules[ 'document/position' ];
|
|
|
+
|
|
|
+ expect( new Position( [ 0 ], doc ) ).to.have.property( 'nodeBefore' ).that.is.null;
|
|
|
+ expect( new Position( [ 1 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( p );
|
|
|
+ expect( new Position( [ 2 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( ul );
|
|
|
+
|
|
|
+ expect( new Position( [ 0, 0 ], doc ) ).to.have.property( 'nodeBefore' ).that.is.null;
|
|
|
+
|
|
|
+ expect( new Position( [ 1, 0 ], doc ) ).to.have.property( 'nodeBefore' ).that.is.null;
|
|
|
+ expect( new Position( [ 1, 1 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( li1 );
|
|
|
+ expect( new Position( [ 1, 2 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( li2 );
|
|
|
+
|
|
|
+ expect( new Position( [ 1, 0, 0 ], doc ) ).to.have.property( 'nodeBefore' ).that.is.null;
|
|
|
+ expect( new Position( [ 1, 0, 1 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( f );
|
|
|
+ expect( new Position( [ 1, 0, 2 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( o );
|
|
|
+ expect( new Position( [ 1, 0, 3 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( z );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should have nodeAfter', function() {
|
|
|
+ var Position = modules[ 'document/position' ];
|
|
|
+
|
|
|
+ expect( new Position( [ 0 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( p );
|
|
|
+ expect( new Position( [ 1 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( ul );
|
|
|
+ expect( new Position( [ 2 ], doc ) ).to.have.property( 'nodeAfter' ).that.is.null;
|
|
|
+
|
|
|
+ expect( new Position( [ 0, 0 ], doc ) ).to.have.property( 'nodeAfter' ).that.is.null;
|
|
|
+
|
|
|
+ expect( new Position( [ 1, 0 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( li1 );
|
|
|
+ expect( new Position( [ 1, 1 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( li2 );
|
|
|
+ expect( new Position( [ 1, 2 ], doc ) ).to.have.property( 'nodeAfter' ).that.is.null;
|
|
|
|
|
|
- expect( position.equals( samePosition ) ).to.be.true;
|
|
|
+ expect( new Position( [ 1, 0, 0 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( f );
|
|
|
+ expect( new Position( [ 1, 0, 1 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( o );
|
|
|
+ expect( new Position( [ 1, 0, 2 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( z );
|
|
|
+ expect( new Position( [ 1, 0, 3 ], doc ) ).to.have.property( 'nodeAfter' ).that.is.null;
|
|
|
} );
|
|
|
|
|
|
- it( 'should not equals another position with the different offset', function() {
|
|
|
+ it( 'should equals another position with the same path', function() {
|
|
|
var Position = modules[ 'document/position' ];
|
|
|
|
|
|
- var position = new Position( root, 0 );
|
|
|
- var differentOffset = new Position( root, 1 );
|
|
|
+ var position = new Position( [ 1, 1, 2 ], doc );
|
|
|
+ var samePosition = new Position( [ 1, 1, 2 ], doc );
|
|
|
|
|
|
- expect( position.equals( differentOffset ) ).to.be.false;
|
|
|
+ expect( position.isEqual( samePosition ) ).to.be.true;
|
|
|
} );
|
|
|
|
|
|
- it( 'should not equals another position with the different node', function() {
|
|
|
+ it( 'should not equals another position with the different path', function() {
|
|
|
var Position = modules[ 'document/position' ];
|
|
|
|
|
|
- var position = new Position( root, 0 );
|
|
|
- var differentNode = new Position( p, 0 );
|
|
|
+ var position = new Position( [ 1, 1, 1 ], doc );
|
|
|
+ var differentNode = new Position( [ 1, 2, 2 ], doc );
|
|
|
|
|
|
- expect( position.equals( differentNode ) ).to.be.false;
|
|
|
+ expect( position.isEqual( differentNode ) ).to.be.false;
|
|
|
} );
|
|
|
} );
|