Repository: capricorn86/happy-dom (TypeScript)

Issue:
RangeError: Invalid array length
**Describe the bug**
I use innerHTML to insert a piece of html, then error occurs.

**To Reproduce**
Steps to reproduce the behavior:

just use node to run code below:
```js
import { GlobalRegistrator } from '@happy-dom/global-registrator'
GlobalRegistrator.register()
document.write(`<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <title></title>
  </head>
  <body>
    <div id="app">
    </div>
  </body>
</html>
`)
document.getElementById('app').innerHTML = `<a name="length"></a>`

```

**Expected behavior**
no error

**Screenshots**
```log

   this[name] = this._namedItems[name][0];
                           ^

RangeError: Invalid array length
    at HTMLCollection._appendNamedItem (/mnt/c/node_modules/.pnpm/happy-dom@9.9.2/node_modules/happy-dom/lib/nodes/element/HTMLCollection.js:43:28)
    at ElementUtility.appendChild (/mnt/node_modules/.pnpm/happy-dom@9.9.2/node_modules/happy-dom/lib/nodes/element/ElementUtility.js:43:43)
    at DocumentFragment.appendChild (/mnt/c/node_modules/.pnpm/happy-dom@9.9.2/node_modules/happy-dom/lib/nodes/document-fragment/DocumentFragment.js:146:41)
    at XMLParser.parse (/mnt/c/node_modules/.pnpm/happy-dom@9.9.2/node_modules/happy-dom/lib/xml-parser/XMLParser.js:91:41)
    at set innerHTML [as innerHTML] (/mnt/c/node_modules/.pnpm/happy-dom@9.9.2/node_modules/happy-dom/lib/nodes/element/Element.js:200:48)
    at render (file:///mnt/c/src/test.mjs:74:46)
```

**Device:**
 - windows 11
 - node 18.15.0

**Additional context**
I think the key point of the bug is the attribute `name="length"`, if I change the attribute name("name") or the value("length",) the bug disappears.



Discussion: Thank you for reporting @lovetingyuan! :slightly_smiling_face: 

The issue has been fixed now.

This was a pretty serious bug. Hopefully it is not common to use names or id:s with the same name as methods and properties of the HTMLCollection class.

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v9.10.5



Fix (patch):
diff --git a/packages/happy-dom/src/nodes/element/HTMLCollection.ts b/packages/happy-dom/src/nodes/element/HTMLCollection.ts
index b9b6ac147..eb66f68e6 100644
--- a/packages/happy-dom/src/nodes/element/HTMLCollection.ts
+++ b/packages/happy-dom/src/nodes/element/HTMLCollection.ts
@@ -41,7 +41,7 @@ export default class HTMLCollection<T> extends Array implements IHTMLCollection<
 				this._namedItems[name].push(node);
 			}
 
-			if (!this[name] && this._isValidPropertyName(name)) {
+			if (!this.hasOwnProperty(name) && this._isValidPropertyName(name)) {
 				this[name] = this._namedItems[name][0];
 			}
 		}
@@ -62,7 +62,7 @@ export default class HTMLCollection<T> extends Array implements IHTMLCollection<
 
 				if (this._namedItems[name].length === 0) {
 					delete this._namedItems[name];
-					if (this[name] && this._isValidPropertyName(name)) {
+					if (this.hasOwnProperty(name) && this._isValidPropertyName(name)) {
 						delete this[name];
 					}
 				} else if (this._isValidPropertyName(name)) {
@@ -79,6 +79,10 @@ export default class HTMLCollection<T> extends Array implements IHTMLCollection<
 	 * @returns True if the property name is valid.
 	 */
 	protected _isValidPropertyName(name: string): boolean {
-		return isNaN(Number(name)) || name.includes('.');
+		return (
+			!this.constructor.prototype.hasOwnProperty(name) &&
+			!Array.prototype.hasOwnProperty(name) &&
+			(isNaN(Number(name)) || name.includes('.'))
+		);
 	}
 }
diff --git a/packages/happy-dom/src/nodes/html-form-element/HTMLFormControlsCollection.ts b/packages/happy-dom/src/nodes/html-form-element/HTMLFormControlsCollection.ts
index a45a78601..338abb1c8 100644
--- a/packages/happy-dom/src/nodes/html-form-element/HTMLFormControlsCollection.ts
+++ b/packages/happy-dom/src/nodes/html-form-element/HTMLFormControlsCollection.ts
@@ -93,7 +93,7 @@ export default class HTMLFormControlsCollection
 
 				if (this._namedItems[name].length === 0) {
 					delete this._namedItems[name];
-					if (this[name] && this._isValidPropertyName(name)) {
+					if (this.hasOwnProperty(name) && this._isValidPropertyName(name)) {
 						delete this[name];
 					}
 				} else if (this._isValidPropertyName(name)) {
@@ -111,6 +111,10 @@ export default class HTMLFormControlsCollection
 	 * @returns True if the property name is valid.
 	 */
 	protected _isValidPropertyName(name: string): boolean {
-		return isNaN(Number(name)) || name.includes('.');
+		return (
+			!this.constructor.prototype.hasOwnProperty(name) &&
+			!Array.prototype.hasOwnProperty(name) &&
+			(isNaN(Number(name)) || name.includes('.'))
+		);
 	}
 }