Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/happy-dom/src/selection/Selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ export default class Selection {
);
}

if (node[PropertySymbol.ownerDocument] !== this.#ownerDocument) {
if (
node !== this.#ownerDocument &&
node[PropertySymbol.ownerDocument] !== this.#ownerDocument
) {
return;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -441,7 +447,10 @@ export default class Selection {
);
}

if (node[PropertySymbol.ownerDocument] !== this.#ownerDocument) {
if (
node !== this.#ownerDocument &&
node[PropertySymbol.ownerDocument] !== this.#ownerDocument
) {
return;
}

Expand Down Expand Up @@ -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;
}
Expand Down
54 changes: 54 additions & 0 deletions packages/happy-dom/test/selection/Selection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,19 @@ describe('Selection', () => {
expect((<Event>(<unknown>triggeredEvent)).bubbles).toBe(false);
expect((<Event>(<unknown>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);
});
});
}

Expand Down Expand Up @@ -569,6 +582,19 @@ describe('Selection', () => {
expect((<Event>(<unknown>triggeredEvent)).bubbles).toBe(false);
expect((<Event>(<unknown>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()', () => {
Expand All @@ -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('', '', '');

Expand Down Expand Up @@ -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');
Expand Down