position.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 create positions before elements', function() {
  51. var Position = modules[ 'document/position' ];
  52. expect( new Position( root, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [] );
  53. expect( new Position( p, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 0 ] );
  54. expect( new Position( ul, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1 ] );
  55. expect( new Position( li1, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0 ] );
  56. expect( new Position( f, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 0 ] );
  57. expect( new Position( o, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 1 ] );
  58. expect( new Position( z, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 2 ] );
  59. expect( new Position( li2, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1 ] );
  60. expect( new Position( b, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 0 ] );
  61. expect( new Position( a, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 1 ] );
  62. expect( new Position( r, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 2 ] );
  63. } );
  64. it( 'should create positions after elements', function() {
  65. var Position = modules[ 'document/position' ];
  66. expect( new Position( root, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [] );
  67. expect( new Position( p, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1 ] );
  68. expect( new Position( ul, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 2 ] );
  69. expect( new Position( li1, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1 ] );
  70. expect( new Position( f, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 1 ] );
  71. expect( new Position( o, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 2 ] );
  72. expect( new Position( z, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 3 ] );
  73. expect( new Position( li2, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 2 ] );
  74. expect( new Position( b, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 1 ] );
  75. expect( new Position( a, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 2 ] );
  76. expect( new Position( r, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 3 ] );
  77. } );
  78. } );