Repository: capricorn86/happy-dom (ts)

Issue:
Text.splitText does not throw on invalid index
**Describe the bug**
Calling `Text.spliText` does not throw on invalid index as specified.

**To Reproduce**
`document.createTextNode("abc").splitText(-1)`

**Expected behavior**
Throws a `DOMexception`.

**Additional context**
I will prepare a PR.


Pull request:
#857@patch: Throw DOMException from Text.splitText.
fix #857 and unit tests for `Text.splittext`:
- the DOMException needs to be actually thrown
- missing check for negative index
- added unit test

Patch:
diff --git a/packages/happy-dom/src/nodes/text/Text.ts b/packages/happy-dom/src/nodes/text/Text.ts
index 19a8778f..51c10e67 100644
--- a/packages/happy-dom/src/nodes/text/Text.ts
+++ b/packages/happy-dom/src/nodes/text/Text.ts
@@ -43,15 +43,14 @@ export default class Text extends CharacterData implements IText {
 	 * Breaks the Text node into two nodes at the specified offset, keeping both nodes in the tree as siblings.
 	 *
 	 * @see https://dom.spec.whatwg.org/#dom-text-splittext
-	 * @see https://dom.spec.whatwg.org/#dom-text-splittext
 	 * @param offset Offset.
 	 * @returns New text node.
 	 */
 	public splitText(offset: number): IText {
 		const length = this._data.length;
 
-		if (offset > length) {
-			new DOMException(
+		if (offset < 0 || offset > length) {
+			throw new DOMException(
 				'The index is not in the allowed range.',
 				DOMExceptionNameEnum.indexSizeError
 			);


Test patch:
diff --git a/packages/happy-dom/test/nodes/text/Text.test.ts b/packages/happy-dom/test/nodes/text/Text.test.ts
index cf8bf9de..54df1a34 100644
--- a/packages/happy-dom/test/nodes/text/Text.test.ts
+++ b/packages/happy-dom/test/nodes/text/Text.test.ts
@@ -1,5 +1,7 @@
 import Window from '../../../src/window/Window';
 import Document from '../../../src/nodes/document/Document';
+import DOMException from '../../../src/exception/DOMException';
+import Text from '../../../src/nodes/text/Text';
 
 describe('Text', () => {
 	let window: Window;
@@ -17,6 +19,7 @@ describe('Text', () => {
 	describe('get nodeName()', () => {
 		it('Returns "#text".', () => {
 			const node = document.createTextNode('test');
+			expect(node).toBeInstanceOf(Text);
 			expect(node.nodeName).toBe('#text');
 		});
 	});
@@ -35,4 +38,22 @@ describe('Text', () => {
 			expect(clone.data).toBe(node.data);
 		});
 	});
+
+	describe('splitText()', () => {
+		it('Splits the text node.', () => {
+			const node = document.createTextNode('test');
+			document.body.append(node);
+			const result = node.splitText(2);
+			expect(node.textContent).toBe('te');
+			expect(result).toBeInstanceOf(Text);
+			expect(result.textContent).toBe('st');
+			expect(node.nextSibling).toBe(result);
+			expect(result.previousSibling).toBe(node);
+		});
+		it('Throws on invalid index.', () => {
+			const node = document.createTextNode('test');
+			expect(() => node.splitText(-1)).toThrow(DOMException);
+			expect(() => node.splitText(5)).toThrow(DOMException);
+		});
+	});
 });