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

94.87% Statements 37/39
85.71% Branches 6/7
100% Functions 1/1
94.87% Lines 37/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
    159x
    159x
    159x
    159x
    159x
    159x
    159x
    159x
    159x
    159x
    159x
    377x
    377x
    377x
    377x
    3011x
    377x
    3011x
    377x
    3011x
    3011x
    377x
    377x
    377x
    377x
    377x
    377x
    377x
    377x
    377x

    377x
    377x
    377x
    159x
    159x
     // 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;
        },
      });
    }