position.js 11 KB

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