Skip to content

Commit

Permalink
Merge pull request #1264 from aralroca/master
Browse files Browse the repository at this point in the history
feat: [#1263] Add isEqualNode method
  • Loading branch information
capricorn86 committed Feb 25, 2024
2 parents c2334e6 + 326241b commit 0d76bb3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/happy-dom/src/nodes/node/INode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@ export default interface INode extends IEventTarget {
* @returns True if the given node is equal to the current node, otherwise false.
*/
isSameNode(node: INode): boolean;

/**
* Compares two nodes.
*
* @param node Node to compare.
* @returns "true" if nodes are equal.
*/
isEqualNode(node: INode): boolean;
}
11 changes: 11 additions & 0 deletions packages/happy-dom/src/nodes/node/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ export default class Node extends EventTarget implements INode {
return oldChild;
}

/**
* Compares two nodes.
* Two nodes are equal if they have the same type, defining the same attributes, and so on.
*
* @param node Node to compare.
* @returns boolean - `true` if two nodes are equal.
*/
public isEqualNode(node: INode): boolean {
return NodeUtility.isEqualNode(this, node);
}

/**
* Converts the node to a string.
*
Expand Down
34 changes: 34 additions & 0 deletions packages/happy-dom/test/nodes/node/Node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,40 @@ describe('Node', () => {
});
});

describe('toEqualNode()', () => {
it('Returns "true" if the nodes are equal.', () => {
const div = document.createElement('div');
const span = document.createElement('span');
const text = document.createTextNode('text');
const comment = document.createComment('comment');

div.appendChild(span);
span.appendChild(text);
span.appendChild(comment);

const clone = div.cloneNode(true);

expect(div.isEqualNode(clone)).toBe(true);
});

it('Returns "false" if the nodes are not equal.', () => {
const div = document.createElement('div');
const span = document.createElement('span');
const text = document.createTextNode('text');
const comment = document.createComment('comment');

div.appendChild(span);
span.appendChild(text);
span.appendChild(comment);

const clone = div.cloneNode(true);

clone.appendChild(document.createElement('span'));

expect(div.isEqualNode(clone)).toBe(false);
});
});

describe('dispatchEvent()', () => {
it('Dispatches an event that is set to not bubble.', () => {
const child = document.createElement('span');
Expand Down

0 comments on commit 0d76bb3

Please sign in to comment.