Skip to content

Commit 1e8569b

Browse files
authored
Merge pull request #601 from saschanaz/selection
Add Selection API
2 parents d4492af + 43c2e32 commit 1e8569b

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

baselines/dom.generated.d.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4528,6 +4528,10 @@ interface Document extends Node, NonElementParentNode, DocumentOrShadowRoot, Par
45284528
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/1999/xhtml", localName: string): HTMLCollectionOf<HTMLElement>;
45294529
getElementsByTagNameNS(namespaceURI: "http://www.w3.org/2000/svg", localName: string): HTMLCollectionOf<SVGElement>;
45304530
getElementsByTagNameNS(namespaceURI: string, localName: string): HTMLCollectionOf<Element>;
4531+
/**
4532+
* Returns an object representing the current selection of the document that is loaded into the object displaying a webpage.
4533+
*/
4534+
getSelection(): Selection | null;
45314535
/**
45324536
* Gets a value indicating whether the object currently has focus.
45334537
*/
@@ -4583,9 +4587,6 @@ interface Document extends Node, NonElementParentNode, DocumentOrShadowRoot, Par
45834587
* @param content The text and HTML tags to write.
45844588
*/
45854589
writeln(...text: string[]): void;
4586-
/**
4587-
* Returns an object representing the current selection of the document that is loaded into the object displaying a webpage.
4588-
*/
45894590
addEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
45904591
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
45914592
removeEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
@@ -5408,6 +5409,8 @@ interface GlobalEventHandlersEventMap {
54085409
"seeked": Event;
54095410
"seeking": Event;
54105411
"select": Event;
5412+
"selectionchange": Event;
5413+
"selectstart": Event;
54115414
"stalled": Event;
54125415
"submit": Event;
54135416
"suspend": Event;
@@ -5655,6 +5658,8 @@ interface GlobalEventHandlers {
56555658
* @param ev The event.
56565659
*/
56575660
onselect: ((this: GlobalEventHandlers, ev: Event) => any) | null;
5661+
onselectionchange: ((this: GlobalEventHandlers, ev: Event) => any) | null;
5662+
onselectstart: ((this: GlobalEventHandlers, ev: Event) => any) | null;
56585663
/**
56595664
* Occurs when the download has stopped.
56605665
* @param ev The event.
@@ -14461,32 +14466,27 @@ declare var SecurityPolicyViolationEvent: {
1446114466

1446214467
/** A Selection object represents the range of text selected by the user or the current position of the caret. To obtain a Selection object for examination or modification, call Window.getSelection(). */
1446314468
interface Selection {
14464-
readonly anchorNode: Node;
14469+
readonly anchorNode: Node | null;
1446514470
readonly anchorOffset: number;
14466-
readonly baseNode: Node;
14467-
readonly baseOffset: number;
14468-
readonly extentNode: Node;
14469-
readonly extentOffset: number;
14470-
readonly focusNode: Node;
14471+
readonly focusNode: Node | null;
1447114472
readonly focusOffset: number;
1447214473
readonly isCollapsed: boolean;
1447314474
readonly rangeCount: number;
1447414475
readonly type: string;
1447514476
addRange(range: Range): void;
14476-
collapse(parentNode: Node, offset: number): void;
14477+
collapse(node: Node | null, offset?: number): void;
1447714478
collapseToEnd(): void;
1447814479
collapseToStart(): void;
14479-
containsNode(node: Node, partlyContained: boolean): boolean;
14480+
containsNode(node: Node, allowPartialContainment?: boolean): boolean;
1448014481
deleteFromDocument(): void;
1448114482
empty(): void;
14482-
extend(newNode: Node, offset: number): void;
14483+
extend(node: Node, offset?: number): void;
1448314484
getRangeAt(index: number): Range;
1448414485
removeAllRanges(): void;
1448514486
removeRange(range: Range): void;
14486-
selectAllChildren(parentNode: Node): void;
14487-
setBaseAndExtent(baseNode: Node, baseOffset: number, extentNode: Node, extentOffset: number): void;
14488-
setPosition(parentNode: Node, offset: number): void;
14489-
toString(): string;
14487+
selectAllChildren(node: Node): void;
14488+
setBaseAndExtent(anchorNode: Node, anchorOffset: number, focusNode: Node, focusOffset: number): void;
14489+
setPosition(node: Node | null, offset?: number): void;
1449014490
}
1449114491

1449214492
declare var Selection: {
@@ -16979,7 +16979,7 @@ interface Window extends EventTarget, WindowTimers, WindowSessionStorage, Window
1697916979
focus(): void;
1698016980
getComputedStyle(elt: Element, pseudoElt?: string | null): CSSStyleDeclaration;
1698116981
getMatchedCSSRules(elt: Element, pseudoElt?: string | null): CSSRuleList;
16982-
getSelection(): Selection;
16982+
getSelection(): Selection | null;
1698316983
matchMedia(query: string): MediaQueryList;
1698416984
moveBy(x: number, y: number): void;
1698516985
moveTo(x: number, y: number): void;
@@ -17855,7 +17855,7 @@ declare function departFocus(navigationReason: NavigationReason, origin: FocusNa
1785517855
declare function focus(): void;
1785617856
declare function getComputedStyle(elt: Element, pseudoElt?: string | null): CSSStyleDeclaration;
1785717857
declare function getMatchedCSSRules(elt: Element, pseudoElt?: string | null): CSSRuleList;
17858-
declare function getSelection(): Selection;
17858+
declare function getSelection(): Selection | null;
1785917859
declare function matchMedia(query: string): MediaQueryList;
1786017860
declare function moveBy(x: number, y: number): void;
1786117861
declare function moveTo(x: number, y: number): void;
@@ -18117,6 +18117,8 @@ declare var onseeking: ((this: Window, ev: Event) => any) | null;
1811718117
* @param ev The event.
1811818118
*/
1811918119
declare var onselect: ((this: Window, ev: Event) => any) | null;
18120+
declare var onselectionchange: ((this: Window, ev: Event) => any) | null;
18121+
declare var onselectstart: ((this: Window, ev: Event) => any) | null;
1812018122
/**
1812118123
* Occurs when the download has stopped.
1812218124
* @param ev The event.

inputfiles/idl/Selection.widl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
interface Selection {
2+
readonly attribute Node? anchorNode;
3+
readonly attribute unsigned long anchorOffset;
4+
readonly attribute Node? focusNode;
5+
readonly attribute unsigned long focusOffset;
6+
readonly attribute boolean isCollapsed;
7+
readonly attribute unsigned long rangeCount;
8+
readonly attribute DOMString type;
9+
Range getRangeAt(unsigned long index);
10+
void addRange(Range range);
11+
void removeRange(Range range);
12+
void removeAllRanges();
13+
void empty();
14+
void collapse(Node? node, optional unsigned long offset = 0);
15+
void setPosition(Node? node, optional unsigned long offset = 0);
16+
void collapseToStart();
17+
void collapseToEnd();
18+
void extend(Node node, optional unsigned long offset = 0);
19+
void setBaseAndExtent(Node anchorNode, unsigned long anchorOffset, Node focusNode, unsigned long focusOffset);
20+
void selectAllChildren(Node node);
21+
[CEReactions]
22+
void deleteFromDocument();
23+
boolean containsNode(Node node, optional boolean allowPartialContainment = false);
24+
stringifier DOMString ();
25+
};
26+
27+
partial interface Document {
28+
Selection? getSelection();
29+
};
30+
31+
partial interface Window {
32+
Selection? getSelection();
33+
};
34+
35+
partial interface GlobalEventHandlers {
36+
attribute EventHandler onselectstart;
37+
attribute EventHandler onselectionchange;
38+
};

inputfiles/idlSources.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@
218218
"url": "https://www.w3.org/TR/screen-orientation/",
219219
"title": "Screen Orientation"
220220
},
221+
{
222+
"url": "https://www.w3.org/TR/selection-api/",
223+
"title": "Selection"
224+
},
221225
{
222226
"url": "https://w3c.github.io/ServiceWorker/",
223227
"title": "Service Workers"

0 commit comments

Comments
 (0)