range.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. CKEDITOR.define( [ 'document/positioniterator', 'document/position' ], ( PositionIterator, Position ) => {
  7. /**
  8. * Range class. Range is iterable.
  9. *
  10. * @class document.Range
  11. */
  12. class Range {
  13. /**
  14. * Creates a range.
  15. *
  16. * @param {document.Position} start Start position.
  17. * @param {document.Position} end End position.
  18. * @constructor
  19. */
  20. constructor( start, end ) {
  21. /**
  22. * Start position.
  23. *
  24. * @property {document.Position}
  25. */
  26. this.start = start;
  27. /**
  28. * End position.
  29. *
  30. * @property {document.Position}
  31. */
  32. this.end = end;
  33. }
  34. /**
  35. * Creates a range inside an element which starts before the first child and ends after the last child.
  36. *
  37. * @param {document.Element} element Element which is a parent for the range.
  38. * @returns {document.Range} Created range.
  39. */
  40. static createFromElement( element ) {
  41. return Range.createFromParentsAndOffsets( element, 0, element, element.getChildCount() );
  42. }
  43. /**
  44. * Creates a range from given parents and offsets.
  45. *
  46. * @param {document.Element} startElement Start position parent element.
  47. * @param {Number} startOffset Start position offset.
  48. * @param {document.Element} endElement End position parent element.
  49. * @param {Number} endOffset End position offset.
  50. * @returns {document.Range} Created range.
  51. */
  52. static createFromParentsAndOffsets( startElement, startOffset, endElement, endOffset ) {
  53. return new Range(
  54. Position.createFromParentAndOffset( startElement, startOffset ),
  55. Position.createFromParentAndOffset( endElement, endOffset )
  56. );
  57. }
  58. /**
  59. * Two ranges equal if their start and end positions equal.
  60. *
  61. * @param {document.Range} otherRange Range to compare with.
  62. * @returns {Boolean} True if ranges equal.
  63. */
  64. isEqual( otherRange ) {
  65. return this.start.isEqual( otherRange.start ) && this.end.isEqual( otherRange.end );
  66. }
  67. /**
  68. * Range iterator.
  69. *
  70. * @see document.PositionIterator
  71. */
  72. [ Symbol.iterator ]() {
  73. return new PositionIterator( this );
  74. }
  75. }
  76. return Range;
  77. } );