position.js 10 KB

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