position.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document */
  6. 'use strict';
  7. var modules = bender.amd.require(
  8. 'document/element',
  9. 'document/character',
  10. 'document/position',
  11. 'document/document' );
  12. describe( 'position', function() {
  13. var Element, Character, Document;
  14. var doc, root, p, ul, li1, li2, f, o, z, b, a, r;
  15. // root
  16. // |- p Before: [ 0 ] After: [ 1 ]
  17. // |- ul Before: [ 1 ] After: [ 2 ]
  18. // |- li Before: [ 1, 0 ] After: [ 1, 1 ]
  19. // | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
  20. // | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
  21. // | |- z Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
  22. // |- li Before: [ 1, 1 ] After: [ 1, 2 ]
  23. // |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
  24. // |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
  25. // |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
  26. before( function() {
  27. Element = modules[ 'document/element' ];
  28. Character = modules[ 'document/character' ];
  29. Document = modules[ 'document/document' ];
  30. doc = new Document();
  31. root = doc.root;
  32. p = new Element( root, 'p' );
  33. ul = new Element( root, 'ul' );
  34. li1 = new Element( ul, 'li' );
  35. f = new Character( li1, 'f' );
  36. o = new Character( li1, 'o' );
  37. z = new Character( li1, 'z' );
  38. li2 = new Element( ul, 'li' );
  39. b = new Character( li2, 'b' );
  40. a = new Character( li2, 'a' );
  41. r = new Character( li2, 'r' );
  42. root.children.push( p );
  43. root.children.push( ul );
  44. ul.children.push( li1 );
  45. ul.children.push( li2 );
  46. li1.children.push( f );
  47. li1.children.push( o );
  48. li1.children.push( z );
  49. li2.children.push( b );
  50. li2.children.push( a );
  51. li2.children.push( r );
  52. } );
  53. it( 'should create a position with path and document', function() {
  54. var Position = modules[ 'document/position' ];
  55. var position = new Position( [ 0 ], doc );
  56. expect( position ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  57. expect( position ).to.have.property( 'document' ).that.equals( doc );
  58. } );
  59. it( 'should make positions form node and offset', function() {
  60. var Position = modules[ 'document/position' ];
  61. expect( Position.makePositionFromParentAndOffset( root, 0, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  62. expect( Position.makePositionFromParentAndOffset( root, 1, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  63. expect( Position.makePositionFromParentAndOffset( root, 2, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  64. expect( Position.makePositionFromParentAndOffset( p, 0, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
  65. expect( Position.makePositionFromParentAndOffset( ul, 0, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  66. expect( Position.makePositionFromParentAndOffset( ul, 1, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  67. expect( Position.makePositionFromParentAndOffset( ul, 2, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  68. expect( Position.makePositionFromParentAndOffset( li1, 0, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  69. expect( Position.makePositionFromParentAndOffset( li1, 1, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  70. expect( Position.makePositionFromParentAndOffset( li1, 2, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  71. expect( Position.makePositionFromParentAndOffset( li1, 3, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  72. } );
  73. it( 'should make positions before elements', function() {
  74. var Position = modules[ 'document/position' ];
  75. expect( Position.makePositionBefore( p, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  76. expect( Position.makePositionBefore( ul, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  77. expect( Position.makePositionBefore( li1, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  78. expect( Position.makePositionBefore( f, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  79. expect( Position.makePositionBefore( o, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  80. expect( Position.makePositionBefore( z, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  81. expect( Position.makePositionBefore( li2, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  82. expect( Position.makePositionBefore( b, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 0 ] );
  83. expect( Position.makePositionBefore( a, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  84. expect( Position.makePositionBefore( r, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  85. } );
  86. it( 'should throw error if one try to make positions before root', function() {
  87. var Position = modules[ 'document/position' ];
  88. expect( function() {
  89. Position.makePositionBefore( root, doc );
  90. } ).to.throw( 'You can not make position before root.' );
  91. } );
  92. it( 'should make positions after elements', function() {
  93. var Position = modules[ 'document/position' ];
  94. expect( Position.makePositionAfter( p, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  95. expect( Position.makePositionAfter( ul, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  96. expect( Position.makePositionAfter( li1, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  97. expect( Position.makePositionAfter( f, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  98. expect( Position.makePositionAfter( o, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  99. expect( Position.makePositionAfter( z, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  100. expect( Position.makePositionAfter( li2, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  101. expect( Position.makePositionAfter( b, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  102. expect( Position.makePositionAfter( a, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  103. expect( Position.makePositionAfter( r, doc ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 3 ] );
  104. } );
  105. it( 'should throw error if one try to make positions after root', function() {
  106. var Position = modules[ 'document/position' ];
  107. expect( function() {
  108. Position.makePositionAfter( root, doc );
  109. } ).to.throw( 'You can not make position after root.' );
  110. } );
  111. it( 'should have parent', function() {
  112. var Position = modules[ 'document/position' ];
  113. expect( new Position( [ 0 ], doc ) ).to.have.property( 'parent' ).that.equals( root );
  114. expect( new Position( [ 1 ], doc ) ).to.have.property( 'parent' ).that.equals( root );
  115. expect( new Position( [ 2 ], doc ) ).to.have.property( 'parent' ).that.equals( root );
  116. expect( new Position( [ 0, 0 ], doc ) ).to.have.property( 'parent' ).that.equals( p );
  117. expect( new Position( [ 1, 0 ], doc ) ).to.have.property( 'parent' ).that.equals( ul );
  118. expect( new Position( [ 1, 1 ], doc ) ).to.have.property( 'parent' ).that.equals( ul );
  119. expect( new Position( [ 1, 2 ], doc ) ).to.have.property( 'parent' ).that.equals( ul );
  120. expect( new Position( [ 1, 0, 0 ], doc ) ).to.have.property( 'parent' ).that.equals( li1 );
  121. expect( new Position( [ 1, 0, 1 ], doc ) ).to.have.property( 'parent' ).that.equals( li1 );
  122. expect( new Position( [ 1, 0, 2 ], doc ) ).to.have.property( 'parent' ).that.equals( li1 );
  123. expect( new Position( [ 1, 0, 3 ], doc ) ).to.have.property( 'parent' ).that.equals( li1 );
  124. } );
  125. it( 'should have offset', function() {
  126. var Position = modules[ 'document/position' ];
  127. expect( new Position( [ 0 ], doc ) ).to.have.property( 'offset' ).that.equals( 0 );
  128. expect( new Position( [ 1 ], doc ) ).to.have.property( 'offset' ).that.equals( 1 );
  129. expect( new Position( [ 2 ], doc ) ).to.have.property( 'offset' ).that.equals( 2 );
  130. expect( new Position( [ 0, 0 ], doc ) ).to.have.property( 'offset' ).that.equals( 0 );
  131. expect( new Position( [ 1, 0 ], doc ) ).to.have.property( 'offset' ).that.equals( 0 );
  132. expect( new Position( [ 1, 1 ], doc ) ).to.have.property( 'offset' ).that.equals( 1 );
  133. expect( new Position( [ 1, 2 ], doc ) ).to.have.property( 'offset' ).that.equals( 2 );
  134. expect( new Position( [ 1, 0, 0 ], doc ) ).to.have.property( 'offset' ).that.equals( 0 );
  135. expect( new Position( [ 1, 0, 1 ], doc ) ).to.have.property( 'offset' ).that.equals( 1 );
  136. expect( new Position( [ 1, 0, 2 ], doc ) ).to.have.property( 'offset' ).that.equals( 2 );
  137. expect( new Position( [ 1, 0, 3 ], doc ) ).to.have.property( 'offset' ).that.equals( 3 );
  138. } );
  139. it( 'should have nodeBefore', function() {
  140. var Position = modules[ 'document/position' ];
  141. expect( new Position( [ 0 ], doc ) ).to.have.property( 'nodeBefore' ).that.is.null;
  142. expect( new Position( [ 1 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( p );
  143. expect( new Position( [ 2 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( ul );
  144. expect( new Position( [ 0, 0 ], doc ) ).to.have.property( 'nodeBefore' ).that.is.null;
  145. expect( new Position( [ 1, 0 ], doc ) ).to.have.property( 'nodeBefore' ).that.is.null;
  146. expect( new Position( [ 1, 1 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( li1 );
  147. expect( new Position( [ 1, 2 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( li2 );
  148. expect( new Position( [ 1, 0, 0 ], doc ) ).to.have.property( 'nodeBefore' ).that.is.null;
  149. expect( new Position( [ 1, 0, 1 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( f );
  150. expect( new Position( [ 1, 0, 2 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( o );
  151. expect( new Position( [ 1, 0, 3 ], doc ) ).to.have.property( 'nodeBefore' ).that.equals( z );
  152. } );
  153. it( 'should have nodeAfter', function() {
  154. var Position = modules[ 'document/position' ];
  155. expect( new Position( [ 0 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( p );
  156. expect( new Position( [ 1 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( ul );
  157. expect( new Position( [ 2 ], doc ) ).to.have.property( 'nodeAfter' ).that.is.null;
  158. expect( new Position( [ 0, 0 ], doc ) ).to.have.property( 'nodeAfter' ).that.is.null;
  159. expect( new Position( [ 1, 0 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( li1 );
  160. expect( new Position( [ 1, 1 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( li2 );
  161. expect( new Position( [ 1, 2 ], doc ) ).to.have.property( 'nodeAfter' ).that.is.null;
  162. expect( new Position( [ 1, 0, 0 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( f );
  163. expect( new Position( [ 1, 0, 1 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( o );
  164. expect( new Position( [ 1, 0, 2 ], doc ) ).to.have.property( 'nodeAfter' ).that.equals( z );
  165. expect( new Position( [ 1, 0, 3 ], doc ) ).to.have.property( 'nodeAfter' ).that.is.null;
  166. } );
  167. it( 'should equals another position with the same path', function() {
  168. var Position = modules[ 'document/position' ];
  169. var position = new Position( [ 1, 1, 2 ], doc );
  170. var samePosition = new Position( [ 1, 1, 2 ], doc );
  171. expect( position.isEqual( samePosition ) ).to.be.true;
  172. } );
  173. it( 'should not equals another position with the different path', function() {
  174. var Position = modules[ 'document/position' ];
  175. var position = new Position( [ 1, 1, 1 ], doc );
  176. var differentNode = new Position( [ 1, 2, 2 ], doc );
  177. expect( position.isEqual( differentNode ) ).to.be.false;
  178. } );
  179. } );