Skip to content

Commit

Permalink
capricorn86#728@patch: Use proxy for attributes getter.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbach committed Apr 25, 2023
1 parent 1a827e3 commit 4fdef3f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/happy-dom/src/nodes/element/Element.ts
Expand Up @@ -255,7 +255,21 @@ export default class Element extends Node implements IElement {
* @returns Attributes.
*/
public get attributes(): INamedNodeMap {
return Object.assign(new NamedNodeMap(this), Object.values(this._attributes), this._attributes);
const nodeMap = new NamedNodeMap(this);
return new Proxy(nodeMap, {
get: (target, name) => {
if (name in target && typeof target[name] === 'function') {
return (...args) => target[name](...args);
}

return (
target[name] ||
(typeof name === 'string' || typeof name === 'number'
? this._attributes[name]
: undefined)
);
}
});
}

/**
Expand Down Expand Up @@ -1076,4 +1090,4 @@ export default class Element extends Node implements IElement {
}
return name.toLowerCase();
}
}
}

0 comments on commit 4fdef3f

Please sign in to comment.