All files / lib/dom DomElement.ts

81.01% Statements 64/79
85% Branches 17/20
63.63% Functions 7/11
81.01% Lines 64/79

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                              1x 1x 1x               1x   36x 3360x 3360x 3360x 12778x 12778x 12778x 1698x 1662x 1698x 36x 36x 1662x 1662x 12778x 3360x   36x 36x   23x 23x 23x 23x   36x 555x 555x 555x 555x 555x       555x 555x     555x 555x 555x 1x 1x 1x 1x 555x 555x   36x             36x 1x 1x   36x 554x 554x         36x             36x                 36x               36x   590x 590x   36x 554x 554x 554x     554x 36x 1x   1x  
/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
 * http://juerglehni.com/ & https://puckey.studio/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */
 
// TODO: remove eslint-disable comment and deal with errors over time
/* eslint-disable */
 
import { ref } from '~/globals';
import { Rectangle } from '~/basic/Rectangle';
import { Size } from '~/basic/Size';
 
/**
 * @name DomElement
 * @namespace
 * @private
 */
// @ts-expect-error = Only a void function can be called with the 'new' keyword
export const DomElement = new (function () {
  // Handles both getting and setting of vendor prefix values
  function handlePrefix(el, name, set, value) {
    var prefixes = ['', 'webkit', 'moz', 'Moz', 'ms', 'o'],
      suffix = name[0].toUpperCase() + name.substring(1);
    for (var i = 0; i < 6; i++) {
      var prefix = prefixes[i],
        key = prefix ? prefix + suffix : name;
      if (key in el) {
        if (set) {
          el[key] = value;
        } else {
          return el[key];
        }
        break;
      }
    }
  }
 
  return /** @lends DomElement */ {
    getStyles: function (el) {
      // If el is a document (nodeType == 9), use it directly
      var doc = el && el.nodeType !== 9 ? el.ownerDocument : el,
        view = doc && doc.defaultView;
      return view && view.getComputedStyle(el, '');
    },
 
    getBounds: function (el, viewport) {
      var doc = el.ownerDocument,
        body = doc.body,
        html = doc.documentElement,
        rect;
      try {
        // On IE, for nodes that are not inside the DOM, this throws an
        // exception. Emulate the behavior of all other browsers, which
        // return a rectangle of 0 dimensions.
        rect = el.getBoundingClientRect();
      } catch (e) {
        rect = { left: 0, top: 0, width: 0, height: 0 };
      }
      var x = rect.left - (html.clientLeft || body.clientLeft || 0),
        y = rect.top - (html.clientTop || body.clientTop || 0);
      if (!viewport) {
        var view = doc.defaultView;
        x += view.pageXOffset || html.scrollLeft || body.scrollLeft;
        y += view.pageYOffset || html.scrollTop || body.scrollTop;
      }
      return new Rectangle(x, y, rect.width, rect.height);
    },
 
    getViewportBounds: function (el) {
      var doc = el.ownerDocument,
        view = doc.defaultView,
        html = doc.documentElement;
      return new Rectangle(0, 0, view.innerWidth || html.clientWidth, view.innerHeight || html.clientHeight);
    },
 
    getOffset: function (el, viewport) {
      return DomElement.getBounds(el, viewport).getPoint();
    },
 
    getSize: function (el) {
      return DomElement.getBounds(el, true).getSize();
    },
 
    /**
     * Checks if element is invisible (display: none, ...).
     */
    isInvisible: function (el) {
      return DomElement.getSize(el).equals(new Size(0, 0));
    },
 
    /**
     * Checks if element is visible in current viewport.
     */
    isInView: function (el) {
      // See if the viewport bounds intersect with the windows rectangle
      // which always starts at 0, 0
      return !DomElement.isInvisible(el) && DomElement.getViewportBounds(el).intersects(DomElement.getBounds(el, true));
    },
 
    /**
     * Checks if element is inside the DOM.
     */
    isInserted: function (el) {
      return globalThis.document.body.contains(el);
    },
 
    /**
     * Gets the given property from the element, trying out all browser
     * prefix variants.
     */
    getPrefixed: function (el, name) {
      // @ts-expect-error = Expected 4 arguments, but got 2.
      return el && handlePrefix(el, name);
    },
 
    setPrefixed: function (el, name, value) {
      if (typeof name === 'object') {
        for (var key in name) handlePrefix(el, key, true, name[key]);
      } else {
        handlePrefix(el, name, true, value);
      }
    },
  };
})();
 
ref.DomElement = DomElement;