Merge pull request #5366 from Arjan-P/fix-selection-across-node

Fix selection across DOM node
This commit is contained in:
Oliver Blanthorn 2026-04-16 07:46:36 +00:00 committed by GitHub
commit 9cac02f2f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 26 deletions

View file

@ -401,33 +401,11 @@ export class default_config {
S: "composite js document.getSelection().toString() | fillcmdline tabopen search",
l: `js
const sel = document.getSelection();
const oldFocusNode = sel.focusNode;
const oldFocusOffset = sel.focusOffset;
sel.modify("extend", "forward", "character");
if (sel.isCollapsed) {
sel.setBaseAndExtent(
oldFocusNode,
oldFocusOffset,
sel.focusNode,
sel.focusOffset + 1
);
}
tri.visual.extendByCharacter(sel, "forward");
`,
h: `js
const sel = document.getSelection();
const oldFocusNode = sel.focusNode;
const oldFocusOffset = sel.focusOffset;
sel.modify("extend", "backward", "character");
if (sel.isCollapsed) {
sel.setBaseAndExtent(
oldFocusNode,
oldFocusOffset,
sel.focusNode,
sel.focusOffset - 1
);
}
tri.visual.extendByCharacter(sel, "backward");
`,
e: 'js document.getSelection().modify("extend","forward","word")',
w: 'js document.getSelection().modify("extend","forward","word"); document.getSelection().modify("extend","forward","word"); document.getSelection().modify("extend","backward","word"); document.getSelection().modify("extend","forward","character")',

View file

@ -35,3 +35,25 @@ function getSelectionDirection(selection: Selection): boolean {
range.setEnd(focusNode, selection.focusOffset)
return !range.collapsed
}
export function extendByCharacter(
selection: Selection,
direction: "forward" | "backward" | "left" | "right",
): void {
const oldFocusNode = selection.focusNode
const oldFocusOffset = selection.focusOffset
const oldAnchorNode = selection.anchorNode
const oldAnchorOffset = selection.anchorOffset
if (!oldFocusNode) return
selection.modify("extend", direction, "character")
if (!selection.isCollapsed) return
selection.setBaseAndExtent(
oldAnchorNode,
oldAnchorOffset,
oldFocusNode,
oldFocusOffset,
)
reverseSelection(selection)
selection.modify("extend", direction, "character")
}

11
src/tridactyl.d.ts vendored
View file

@ -15,6 +15,13 @@ interface Window {
scrollByPages(n: number): void
eval(str: string): any
}
interface Selection {
modify(
alter: "move" | "extend",
direction: "forward" | "backward" | "left" | "right",
granularity: "character" | "word" | "line",
): void
}
// Record that we've added a property with convenience objects to the
// window object:
@ -47,7 +54,7 @@ interface findResult {
interface HTMLElement {
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/inert
inert: boolean;
inert: boolean
// Let's be future proof:
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus
@ -84,7 +91,7 @@ declare namespace browser.tabs {
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/d1180e5218a7bf69e6f0da5ac2e2584bd57a1cdf/types/firefox-webext-browser/index.d.ts
interface WebExtEventBase<
TAddListener extends (...args: any[]) => any,
TCallback
TCallback,
> {
addListener: TAddListener