Skip to content

Commit 8f3097e

Browse files
committed
Remove util fn addElementsToPage, and rename refactors
This utils function wasn't pulling its weight.
1 parent 13b2bc9 commit 8f3097e

File tree

4 files changed

+31
-35
lines changed

4 files changed

+31
-35
lines changed

content_scripts/link_hints.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ class LinkHintsMode {
361361
// We need documentElement to be ready in order to append links.
362362
if (!document.documentElement) return;
363363

364-
this.hintMarkerContainingDiv = null;
364+
this.containerEl = null;
365365
// Function that does the appropriate action on the selected link.
366366
this.linkActivator = undefined;
367367
// The link-hints "mode" (in the key-handler, indicator sense).
@@ -407,20 +407,28 @@ class LinkHintsMode {
407407
}
408408

409409
renderHints() {
410+
if (this.containerEl == null) {
411+
const div = DomUtils.createElement("div");
412+
div.id = "vimiumHintMarkerContainer";
413+
div.className = "vimiumReset";
414+
this.containerEl = div;
415+
document.documentElement.appendChild(div);
416+
}
417+
410418
// Append these markers as top level children instead of as child nodes to the link itself,
411419
// because some clickable elements cannot contain children, e.g. submit buttons.
412-
this.hintMarkerContainingDiv = DomUtils.addElementsToPage(
413-
this.hintMarkers.filter((m) => m.isLocalMarker()).map((m) => m.element),
414-
{ id: "vimiumHintMarkerContainer", className: "vimiumReset" }
415-
);
420+
const markerEls = this.hintMarkers.filter((m) => m.isLocalMarker()).map((m) => m.element);
421+
for (const el of markerEls) {
422+
this.containerEl.appendChild(el);
423+
}
416424

417425
// TODO(philc): 2024-03-27 Remove this hasPopoverSupport check once Firefox has popover support.
418426
// Also move this CSS into vimium.css.
419-
const hasPopoverSupport = this.hintMarkerContainingDiv.showPopover != null;
427+
const hasPopoverSupport = this.containerEl.showPopover != null;
420428
if (hasPopoverSupport) {
421-
this.hintMarkerContainingDiv.popover = "manual";
422-
this.hintMarkerContainingDiv.showPopover();
423-
Object.assign(this.hintMarkerContainingDiv.style, {
429+
this.containerEl.popover = "manual";
430+
this.containerEl.showPopover();
431+
Object.assign(this.containerEl.style, {
424432
top: 0,
425433
left: 0,
426434
position: "absolute",
@@ -762,10 +770,10 @@ class LinkHintsMode {
762770
}
763771

764772
removeHintMarkers() {
765-
if (this.hintMarkerContainingDiv) {
766-
DomUtils.removeElement(this.hintMarkerContainingDiv);
773+
if (this.containerEl) {
774+
DomUtils.removeElement(this.containerEl);
767775
}
768-
this.hintMarkerContainingDiv = null;
776+
this.containerEl = null;
769777
}
770778
}
771779

content_scripts/mode_normal.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,14 @@ class FocusSelector extends Mode {
509509
},
510510
});
511511

512-
this.hintContainingDiv = DomUtils.addElementsToPage(hints, {
513-
id: "vimiumInputMarkerContainer",
514-
className: "vimiumReset",
515-
});
512+
const div = DomUtils.createElement("div");
513+
div.id = "vimiumInputMarkerContainer";
514+
div.className = "vimiumReset";
515+
for (const el of hints) {
516+
div.appendChild(el);
517+
}
518+
this.hintContainerEl = div;
519+
document.documentElement.appendChild(div);
516520

517521
DomUtils.simulateSelect(visibleInputs[selectedInputIndex].element);
518522
if (visibleInputs.length === 1) {
@@ -525,7 +529,7 @@ class FocusSelector extends Mode {
525529

526530
exit() {
527531
super.exit();
528-
DomUtils.removeElement(this.hintContainingDiv);
532+
DomUtils.removeElement(this.hintContainerEl);
529533
if (document.activeElement && DomUtils.isEditable(document.activeElement)) {
530534
return new InsertMode({
531535
singleton: "post-find-mode/focus-input",

lib/dom_utils.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,6 @@ const DomUtils = {
7979
}
8080
},
8181

82-
//
83-
// Adds a list of elements to a new container div, and adds that to the page.
84-
// Returns the container div.
85-
//
86-
// Note that adding these nodes all at once (via a parent div) is significantly faster than
87-
// one-by-one.
88-
addElementsToPage(elements, containerOptions) {
89-
// Don't create a new container if we already have one.
90-
const parent = document.getElementById(containerOptions.id) || this.createElement("div");
91-
if (containerOptions.id != null) parent.id = containerOptions.id;
92-
if (containerOptions.className != null) parent.className = containerOptions.className;
93-
for (const el of elements) parent.appendChild(el);
94-
document.documentElement.appendChild(parent);
95-
return parent;
96-
},
97-
9882
//
9983
// Remove an element from its DOM tree.
10084
//

tests/dom_tests/dom_tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ const createGeneralHintTests = (isFilteredMode) => {
7272

7373
should("create hints when activated, discard them when deactivated", () => {
7474
const mode = activateLinkHintsMode();
75-
assert.isFalse(mode.hintMarkerContainingDiv == null);
75+
assert.isFalse(mode.containerEl == null);
7676
mode.deactivateMode();
77-
assert.isTrue(mode.hintMarkerContainingDiv == null);
77+
assert.isTrue(mode.containerEl == null);
7878
});
7979

8080
should("position items correctly", () => {

0 commit comments

Comments
 (0)