Repository: capricorn86/happy-dom (ts)

Issue:
getElementsByClassName return wrong results when innerHTML ends with /n
**To Reproduce**
```ts
import { Window } from 'happy-dom';

const window = new Window();
const document = window.document;
document.body.innerHTML = `<div class="container"></div>/n`;
// document.body.innerHTML = `<div class="container"></div>
// `;
// document.body.innerHTML = `<div class="container"></div>`;

const container = document.querySelector('.container');

const div1 = document.createElement('div');
div1.classList.add('someClassName');
container.after(div1);

const div2 = document.createElement('div');
div2.classList.add('someClassName');
div1.after(div2);

const l1 = Array.from(document.getElementsByClassName('someClassName'));
console.log(l1.length); // output 3, wrong

const l2 = Array.from(document.querySelectorAll('.someClassName'));
console.log(l2.length); // output 2, OK

```

happy-dom: v9.1.9
node.js: v18.12.1


Pull request:
#846@patch: After add element correctly.
fix #846 

Patch:
diff --git a/packages/happy-dom/src/nodes/element/ElementUtility.ts b/packages/happy-dom/src/nodes/element/ElementUtility.ts
index 92395425..25d3b899 100644
--- a/packages/happy-dom/src/nodes/element/ElementUtility.ts
+++ b/packages/happy-dom/src/nodes/element/ElementUtility.ts
@@ -119,6 +119,8 @@ export default class ElementUtility {
 						parentElement.children.splice(index, 0, <IElement>newNode);
 					}
 				} else {
+					parentElement.children.length = 0;
+
 					for (const node of parentElement.childNodes) {
 						if (node === referenceNode) {
 							parentElement.children.push(<IElement>newNode);


Test patch:
diff --git a/packages/happy-dom/test/nodes/element/Element.test.ts b/packages/happy-dom/test/nodes/element/Element.test.ts
index a6226188..f76ec839 100644
--- a/packages/happy-dom/test/nodes/element/Element.test.ts
+++ b/packages/happy-dom/test/nodes/element/Element.test.ts
@@ -1076,6 +1076,45 @@ describe('Element', () => {
 			expect(element.children[2] === span).toBe(true);
 		});
 
+		it('After should add child element correctly', () => {
+			document.body.innerHTML = `<div class="container"></div>\n`;
+			expect(document.body.children.length).toBe(1);
+			const container = document.querySelector('.container');
+
+			const div1 = document.createElement('div');
+			div1.classList.add('someClassName');
+			div1.innerHTML = 'div1';
+			container.after(div1);
+			expect(document.body.children.length).toBe(2);
+
+			const div2 = document.createElement('div');
+			div2.classList.add('someClassName');
+			div2.innerHTML = 'div2';
+			div1.after(div2);
+
+			expect(document.body.children.length).toBe(3);
+			expect(document.body.children[1] === div1).toBe(true);
+			expect(document.body.children[2] === div2).toBe(true);
+			expect(document.getElementsByClassName('someClassName').length).toBe(2);
+		});
+
+		it('Insert before comment node should be at the correct location.', () => {
+			const span1 = document.createElement('span');
+			const span2 = document.createElement('span');
+			const span3 = document.createElement('span');
+			const comment = document.createComment('test');
+
+			element.appendChild(span1);
+			element.appendChild(comment);
+			element.appendChild(span2);
+			element.insertBefore(span3, comment);
+
+			expect(element.children.length).toBe(3);
+			expect(element.children[0] === span1).toBe(true);
+			expect(element.children[1] === span3).toBe(true);
+			expect(element.children[2] === span2).toBe(true);
+		});
+
 		// See: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
 		it('Insert the children instead of the actual element before another reference Node if the type is DocumentFragment.', () => {
 			const child1 = document.createElement('span');