diff options
| author | Darius Jahandarie <djahandarie@gmail.com> | 2023-12-06 03:53:16 +0000 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-06 03:53:16 +0000 | 
| commit | bd5bc1a5db29903bc098995cd9262c4576bf76af (patch) | |
| tree | c9214189e0214480fcf6539ad1c6327aef6cbd1c /ext/js/dom/text-source-range.js | |
| parent | fd6bba8a2a869eaf2b2c1fa49001f933fce3c618 (diff) | |
| parent | 23e6fb76319c9ed7c9bcdc3efba39bc5dd38f288 (diff) | |
Merge pull request #339 from toasted-nutbread/type-annotations
Type annotations
Diffstat (limited to 'ext/js/dom/text-source-range.js')
| -rw-r--r-- | ext/js/dom/text-source-range.js | 36 | 
1 files changed, 26 insertions, 10 deletions
diff --git a/ext/js/dom/text-source-range.js b/ext/js/dom/text-source-range.js index d5e70052..fd09fdda 100644 --- a/ext/js/dom/text-source-range.js +++ b/ext/js/dom/text-source-range.js @@ -18,7 +18,6 @@  import {DocumentUtil} from './document-util.js';  import {DOMTextScanner} from './dom-text-scanner.js'; -import {TextSourceElement} from './text-source-element.js';  /**   * This class represents a text source that comes from text nodes in the document. @@ -36,26 +35,33 @@ export class TextSourceRange {       * @param {?Element} imposterElement The temporary imposter element.       * @param {?Element} imposterSourceElement The source element which the imposter is imitating.       *   Must not be `null` if imposterElement is specified. -     * @param {?Rect[]} cachedRects A set of cached `DOMRect`s representing the rects of the text source, +     * @param {?DOMRect[]} cachedRects A set of cached `DOMRect`s representing the rects of the text source,       *   which can be used after the imposter element is removed from the page.       *   Must not be `null` if imposterElement is specified. -     * @param {?Rect} cachedSourceRect A cached `DOMRect` representing the rect of the `imposterSourceElement`, +     * @param {?DOMRect} cachedSourceRect A cached `DOMRect` representing the rect of the `imposterSourceElement`,       *   which can be used after the imposter element is removed from the page.       *   Must not be `null` if imposterElement is specified.       */      constructor(range, rangeStartOffset, content, imposterElement, imposterSourceElement, cachedRects, cachedSourceRect) { +        /** @type {Range} */          this._range = range; +        /** @type {number} */          this._rangeStartOffset = rangeStartOffset; +        /** @type {string} */          this._content = content; +        /** @type {?Element} */          this._imposterElement = imposterElement; +        /** @type {?Element} */          this._imposterSourceElement = imposterSourceElement; +        /** @type {?DOMRect[]} */          this._cachedRects = cachedRects; +        /** @type {?DOMRect} */          this._cachedSourceRect = cachedSourceRect;      }      /**       * Gets the type name of this instance. -     * @type {string} +     * @type {'range'}       */      get type() {          return 'range'; @@ -71,7 +77,7 @@ export class TextSourceRange {      /**       * The starting offset for the range. -     * @type {Range} +     * @type {number}       */      get rangeStartOffset() {          return this._rangeStartOffset; @@ -169,12 +175,12 @@ export class TextSourceRange {      /**       * Gets writing mode that is used for this element.       * See: https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode. -     * @returns {string} The rects. +     * @returns {import('document-util').NormalizedWritingMode} The writing mode.       */      getWritingMode() {          let node = this._isImposterDisconnected() ? this._imposterSourceElement : this._range.startContainer;          if (node !== null && node.nodeType !== Node.ELEMENT_NODE) { node = node.parentElement; } -        return DocumentUtil.getElementWritingMode(node); +        return DocumentUtil.getElementWritingMode(/** @type {?Element} */ (node));      }      /** @@ -183,6 +189,7 @@ export class TextSourceRange {      select() {          if (this._imposterElement !== null) { return; }          const selection = window.getSelection(); +        if (selection === null) { return; }          selection.removeAllRanges();          selection.addRange(this._range);      } @@ -193,12 +200,13 @@ export class TextSourceRange {      deselect() {          if (this._imposterElement !== null) { return; }          const selection = window.getSelection(); +        if (selection === null) { return; }          selection.removeAllRanges();      }      /**       * Checks whether another text source has the same starting point. -     * @param {TextSourceElement|TextSourceRange} other The other source to test. +     * @param {import('text-source').TextSource} other The other source to test.       * @returns {boolean} `true` if the starting points are equivalent, `false` otherwise.       * @throws {Error} An exception can be thrown if `Range.compareBoundaryPoints` fails,       *   which shouldn't happen, but the handler is kept in case of unexpected errors. @@ -220,7 +228,7 @@ export class TextSourceRange {              try {                  return this._range.compareBoundaryPoints(Range.START_TO_START, other.range) === 0;              } catch (e) { -                if (e.name === 'WrongDocumentError') { +                if (e instanceof Error && e.name === 'WrongDocumentError') {                      // This can happen with shadow DOMs if the ranges are in different documents.                      return false;                  } @@ -269,9 +277,17 @@ export class TextSourceRange {      /**       * Gets the cached rects for a disconnected imposter element. -     * @returns {Rect[]} The rects for the element. +     * @returns {DOMRect[]} The rects for the element. +     * @throws {Error}       */      _getCachedRects() { +        if ( +            this._cachedRects === null || +            this._cachedSourceRect === null || +            this._imposterSourceElement === null +        ) { +            throw new Error('Cached rects not valid for this instance'); +        }          const sourceRect = DocumentUtil.convertRectZoomCoordinates(this._imposterSourceElement.getBoundingClientRect(), this._imposterSourceElement);          return DocumentUtil.offsetDOMRects(              this._cachedRects,  |