position.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. describe( 'position', function() {
  12. var Element, Character;
  13. var root, p, ul, li1, li2, f, o, z, b, a, r;
  14. // root
  15. // |- p Before: [ 0 ] After: [ 1 ]
  16. // |- ul Before: [ 1 ] After: [ 2 ]
  17. // |- li Before: [ 1, 0 ] After: [ 1, 1 ]
  18. // | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
  19. // | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
  20. // | |- z Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
  21. // |- li Before: [ 1, 1 ] After: [ 1, 2 ]
  22. // |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
  23. // |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
  24. // |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
  25. before( function() {
  26. Element = modules[ 'document/element' ];
  27. Character = modules[ 'document/character' ];
  28. root = new Element();
  29. p = new Element( root, 'p' );
  30. ul = new Element( root, 'ul' );
  31. li1 = new Element( ul, 'li' );
  32. f = new Character( li1, 'f' );
  33. o = new Character( li1, 'o' );
  34. z = new Character( li1, 'z' );
  35. li2 = new Element( ul, 'li' );
  36. b = new Character( li2, 'b' );
  37. a = new Character( li2, 'a' );
  38. r = new Character( li2, 'r' );
  39. root.children.push( p );
  40. root.children.push( ul );
  41. ul.children.push( li1 );
  42. ul.children.push( li2 );
  43. li1.children.push( f );
  44. li1.children.push( o );
  45. li1.children.push( z );
  46. li2.children.push( b );
  47. li2.children.push( a );
  48. li2.children.push( r );
  49. } );
  50. it( 'should have path', function() {
  51. var Position = modules[ 'document/position' ];
  52. expect( new Position( root, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  53. expect( new Position( root, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  54. expect( new Position( root, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  55. expect( new Position( p, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
  56. expect( new Position( ul, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  57. expect( new Position( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  58. expect( new Position( ul, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  59. expect( new Position( li1, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  60. expect( new Position( li1, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  61. expect( new Position( li1, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  62. expect( new Position( li1, 3 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  63. } );
  64. it( 'should have nodeBefore', function() {
  65. var Position = modules[ 'document/position' ];
  66. expect( new Position( root, 0 ) ).to.have.property( 'nodeBefore' ).that.is.null;
  67. expect( new Position( root, 1 ) ).to.have.property( 'nodeBefore' ).that.equals( p );
  68. expect( new Position( root, 2 ) ).to.have.property( 'nodeBefore' ).that.equals( ul );
  69. expect( new Position( p, 0 ) ).to.have.property( 'nodeBefore' ).that.is.null;
  70. expect( new Position( ul, 0 ) ).to.have.property( 'nodeBefore' ).that.is.null;
  71. expect( new Position( ul, 1 ) ).to.have.property( 'nodeBefore' ).that.equals( li1 );
  72. expect( new Position( ul, 2 ) ).to.have.property( 'nodeBefore' ).that.equals( li2 );
  73. expect( new Position( li1, 0 ) ).to.have.property( 'nodeBefore' ).that.is.null;
  74. expect( new Position( li1, 1 ) ).to.have.property( 'nodeBefore' ).that.equals( f );
  75. expect( new Position( li1, 2 ) ).to.have.property( 'nodeBefore' ).that.equals( o );
  76. expect( new Position( li1, 3 ) ).to.have.property( 'nodeBefore' ).that.equals( z );
  77. } );
  78. it( 'should have nodeAfter', function() {
  79. var Position = modules[ 'document/position' ];
  80. expect( new Position( root, 0 ) ).to.have.property( 'nodeAfter' ).that.equals( p );
  81. expect( new Position( root, 1 ) ).to.have.property( 'nodeAfter' ).that.equals( ul );
  82. expect( new Position( root, 2 ) ).to.have.property( 'nodeAfter' ).that.is.null;
  83. expect( new Position( p, 0 ) ).to.have.property( 'nodeAfter' ).that.is.null;
  84. expect( new Position( ul, 0 ) ).to.have.property( 'nodeAfter' ).that.equals( li1 );
  85. expect( new Position( ul, 1 ) ).to.have.property( 'nodeAfter' ).that.equals( li2 );
  86. expect( new Position( ul, 2 ) ).to.have.property( 'nodeAfter' ).that.is.null;
  87. expect( new Position( li1, 0 ) ).to.have.property( 'nodeAfter' ).that.equals( f );
  88. expect( new Position( li1, 1 ) ).to.have.property( 'nodeAfter' ).that.equals( o );
  89. expect( new Position( li1, 2 ) ).to.have.property( 'nodeAfter' ).that.equals( z );
  90. expect( new Position( li1, 3 ) ).to.have.property( 'nodeAfter' ).that.is.null;
  91. } );
  92. it( 'should equals another position with the same offset and node', function() {
  93. var Position = modules[ 'document/position' ];
  94. var position = new Position( root, 0 );
  95. var samePosition = new Position( root, 0 );
  96. expect( position.equals( samePosition ) ).to.be.true;
  97. } );
  98. it( 'should not equals another position with the different offset', function() {
  99. var Position = modules[ 'document/position' ];
  100. var position = new Position( root, 0 );
  101. var differentOffset = new Position( root, 1 );
  102. expect( position.equals( differentOffset ) ).to.be.false;
  103. } );
  104. it( 'should not equals another position with the different node', function() {
  105. var Position = modules[ 'document/position' ];
  106. var position = new Position( root, 0 );
  107. var differentNode = new Position( p, 0 );
  108. expect( position.equals( differentNode ) ).to.be.false;
  109. } );
  110. } );