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 26f5838
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions packages/happy-dom/src/nodes/element/Element.ts
Expand Up @@ -14,14 +14,13 @@ import XMLSerializer from '../../xml-serializer/XMLSerializer';
import ChildNodeUtility from '../child-node/ChildNodeUtility';
import ParentNodeUtility from '../parent-node/ParentNodeUtility';
import NonDocumentChildNodeUtility from '../child-node/NonDocumentChildNodeUtility';
import IElement from './IElement';
import IElement, { TInsertAdjacentPositions } from './IElement';
import DOMException from '../../exception/DOMException';
import IShadowRoot from '../shadow-root/IShadowRoot';
import INode from '../node/INode';
import IDocument from '../document/IDocument';
import IHTMLCollection from './IHTMLCollection';
import INodeList from '../node/INodeList';
import { TInsertAdjacentPositions } from './IElement';
import IText from '../text/IText';
import IDOMRectList from './IDOMRectList';
import DOMRectListFactory from './DOMRectListFactory';
Expand Down Expand Up @@ -255,7 +254,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 @@ -404,7 +417,8 @@ export default class Element extends Node implements IElement {
}

/**
* The Node.replaceWith() method replaces this Node in the children list of its parent with a set of Node or DOMString objects.
* The Node.replaceWith() method replaces this Node in the children list of its parent with a set of Node or
* DOMString objects.
*
* @param nodes List of Node or DOMString.
*/
Expand All @@ -413,7 +427,8 @@ export default class Element extends Node implements IElement {
}

/**
* Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just before this ChildNode. DOMString objects are inserted as equivalent Text nodes.
* Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just before this
* ChildNode. DOMString objects are inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
Expand All @@ -422,7 +437,8 @@ export default class Element extends Node implements IElement {
}

/**
* Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just after this ChildNode. DOMString objects are inserted as equivalent Text nodes.
* Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent, just after this
* ChildNode. DOMString objects are inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
Expand All @@ -431,7 +447,8 @@ export default class Element extends Node implements IElement {
}

/**
* Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
* Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are
* inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
Expand All @@ -440,7 +457,8 @@ export default class Element extends Node implements IElement {
}

/**
* Inserts a set of Node objects or DOMString objects before the first child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
* Inserts a set of Node objects or DOMString objects before the first child of the ParentNode. DOMString objects are
* inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
Expand Down Expand Up @@ -564,7 +582,8 @@ export default class Element extends Node implements IElement {
* Returns `true` if attribute name is eventually present, and `false` otherwise.
*
* @param name A DOMString specifying the name of the attribute to be toggled.
* @param force A boolean value to determine whether the attribute should be added or removed, no matter whether the attribute is present or not at the moment.
* @param force A boolean value to determine whether the attribute should be added or removed, no matter whether the
* attribute is present or not at the moment.
*/
public toggleAttribute(name: string, force?: boolean): boolean {
name = name.toLowerCase();
Expand Down Expand Up @@ -727,7 +746,8 @@ export default class Element extends Node implements IElement {
}

/**
* Traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string.
* Traverses the Element and its parents (heading toward the document root) until it finds a node that matches the
* provided selector string.
*
* @param selector Selector.
* @returns Closest matching element.
Expand Down

0 comments on commit 26f5838

Please sign in to comment.