fix up element rewriter

This commit is contained in:
Percs 2024-07-17 02:47:16 -05:00
parent 3f91d49b6d
commit 0088a7054e

View file

@ -34,19 +34,19 @@ for (const attr of attrs) {
const descriptor = Object.getOwnPropertyDescriptor(element.prototype, attr); const descriptor = Object.getOwnPropertyDescriptor(element.prototype, attr);
Object.defineProperty(element.prototype, attr, { Object.defineProperty(element.prototype, attr, {
get() { get() {
if (/src|href|data|action|formaction/.test(attr)) { if (["src", "data", "href", "action", "formaction"].includes(attr)) {
return decodeUrl(descriptor.get.call(this)); return decodeUrl(descriptor.get.call(this));
} }
if (this.__origattrs[attr]) { if (this.$origattrs[attr]) {
return this.__origattrs[attr]; return this.$origattrs[attr];
} }
return descriptor.get.call(this); return descriptor.get.call(this);
}, },
set(value) { set(value) {
this.__origattrs[attr] = value; this.$origattrs[attr] = value;
if (["nonce", "integrity", "csp"].includes(attr)) { if (["nonce", "integrity", "csp"].includes(attr)) {
return; return;
@ -68,16 +68,16 @@ for (const attr of attrs) {
declare global { declare global {
interface Element { interface Element {
__origattrs: Record<string, string>; $origattrs: Record<string, string>;
} }
} }
Element.prototype.__origattrs = {}; Element.prototype.$origattrs = {};
Element.prototype.getAttribute = new Proxy(Element.prototype.getAttribute, { Element.prototype.getAttribute = new Proxy(Element.prototype.getAttribute, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
if (attrs.includes(argArray[0]) && thisArg.__origattrs[argArray[0]]) { if (attrs.includes(argArray[0]) && thisArg.$origattrs[argArray[0]]) {
return thisArg.__origattrs[argArray[0]]; return thisArg.$origattrs[argArray[0]];
} }
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
@ -87,7 +87,7 @@ Element.prototype.getAttribute = new Proxy(Element.prototype.getAttribute, {
Element.prototype.setAttribute = new Proxy(Element.prototype.setAttribute, { Element.prototype.setAttribute = new Proxy(Element.prototype.setAttribute, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
if (attrs.includes(argArray[0])) { if (attrs.includes(argArray[0])) {
thisArg.__origattrs[argArray[0]] = argArray[1]; thisArg.$origattrs[argArray[0]] = argArray[1];
if (["nonce", "integrity", "csp"].includes(argArray[0])) { if (["nonce", "integrity", "csp"].includes(argArray[0])) {
return; return;
} else if ( } else if (