position.js 12 KB

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