diff --git a/packages/happy-dom/src/selection/Selection.ts b/packages/happy-dom/src/selection/Selection.ts index ddfd092b1..cc9fac6b9 100644 --- a/packages/happy-dom/src/selection/Selection.ts +++ b/packages/happy-dom/src/selection/Selection.ts @@ -256,7 +256,10 @@ export default class Selection { ); } - if (node[PropertySymbol.ownerDocument] !== this.#ownerDocument) { + if ( + node !== this.#ownerDocument && + node[PropertySymbol.ownerDocument] !== this.#ownerDocument + ) { return; } @@ -378,7 +381,10 @@ export default class Selection { * @param offset Offset. */ public extend(node: Node, offset: number): void { - if (node[PropertySymbol.ownerDocument] !== this.#ownerDocument) { + if ( + node !== this.#ownerDocument && + node[PropertySymbol.ownerDocument] !== this.#ownerDocument + ) { return; } @@ -441,7 +447,10 @@ export default class Selection { ); } - if (node[PropertySymbol.ownerDocument] !== this.#ownerDocument) { + if ( + node !== this.#ownerDocument && + node[PropertySymbol.ownerDocument] !== this.#ownerDocument + ) { return; } @@ -482,8 +491,10 @@ export default class Selection { } if ( - anchorNode[PropertySymbol.ownerDocument] !== this.#ownerDocument || - focusNode[PropertySymbol.ownerDocument] !== this.#ownerDocument + (anchorNode !== this.#ownerDocument && + anchorNode[PropertySymbol.ownerDocument] !== this.#ownerDocument) || + (focusNode !== this.#ownerDocument && + focusNode[PropertySymbol.ownerDocument] !== this.#ownerDocument) ) { return; } diff --git a/packages/happy-dom/test/selection/Selection.test.ts b/packages/happy-dom/test/selection/Selection.test.ts index 44948fa85..3b5ddf7be 100644 --- a/packages/happy-dom/test/selection/Selection.test.ts +++ b/packages/happy-dom/test/selection/Selection.test.ts @@ -329,6 +329,19 @@ describe('Selection', () => { expect(((triggeredEvent)).bubbles).toBe(false); expect(((triggeredEvent)).cancelable).toBe(false); }); + + it('Accepts Document node.', () => { + selection[method](document, 0); + + expect(selection.rangeCount).toBe(1); + + const newRange = selection.getRangeAt(0); + + expect(newRange.startContainer).toBe(document); + expect(newRange.startOffset).toBe(0); + expect(newRange.endContainer).toBe(document); + expect(newRange.endOffset).toBe(0); + }); }); } @@ -569,6 +582,19 @@ describe('Selection', () => { expect(((triggeredEvent)).bubbles).toBe(false); expect(((triggeredEvent)).cancelable).toBe(false); }); + + it('Accepts Document node.', () => { + const text = document.createTextNode('text'); + + document.body.appendChild(text); + + selection.collapse(text, 0); + selection.extend(document, 0); + + expect(selection.rangeCount).toBe(1); + expect(selection.focusNode).toBe(document); + expect(selection.focusOffset).toBe(0); + }); }); describe('selectAllChildren()', () => { @@ -592,6 +618,19 @@ describe('Selection', () => { expect(newRange.endOffset).toBe(3); }); + it('Accepts Document node.', () => { + selection.selectAllChildren(document); + + expect(selection.rangeCount).toBe(1); + + const newRange = selection.getRangeAt(0); + + expect(newRange.startContainer).toBe(document); + expect(newRange.startOffset).toBe(0); + expect(newRange.endContainer).toBe(document); + expect(newRange.endOffset).toBe(document.childNodes.length); + }); + it(`Throws error if node type is ${NodeTypeEnum.documentTypeNode}.`, () => { const documentType = document.implementation.createDocumentType('', '', ''); @@ -658,6 +697,21 @@ describe('Selection', () => { expect(selection.anchorNode).toBe(newRange.endContainer); }); + it('Accepts Document node as boundary point.', () => { + const text = document.createTextNode('text'); + + document.body.appendChild(text); + + selection.setBaseAndExtent(document, 0, text, 2); + + expect(selection.rangeCount).toBe(1); + + const newRange = selection.getRangeAt(0); + + expect(newRange.startContainer).toBe(document); + expect(newRange.endContainer).toBe(text); + }); + it('Throws error if wrong offset.', () => { const start = document.createTextNode('start'); const end = document.createTextNode('end');