From 36ce7ed9e987190f08547cfe0863dece19b3307f Mon Sep 17 00:00:00 2001 From: Rodrigo Rodrigues Date: Fri, 1 Mar 2024 20:02:07 -0300 Subject: [PATCH] fix: [#1285] Prevent contains from accessing properties of undefined arguments --- packages/happy-dom/src/nodes/node/Node.ts | 5 ++++- packages/happy-dom/test/nodes/node/Node.test.ts | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/happy-dom/src/nodes/node/Node.ts b/packages/happy-dom/src/nodes/node/Node.ts index ff19017d3..ad00b175f 100644 --- a/packages/happy-dom/src/nodes/node/Node.ts +++ b/packages/happy-dom/src/nodes/node/Node.ts @@ -298,7 +298,10 @@ export default class Node extends EventTarget implements INode { * @param otherNode Node to test with. * @returns "true" if this node contains the other node. */ - public contains(otherNode: INode): boolean { + public contains(otherNode: INode | undefined): boolean { + if (otherNode === undefined) { + return false; + } return NodeUtility.isInclusiveAncestor(this, otherNode); } diff --git a/packages/happy-dom/test/nodes/node/Node.test.ts b/packages/happy-dom/test/nodes/node/Node.test.ts index f840ad095..58f1586b3 100644 --- a/packages/happy-dom/test/nodes/node/Node.test.ts +++ b/packages/happy-dom/test/nodes/node/Node.test.ts @@ -330,6 +330,11 @@ describe('Node', () => { expect(div.contains(null)).toBe(false); }); + it('Returns "false" if match node is undefined.', () => { + const div = document.createElement('div'); + + expect(div.contains(undefined)).toBe(false); + }); }); describe('getRootNode()', () => {