From 7170acb48e6aa476371d28e09343fdc2d75b8f3f Mon Sep 17 00:00:00 2001 From: "stefan.eckert" Date: Sun, 30 Jul 2023 09:37:07 +0200 Subject: [PATCH] feat(operator): to primitive impl for dom point --- src/dom.js | 12 ++++++++++++ src/operator.js | 18 ++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/dom.js diff --git a/src/dom.js b/src/dom.js new file mode 100644 index 00000000..f4363e18 --- /dev/null +++ b/src/dom.js @@ -0,0 +1,12 @@ +import { cachedValueOf, defineVectorLength } from "./operator.js"; + +export const hijackDOMPoint = (DOMPoint) => { + try { + cachedValueOf(DOMPoint); + defineVectorLength(DOMPoint, 4); + } catch (e) { + console.error('error while hijackDOMPoint'); + console.error(e); + } +}; + diff --git a/src/operator.js b/src/operator.js index 026185e3..b438d6bd 100644 --- a/src/operator.js +++ b/src/operator.js @@ -1,4 +1,5 @@ import { isNumber } from './utils/math'; +import { hijackDOMPoint } from "./dom.js"; const X = 0; const Y = 1; @@ -194,16 +195,20 @@ export function operatorCalc (alg, result) { export function cachedValueOf (VectorClass, getSource) { const Vector = VectorClass.prototype; Vector[GET_SOURCE] = getSource; - const name = 'valueOf'; - const org = Vector[name]; + const org = Vector[Symbol.toPrimitive] || function (hint) { + if (hint === 'string') { + return this.toString(); + } + return this.valueOf(); + }; - Vector[name] = function () { + Vector[Symbol.toPrimitive] = function (hint) { if (inProgress === X) { inVector = inVector ? maxVector(inVector, this) : this; collect[elCount - 1] = this; } if (inProgress === DEFAULT) { - return org.call(this); + return org.call(this, hint); } return getVectorValue(this, inProgress); }; @@ -277,3 +282,8 @@ export function cachedFactory (VectorClass) { export function cachedFunction (fun) { return bindCache(fun); } + +if (typeof DOMPoint !== 'undefined' ) { + // eslint-disable-next-line no-undef + hijackDOMPoint(DOMPoint); +}