positioniterator.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/document',
  9. 'document/element',
  10. 'document/character',
  11. 'document/positioniterator',
  12. 'document/position',
  13. 'document/range' );
  14. describe( 'range iterator', function() {
  15. var document, expectedItems;
  16. var root, img1, paragraph, charB, charA, charR, img2, charX;
  17. var OPENING_TAG, CLOSING_TAG, CHARACTER;
  18. before( function() {
  19. var Document = modules[ 'document/document' ];
  20. var Element = modules[ 'document/element' ];
  21. var Character = modules[ 'document/character' ];
  22. var PositionIterator = modules[ 'document/positioniterator' ];
  23. OPENING_TAG = PositionIterator.OPENING_TAG;
  24. CLOSING_TAG = PositionIterator.CLOSING_TAG;
  25. CHARACTER = PositionIterator.CHARACTER;
  26. document = new Document();
  27. root = document.root;
  28. // root
  29. // |- img1
  30. // |- p
  31. // |- B
  32. // |- A
  33. // |- R
  34. // |
  35. // |- img2
  36. // |
  37. // |- X
  38. img1 = new Element( root, 'img1' );
  39. root.children.push( img1 );
  40. paragraph = new Element( root, 'p' );
  41. root.children.push( paragraph );
  42. charB = new Character( paragraph, 'B' );
  43. paragraph.children.push( charB );
  44. charA = new Character( paragraph, 'A' );
  45. paragraph.children.push( charA );
  46. charR = new Character( paragraph, 'R' );
  47. paragraph.children.push( charR );
  48. img2 = new Element( paragraph, 'img2' );
  49. paragraph.children.push( img2 );
  50. charX = new Character( paragraph, 'X' );
  51. paragraph.children.push( charX );
  52. expectedItems = [
  53. { type: OPENING_TAG, node: img1 },
  54. { type: CLOSING_TAG, node: img1 },
  55. { type: OPENING_TAG, node: paragraph },
  56. { type: CHARACTER, node: charB },
  57. { type: CHARACTER, node: charA },
  58. { type: CHARACTER, node: charR },
  59. { type: OPENING_TAG, node: img2 },
  60. { type: CLOSING_TAG, node: img2 },
  61. { type: CHARACTER, node: charX },
  62. { type: CLOSING_TAG, node: paragraph }
  63. ];
  64. } );
  65. it( 'should return next position', function() {
  66. var PositionIterator = modules[ 'document/positioniterator' ];
  67. var Position = modules[ 'document/position' ];
  68. var iterator = new PositionIterator( new Position( [ 0 ], document ) ); // begging of root
  69. var i, len;
  70. for ( i = 0, len = expectedItems.length; i < len; i++ ) {
  71. expect( iterator.next() ).to.deep.equal( { done: false, value: expectedItems[ i ] } );
  72. }
  73. expect( iterator.next() ).to.have.property( 'done' ).that.is.true;
  74. } );
  75. it( 'should return previous position', function() {
  76. var PositionIterator = modules[ 'document/positioniterator' ];
  77. var Position = modules[ 'document/position' ];
  78. var iterator = new PositionIterator( new Position( [ 2 ], document ) ); // ending of root
  79. for ( var i = expectedItems.length - 1; i >= 0; i-- ) {
  80. expect( iterator.previous() ).to.deep.equal( { done: false, value: expectedItems[ i ] } );
  81. }
  82. expect( iterator.previous() ).to.have.property( 'done' ).that.is.true;
  83. } );
  84. it( 'should return next position in the boundaries', function() {
  85. var PositionIterator = modules[ 'document/positioniterator' ];
  86. var Position = modules[ 'document/position' ];
  87. var Range = modules[ 'document/range' ];
  88. var start = new Position( [ 1, 0 ], document ); // p, 0
  89. var end = new Position( [ 1, 3, 0 ], document ); // img, 0
  90. var iterator = new PositionIterator( new Range( start, end ) );
  91. var i, len;
  92. for ( i = 3, len = expectedItems.length; i < 7; i++ ) {
  93. expect( iterator.next() ).to.deep.equal( { done: false, value: expectedItems[ i ] } );
  94. }
  95. expect( iterator.next() ).to.have.property( 'done' ).that.is.true;
  96. } );
  97. it( 'should return previous position in the boundaries', function() {
  98. var PositionIterator = modules[ 'document/positioniterator' ];
  99. var Position = modules[ 'document/position' ];
  100. var Range = modules[ 'document/range' ];
  101. var start = new Position( [ 1, 0 ], document ); // p, 0
  102. var end = new Position( [ 1, 3, 0 ], document ); // img, 0
  103. var iterator = new PositionIterator( new Range( start, end ), end );
  104. var i, len;
  105. for ( i = 6, len = expectedItems.length; i > 2; i-- ) {
  106. expect( iterator.previous() ).to.deep.equal( { done: false, value: expectedItems[ i ] } );
  107. }
  108. expect( iterator.previous() ).to.have.property( 'done' ).that.is.true;
  109. } );
  110. it( 'should return iterate over the range', function() {
  111. var Position = modules[ 'document/position' ];
  112. var Range = modules[ 'document/range' ];
  113. var start = new Position( [ 0 ], document ); // begging of root
  114. var end = new Position( [ 2 ], document ); // ending of root
  115. var range = new Range( start, end );
  116. var i = 0;
  117. var value;
  118. for ( value of range ) {
  119. expect( value ).to.deep.equal( expectedItems[ i ] );
  120. i++;
  121. }
  122. expect( i ).to.equal( expectedItems.length );
  123. } );
  124. } );