position.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. const modules = bender.amd.require(
  8. 'document/element',
  9. 'document/character',
  10. 'document/position',
  11. 'document/document',
  12. 'ckeditorerror',
  13. 'document/nodelist'
  14. );
  15. describe( 'position', () => {
  16. let Element, Character, Document, NodeList, Position, CKEditorError;
  17. let doc, root, otherRoot, p, ul, li1, li2, f, o, z, b, a, r;
  18. // root
  19. // |- p Before: [ 0 ] After: [ 1 ]
  20. // |- ul Before: [ 1 ] After: [ 2 ]
  21. // |- li Before: [ 1, 0 ] After: [ 1, 1 ]
  22. // | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
  23. // | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
  24. // | |- z Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
  25. // |- li Before: [ 1, 1 ] After: [ 1, 2 ]
  26. // |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
  27. // |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
  28. // |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
  29. before( () => {
  30. Element = modules[ 'document/element' ];
  31. Character = modules[ 'document/character' ];
  32. Document = modules[ 'document/document' ];
  33. NodeList = modules[ 'document/nodelist' ];
  34. Position = modules[ 'document/position' ];
  35. CKEditorError = modules.ckeditorerror;
  36. doc = new Document();
  37. root = doc.createRoot( 'root' );
  38. otherRoot = doc.createRoot( 'otherRoot' );
  39. f = new Character( 'f' );
  40. o = new Character( 'o' );
  41. z = new Character( 'z' );
  42. li1 = new Element( 'li', [], [ f, o, z ] );
  43. b = new Character( 'b' );
  44. a = new Character( 'a' );
  45. r = new Character( 'r' );
  46. li2 = new Element( 'li', [], [ b, a, r ] );
  47. ul = new Element( 'ul', [], [ li1, li2 ] );
  48. p = new Element( 'p' );
  49. root.insertChildren( 0, [ p, ul ] );
  50. } );
  51. it( 'should create a position with path and document', () => {
  52. let position = new Position( [ 0 ], root );
  53. expect( position ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  54. expect( position ).to.have.property( 'root' ).that.equals( root );
  55. } );
  56. it( 'should throw error if given root is not a RootElement instance', () => {
  57. expect( () => {
  58. new Position( [ 0 ] )
  59. } ).to.throw( CKEditorError, /position-root-not-rootelement/ );
  60. expect( () => {
  61. new Position( [ 0 ], new Element( 'p' ) );
  62. } ).to.throw( CKEditorError, /position-root-not-rootelement/ );
  63. } );
  64. it( 'should make positions form node and offset', () => {
  65. expect( Position.createFromParentAndOffset( root, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  66. expect( Position.createFromParentAndOffset( root, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  67. expect( Position.createFromParentAndOffset( root, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  68. expect( Position.createFromParentAndOffset( p, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
  69. expect( Position.createFromParentAndOffset( ul, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  70. expect( Position.createFromParentAndOffset( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  71. expect( Position.createFromParentAndOffset( ul, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  72. expect( Position.createFromParentAndOffset( li1, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  73. expect( Position.createFromParentAndOffset( li1, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  74. expect( Position.createFromParentAndOffset( li1, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  75. expect( Position.createFromParentAndOffset( li1, 3 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  76. } );
  77. it( 'should make positions before elements', () => {
  78. expect( Position.createBefore( p ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  79. expect( Position.createBefore( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  80. expect( Position.createBefore( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  81. expect( Position.createBefore( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  82. expect( Position.createBefore( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  83. expect( Position.createBefore( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  84. expect( Position.createBefore( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  85. expect( Position.createBefore( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 0 ] );
  86. expect( Position.createBefore( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  87. expect( Position.createBefore( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  88. } );
  89. it( 'should throw error if one try to make positions before root', () => {
  90. expect( () => {
  91. Position.createBefore( root );
  92. } ).to.throw( CKEditorError, /position-before-root/ );
  93. } );
  94. it( 'should make positions after elements', () => {
  95. expect( Position.createAfter( p ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  96. expect( Position.createAfter( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  97. expect( Position.createAfter( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  98. expect( Position.createAfter( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  99. expect( Position.createAfter( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  100. expect( Position.createAfter( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  101. expect( Position.createAfter( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  102. expect( Position.createAfter( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  103. expect( Position.createAfter( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  104. expect( Position.createAfter( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 3 ] );
  105. } );
  106. it( 'should throw error if one try to make positions after root', () => {
  107. expect( () => {
  108. Position.createAfter( root );
  109. } ).to.throw( CKEditorError, /position-after-root/ );
  110. } );
  111. it( 'should have parent', () => {
  112. expect( new Position( [ 0 ], root ) ).to.have.property( 'parent' ).that.equals( root );
  113. expect( new Position( [ 1 ], root ) ).to.have.property( 'parent' ).that.equals( root );
  114. expect( new Position( [ 2 ], root ) ).to.have.property( 'parent' ).that.equals( root );
  115. expect( new Position( [ 0, 0 ], root ) ).to.have.property( 'parent' ).that.equals( p );
  116. expect( new Position( [ 1, 0 ], root ) ).to.have.property( 'parent' ).that.equals( ul );
  117. expect( new Position( [ 1, 1 ], root ) ).to.have.property( 'parent' ).that.equals( ul );
  118. expect( new Position( [ 1, 2 ], root ) ).to.have.property( 'parent' ).that.equals( ul );
  119. expect( new Position( [ 1, 0, 0 ], root ) ).to.have.property( 'parent' ).that.equals( li1 );
  120. expect( new Position( [ 1, 0, 1 ], root ) ).to.have.property( 'parent' ).that.equals( li1 );
  121. expect( new Position( [ 1, 0, 2 ], root ) ).to.have.property( 'parent' ).that.equals( li1 );
  122. expect( new Position( [ 1, 0, 3 ], root ) ).to.have.property( 'parent' ).that.equals( li1 );
  123. } );
  124. it( 'should have offset', () => {
  125. expect( new Position( [ 0 ], root ) ).to.have.property( 'offset' ).that.equals( 0 );
  126. expect( new Position( [ 1 ], root ) ).to.have.property( 'offset' ).that.equals( 1 );
  127. expect( new Position( [ 2 ], root ) ).to.have.property( 'offset' ).that.equals( 2 );
  128. expect( new Position( [ 0, 0 ], root ) ).to.have.property( 'offset' ).that.equals( 0 );
  129. expect( new Position( [ 1, 0 ], root ) ).to.have.property( 'offset' ).that.equals( 0 );
  130. expect( new Position( [ 1, 1 ], root ) ).to.have.property( 'offset' ).that.equals( 1 );
  131. expect( new Position( [ 1, 2 ], root ) ).to.have.property( 'offset' ).that.equals( 2 );
  132. expect( new Position( [ 1, 0, 0 ], root ) ).to.have.property( 'offset' ).that.equals( 0 );
  133. expect( new Position( [ 1, 0, 1 ], root ) ).to.have.property( 'offset' ).that.equals( 1 );
  134. expect( new Position( [ 1, 0, 2 ], root ) ).to.have.property( 'offset' ).that.equals( 2 );
  135. expect( new Position( [ 1, 0, 3 ], root ) ).to.have.property( 'offset' ).that.equals( 3 );
  136. } );
  137. it( 'should be able to set offset', () => {
  138. let position = new Position( [ 1, 0, 2 ], root );
  139. position.offset = 4;
  140. expect( position.offset ).to.equal( 4 );
  141. expect( position.path ).to.deep.equal( [ 1, 0, 4 ] );
  142. } );
  143. it( 'should have nodeBefore', () => {
  144. expect( new Position( [ 0 ], root ) ).to.have.property( 'nodeBefore' ).that.is.null;
  145. expect( new Position( [ 1 ], root ) ).to.have.property( 'nodeBefore' ).that.equals( p );
  146. expect( new Position( [ 2 ], root ) ).to.have.property( 'nodeBefore' ).that.equals( ul );
  147. expect( new Position( [ 0, 0 ], root ) ).to.have.property( 'nodeBefore' ).that.is.null;
  148. expect( new Position( [ 1, 0 ], root ) ).to.have.property( 'nodeBefore' ).that.is.null;
  149. expect( new Position( [ 1, 1 ], root ) ).to.have.property( 'nodeBefore' ).that.equals( li1 );
  150. expect( new Position( [ 1, 2 ], root ) ).to.have.property( 'nodeBefore' ).that.equals( li2 );
  151. expect( new Position( [ 1, 0, 0 ], root ) ).to.have.property( 'nodeBefore' ).that.is.null;
  152. expect( new Position( [ 1, 0, 1 ], root ) ).to.have.property( 'nodeBefore' ).that.equals( f );
  153. expect( new Position( [ 1, 0, 2 ], root ) ).to.have.property( 'nodeBefore' ).that.equals( o );
  154. expect( new Position( [ 1, 0, 3 ], root ) ).to.have.property( 'nodeBefore' ).that.equals( z );
  155. } );
  156. it( 'should have nodeAfter', () => {
  157. expect( new Position( [ 0 ], root ) ).to.have.property( 'nodeAfter' ).that.equals( p );
  158. expect( new Position( [ 1 ], root ) ).to.have.property( 'nodeAfter' ).that.equals( ul );
  159. expect( new Position( [ 2 ], root ) ).to.have.property( 'nodeAfter' ).that.is.null;
  160. expect( new Position( [ 0, 0 ], root ) ).to.have.property( 'nodeAfter' ).that.is.null;
  161. expect( new Position( [ 1, 0 ], root ) ).to.have.property( 'nodeAfter' ).that.equals( li1 );
  162. expect( new Position( [ 1, 1 ], root ) ).to.have.property( 'nodeAfter' ).that.equals( li2 );
  163. expect( new Position( [ 1, 2 ], root ) ).to.have.property( 'nodeAfter' ).that.is.null;
  164. expect( new Position( [ 1, 0, 0 ], root ) ).to.have.property( 'nodeAfter' ).that.equals( f );
  165. expect( new Position( [ 1, 0, 1 ], root ) ).to.have.property( 'nodeAfter' ).that.equals( o );
  166. expect( new Position( [ 1, 0, 2 ], root ) ).to.have.property( 'nodeAfter' ).that.equals( z );
  167. expect( new Position( [ 1, 0, 3 ], root ) ).to.have.property( 'nodeAfter' ).that.is.null;
  168. } );
  169. it( 'should equals another position with the same path and root', () => {
  170. let position = new Position( [ 1, 1, 2 ], root );
  171. let samePosition = new Position( [ 1, 1, 2 ], root );
  172. expect( position.isEqual( samePosition ) ).to.be.true;
  173. } );
  174. it( 'should not equals another position with the different path', () => {
  175. let position = new Position( [ 1, 1, 1 ], root );
  176. let differentPosition = new Position( [ 1, 2, 2 ], root );
  177. expect( position.isEqual( differentPosition ) ).to.be.false;
  178. } );
  179. it( 'should not equals another position with the different root', () => {
  180. let position = new Position( [ 1, 1, 1 ], root );
  181. let differentPosition = new Position( [ 1, 1, 1 ], otherRoot );
  182. expect( position.isEqual( differentPosition ) ).to.be.false;
  183. } );
  184. it( 'should have correct parent path property', () => {
  185. let position = new Position( [ 1, 2, 3 ], root );
  186. expect( position.parentPath ).to.deep.equal( [ 1, 2 ] );
  187. } );
  188. it( 'should return a new, equal position when cloned', () => {
  189. const position = new Position( [ 1, 2, 3 ], root );
  190. const clone = position.clone();
  191. expect( clone ).not.to.be.equal( position ); // clone is not pointing to the same object as position
  192. expect( clone.isEqual( position ) ).to.be.true; // but they are equal in the position-sense
  193. expect( clone.path ).not.to.be.equal( position.path ); // make sure the paths are not the same array
  194. } );
  195. describe( 'compareWith', () => {
  196. it( 'should return Position.SAME if positions are same', () => {
  197. const position = new Position( [ 1, 2, 3 ], root );
  198. const compared = new Position( [ 1, 2, 3 ], root );
  199. expect( position.compareWith( compared ) ).to.equal( Position.SAME );
  200. } );
  201. it( 'should return Position.BEFORE if the position is before compared one', () => {
  202. const position = new Position( [ 1, 2, 3 ], root );
  203. const compared = new Position( [ 1, 3 ], root );
  204. expect( position.compareWith( compared ) ).to.equal( Position.BEFORE );
  205. } );
  206. it( 'should return Position.AFTER if the position is after compared one', () => {
  207. const position = new Position( [ 1, 2, 3, 4 ], root );
  208. const compared = new Position( [ 1, 2, 3 ], root );
  209. expect( position.compareWith( compared ) ).to.equal( Position.AFTER );
  210. } );
  211. } );
  212. } );