# All files / test-utils/polyfills offsetParent.ts

100% Statements 39/39
100% Branches 10/10
100% Functions 1/1
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    401x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    307x
    307x
    307x
    307x
    1589x
    307x
    1434x
    307x
    1434x
    1434x
    307x
    307x
    307x
    307x
    152x
    152x
    152x
    152x
    307x
    155x
    155x
    152x
    152x
    152x
    1x
    1x
     // this polyfill was added to support the tree keyboard movement behavior
    if (typeof window !== "undefined") {
      // Based off of https://github.com/jsdom/jsdom/issues/1261#issuecomment-512217225
      //
      // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent
      //
      // Note: offsetParent returns null in the following situations:
      //
      // - The element or any ancestor has the display property set to none.
      // - The element has the position property set to fixed (Firefox returns <body>).
      // - The element is <body> or <html>.
      Object.defineProperty(HTMLElement.prototype, "offsetParent", {
        get(this: HTMLElement) {
          // eslint-disable-next-line @typescript-eslint/no-this-alias
          let element: ParentNode | null = this;
          while (
            element &&
            // it'll almost always be an html element, unless using document fragments
            !(element as HTMLElement).hidden &&
            (element as HTMLElement).style?.display?.toLowerCase() !== "none"
          ) {
            element = element.parentNode;
          }

          if (
            // the current element or a parent has display: none
            element ||
            // the current element is fixed
            this.style?.position?.toLowerCase() === "fixed" ||
            this.tagName.toLowerCase() === "html" ||
            this.tagName.toLowerCase() === "body"
          ) {
            return null;
          }

          return this.parentNode;
        },
      });
    }