|
| 1 | +{ |
| 2 | + // TODO: Only perform the escapes on the correct element |
| 3 | + const escapeAttrs = ["href", "integrity"]; |
| 4 | + |
| 5 | + const escape = { |
| 6 | + get: attr => { |
| 7 | + return attr |
| 8 | + .replace($aero.escape(escapeAttrs[0]), "_$&") |
| 9 | + .replace($aero.escape(escapeAttrs[1]), "_$&"); |
| 10 | + }, |
| 11 | + }; |
| 12 | + |
| 13 | + const attr = { |
| 14 | + apply(_target, _that, args) { |
| 15 | + const [, name] = args; |
| 16 | + |
| 17 | + args[0] = escape.get(name); |
| 18 | + |
| 19 | + return Reflect.apply(...arguments); |
| 20 | + }, |
| 21 | + }; |
| 22 | + const attrNS = { |
| 23 | + apply(_target, _that, args) { |
| 24 | + const [, name] = args; |
| 25 | + |
| 26 | + args[1] = escape.get(name); |
| 27 | + |
| 28 | + return Reflect.apply(...arguments); |
| 29 | + }, |
| 30 | + }; |
| 31 | + const removeAttr = { |
| 32 | + apply(_target, _that, args) { |
| 33 | + // Remove |
| 34 | + Reflect.apply(...arguments); |
| 35 | + |
| 36 | + // Remove the backup too |
| 37 | + const [name] = args; |
| 38 | + if (name.includes(escapeAttrs)) args[0] = `_${name}`; |
| 39 | + |
| 40 | + Reflect.apply(...arguments); |
| 41 | + }, |
| 42 | + }; |
| 43 | + const removeAttrNS = { |
| 44 | + apply(_target, _that, args) { |
| 45 | + // Remove |
| 46 | + Reflect.apply(...arguments); |
| 47 | + |
| 48 | + // Remove the backup too |
| 49 | + const [, name] = args; |
| 50 | + if (name.includes(escapeAttrs)) args[1] = `_${name}`; |
| 51 | + |
| 52 | + Reflect.apply(...arguments); |
| 53 | + }, |
| 54 | + }; |
| 55 | + |
| 56 | + window.Element.hasAttribute = new Proxy( |
| 57 | + Element.prototype.hasAttribute, |
| 58 | + attr |
| 59 | + ); |
| 60 | + window.Element.hasAttribute = new Proxy( |
| 61 | + Element.prototype.hasAttribute, |
| 62 | + attr |
| 63 | + ); |
| 64 | + window.Element.hasAttributeNS = new Proxy( |
| 65 | + Element.prototype.hasAttribute, |
| 66 | + attrNS |
| 67 | + ); |
| 68 | + window.Element.getAttribute = new Proxy( |
| 69 | + Element.prototype.getAttribute, |
| 70 | + attr |
| 71 | + ); |
| 72 | + window.Element.getAttributeNode = new Proxy( |
| 73 | + Element.prototype.getAttributeNode, |
| 74 | + attr |
| 75 | + ); |
| 76 | + window.Element.getAttributeNS = new Proxy( |
| 77 | + Element.prototype.getAttribute, |
| 78 | + attrNS |
| 79 | + ); |
| 80 | + window.Element.getAttributeNodeNS = new Proxy( |
| 81 | + Element.prototype.getAttribute, |
| 82 | + attrNS |
| 83 | + ); |
| 84 | + window.Element.getAttributeNames = new Proxy( |
| 85 | + Element.prototype.getAttributeNames, |
| 86 | + { |
| 87 | + apply(target) { |
| 88 | + return Reflect.apply(...arguments) |
| 89 | + .filter(attr => attr === "_href") |
| 90 | + .map(attr => |
| 91 | + (target instanceof HTMLAnchorElement && |
| 92 | + $aero.escape("href").test(attr)) || |
| 93 | + (target instanceof HTMLScriptElement && |
| 94 | + $aero.escape("integrity").test(attr)) |
| 95 | + ? attr.slice(1) |
| 96 | + : attr |
| 97 | + ); |
| 98 | + }, |
| 99 | + } |
| 100 | + ); |
| 101 | + // Element.toggleAttribute |
| 102 | + window.Element.prototype.toggleAttribute = new Proxy( |
| 103 | + window.Element.prototype.toggleAttribute, |
| 104 | + removeAttr |
| 105 | + ); |
| 106 | + window.Element.prototype.removeAttribute = new Proxy( |
| 107 | + window.Element.prototype.removeAttribute, |
| 108 | + removeAttr |
| 109 | + ); |
| 110 | + window.Element.prototype.removeAttributeNS = new Proxy( |
| 111 | + window.Element.prototype.removeAttribute, |
| 112 | + removeAttrNS |
| 113 | + ); |
| 114 | + |
| 115 | + function conceal(attr) { |
| 116 | + Object.defineProperty(window.Element, attr, { |
| 117 | + get: () => undefined, |
| 118 | + }); |
| 119 | + } |
| 120 | + conceal("_href"); |
| 121 | + conceal("_xlink:href"); |
| 122 | + conceal("_integrity"); |
| 123 | +} |
0 commit comments