positioniterator.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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( root, 0 ) );
  69. for ( var i = 0, len = expectedItems.length; i < len; i++ ) {
  70. expect( iterator.next() ).to.deep.equal( { done: false, value: expectedItems[ i ] } );
  71. }
  72. expect( iterator.next() ).to.have.property( 'done' ).that.is.true;
  73. } );
  74. it( 'should return previous position', function() {
  75. var PositionIterator = modules[ 'document/positioniterator' ];
  76. var Position = modules[ 'document/position' ];
  77. var iterator = new PositionIterator( new Position( root, 2 ) );
  78. for ( var i = expectedItems.length - 1; i >= 0; i-- ) {
  79. expect( iterator.previous() ).to.deep.equal( { done: false, value: expectedItems[ i ] } );
  80. }
  81. expect( iterator.previous() ).to.have.property( 'done' ).that.is.true;
  82. } );
  83. it( 'should return iterate over the range', function() {
  84. var Position = modules[ 'document/position' ];
  85. var Range = modules[ 'document/range' ];
  86. var start = new Position( root, 0 );
  87. var end = new Position( root, 2 );
  88. var range = new Range( start, end );
  89. var i = 0;
  90. var value;
  91. for ( value of range ) {
  92. expect( value ).to.deep.equal( expectedItems[ i ] );
  93. i++;
  94. }
  95. expect( i ).to.equal( expectedItems.length );
  96. } );
  97. } );