File size: 2,081 Bytes
bee6636 |
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 |
import { ScramjetClient } from "../client";
export default function (client: ScramjetClient, _self: typeof window) {
client.Trap("Element.prototype.attributes", {
get(ctx) {
const map = ctx.get() as NamedNodeMap;
const proxy = new Proxy(map, {
get(target, prop, _receiver) {
const value = Reflect.get(target, prop);
if (prop === "length") {
return Object.keys(proxy).length;
}
if (prop === "getNamedItem") {
return (name: string) => proxy[name];
}
if (prop === "getNamedItemNS") {
return (namespace: string, name: string) =>
proxy[`${namespace}:${name}`];
}
if (prop in NamedNodeMap.prototype && typeof value === "function") {
return new Proxy(value, {
apply(target, that, args) {
if (that === proxy) {
return Reflect.apply(target, map, args);
}
return Reflect.apply(target, that, args);
},
});
}
if (
(typeof prop === "string" || typeof prop === "number") &&
!isNaN(Number(prop))
) {
const position = Object.keys(proxy)[prop];
return map[position];
}
if (!this.has(target, prop)) return undefined;
return value;
},
ownKeys(target) {
const keys = Reflect.ownKeys(target);
return keys.filter((key) => this.has(target, key));
},
has(target, prop) {
if (typeof prop === "symbol") return Reflect.has(target, prop);
if (prop.startsWith("scramjet-attr-")) return false;
if (map[prop]?.name?.startsWith("scramjet-attr-")) return false;
return Reflect.has(target, prop);
},
});
return proxy;
},
});
client.Trap("Attr.prototype.value", {
get(ctx) {
if (ctx.this?.ownerElement) {
return ctx.this.ownerElement.getAttribute(ctx.this.name);
}
return ctx.get();
},
set(ctx, value) {
if (ctx.this?.ownerElement) {
return ctx.this.ownerElement.setAttribute(ctx.this.name, value);
}
return ctx.set(value);
},
});
}
|