Skip to content

Commit

Permalink
#450@major: Adds support for Document.createRange(). Adds integration…
Browse files Browse the repository at this point in the history
… for Document.getSelection(). Fixes issue preventing usage of multiple Window instances.
  • Loading branch information
capricorn86 committed Jun 30, 2022
1 parent 3d9a227 commit 16a7c2f
Show file tree
Hide file tree
Showing 5 changed files with 735 additions and 77 deletions.
4 changes: 3 additions & 1 deletion packages/happy-dom/src/index.ts
Expand Up @@ -104,6 +104,7 @@ import Storage from './storage/Storage';
import DOMRect from './nodes/element/DOMRect';
import { URLSearchParams } from 'url';
import Selection from './selection/Selection';
import Range from './range/Range';

export {
GlobalWindow,
Expand Down Expand Up @@ -211,5 +212,6 @@ export {
Storage,
DOMRect,
URLSearchParams,
Selection
Selection,
Range
};
33 changes: 9 additions & 24 deletions packages/happy-dom/src/selection/Selection.ts
Expand Up @@ -102,7 +102,7 @@ export default class Selection {
* Returns anchor node.
*
* @deprecated
* @alias this.anchorNode
* @alias anchorNode
* @returns Node.
*/
public get baseNode(): INode {
Expand All @@ -113,7 +113,7 @@ export default class Selection {
* Returns anchor offset.
*
* @deprecated
* @alias this.anchorOffset
* @alias anchorOffset
* @returns Node.
*/
public get baseOffset(): number {
Expand All @@ -127,12 +127,7 @@ export default class Selection {
* @returns Node.
*/
public get focusNode(): INode {
if (!this._range) {
return null;
}
return this._direction === SelectionDirectionEnum.forwards
? this._range.startContainer
: this._range.endContainer;
return this.anchorNode;
}

/**
Expand All @@ -142,19 +137,14 @@ export default class Selection {
* @returns Node.
*/
public get focusOffset(): number {
if (!this._range) {
return null;
}
return this._direction === SelectionDirectionEnum.forwards
? this._range.startOffset
: this._range.endOffset;
return this.anchorOffset;
}

/**
* Returns focus node.
*
* @deprecated
* @alias this.focusNode
* @alias focusNode
* @returns Node.
*/
public get extentNode(): INode {
Expand All @@ -165,7 +155,7 @@ export default class Selection {
* Returns focus offset.
*
* @deprecated
* @alias this.focusOffset
* @alias focusOffset
* @returns Node.
*/
public get extentOffset(): number {
Expand Down Expand Up @@ -225,7 +215,7 @@ export default class Selection {
/**
* Removes all ranges.
*
* @alias this.removeAllRanges()
* @alias removeAllRanges()
*/
public empty(): void {
this.removeAllRanges();
Expand Down Expand Up @@ -273,7 +263,7 @@ export default class Selection {
* Collapses the current selection to a single point.
*
* @see https://w3c.github.io/selection-api/#dom-selection-setposition
* @alias this.collapse()
* @alias collapse()
* @param node Node.
* @param offset Offset.
*/
Expand Down Expand Up @@ -528,12 +518,7 @@ export default class Selection {

if (oldRange !== this._range) {
// https://w3c.github.io/selection-api/#selectionchange-event
this._ownerDocument.dispatchEvent(
new Event('selectionchange', {
bubbles: false,
cancelable: false
})
);
this._ownerDocument.dispatchEvent(new Event('selectionchange'));
}
}
}
15 changes: 14 additions & 1 deletion packages/happy-dom/test/nodes/document/Document.test.ts
Expand Up @@ -29,6 +29,7 @@ import DocumentReadyStateEnum from '../../../src/nodes/document/DocumentReadySta
import ISVGElement from '../../../src/nodes/svg-element/ISVGElement';
import CustomEvent from '../../../src/event/events/CustomEvent';
import Selection from '../../../src/selection/Selection';
import Range from '../../../src/range/Range';

/* eslint-disable jsdoc/require-jsdoc */

Expand Down Expand Up @@ -1071,9 +1072,21 @@ describe('Document', () => {
});

describe('getSelection()', () => {
it('Returns selection.', () => {
it('Returns an instance of Selection.', () => {
expect(document.getSelection() instanceof Selection).toBe(true);
});

it('Returns the same instance when called multiple times.', () => {
const selection1 = document.getSelection();
const selection2 = document.getSelection();
expect(selection1 === selection2).toBe(true);
});
});

describe('createRange()', () => {
it('Returns an instance of Range.', () => {
expect(document.createRange() instanceof Range).toBe(true);
});
});

describe('hasFocus()', () => {
Expand Down

0 comments on commit 16a7c2f

Please sign in to comment.