mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 15:00:01 -04:00
fix element methods
This commit is contained in:
parent
212a9510d5
commit
e0c38a5c58
1 changed files with 25 additions and 8 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import { decodeUrl } from "../shared/rewriters/url";
|
||||||
import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "./shared";
|
import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "./shared";
|
||||||
|
|
||||||
const attrObject = {
|
const attrObject = {
|
||||||
|
@ -21,11 +22,20 @@ 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() {
|
||||||
return this.dataset[attr];
|
if (/src|href|data|action|formaction/.test(attr)) {
|
||||||
|
return decodeUrl(descriptor.get.call(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.__origattrs[attr]) {
|
||||||
|
return this.__origattrs[attr];
|
||||||
|
}
|
||||||
|
|
||||||
|
return descriptor.get.call(this);
|
||||||
},
|
},
|
||||||
|
|
||||||
set(value) {
|
set(value) {
|
||||||
this.dataset[attr] = value;
|
this.__origattrs[attr] = value;
|
||||||
|
|
||||||
if (/nonce|integrity|csp/.test(attr)) {
|
if (/nonce|integrity|csp/.test(attr)) {
|
||||||
return;
|
return;
|
||||||
} else if (/src|href|data|action|formaction/.test(attr)) {
|
} else if (/src|href|data|action|formaction/.test(attr)) {
|
||||||
|
@ -47,10 +57,18 @@ for (const attr of attrs) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Element {
|
||||||
|
__origattrs: Record<string, string>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.dataset[argArray[0]]) {
|
if (attrs.includes(argArray[0]) && thisArg.__origattrs[argArray[0]]) {
|
||||||
return thisArg.dataset[argArray[0]];
|
return thisArg.__origattrs[argArray[0]];
|
||||||
}
|
}
|
||||||
|
|
||||||
return Reflect.apply(target, thisArg, argArray);
|
return Reflect.apply(target, thisArg, argArray);
|
||||||
|
@ -60,11 +78,10 @@ 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.dataset[argArray[0]] = argArray[1];
|
thisArg.__origattrs[argArray[0]] = argArray[1];
|
||||||
if (/nonce|integrity|csp/.test(argArray[0])) {
|
if (/nonce|integrity|csp/.test(argArray[0])) {
|
||||||
return;
|
return;
|
||||||
} else if (/src|href|data|action|formaction/.test(argArray[0])) {
|
} else if (/src|href|data|action|formaction/.test(argArray[0])) {
|
||||||
console.log(thisArg);
|
|
||||||
argArray[1] = encodeUrl(argArray[1]);
|
argArray[1] = encodeUrl(argArray[1]);
|
||||||
} else if (argArray[0] === "srcdoc") {
|
} else if (argArray[0] === "srcdoc") {
|
||||||
argArray[1] = rewriteHtml(argArray[1]);
|
argArray[1] = rewriteHtml(argArray[1]);
|
||||||
|
@ -88,11 +105,11 @@ Object.defineProperty(Element.prototype, "innerHTML", {
|
||||||
value = rewriteJs(value);
|
value = rewriteJs(value);
|
||||||
} else if (this instanceof HTMLStyleElement) {
|
} else if (this instanceof HTMLStyleElement) {
|
||||||
value = rewriteCss(value);
|
value = rewriteCss(value);
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
} else if (!(value instanceof TrustedHTML)) {
|
} else if (!(value instanceof TrustedHTML)) {
|
||||||
value = rewriteHtml(value);
|
value = rewriteHtml(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return innerHTML.set.call(this, value);
|
return innerHTML.set.call(this, value);
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue