From 8cdd20d8ce4c7455df93afa5c9e57e2d48f461ad Mon Sep 17 00:00:00 2001 From: weidenhoefer Date: Fri, 5 Jun 2026 08:31:32 +0200 Subject: [PATCH 1/2] Fix gap of upward opening dynamically created contextmenu The helper function getLineHeight calculates the height of a contextmenu entry by mocking two entries and divide the height of the container by two. But this does not take the padding of the container into account. Padding must be substracted from the offsetHeight. --- src/helpers/dom.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/helpers/dom.ts b/src/helpers/dom.ts index 609afcc..47a5c23 100644 --- a/src/helpers/dom.ts +++ b/src/helpers/dom.ts @@ -33,7 +33,11 @@ export function getLineHeight(container: HTMLDivElement): number { cloned.append(element2) container.parentNode?.append(cloned) - const height = cloned.offsetHeight / 2 + const clonedStyle = globalThis.getComputedStyle(cloned) + const paddingTop = Number.parseInt(clonedStyle.paddingTop, 10) + const paddingBottom = Number.parseInt(clonedStyle.paddingBottom, 10) + + const height = (cloned.offsetHeight - (paddingTop + paddingBottom)) / 2 container.parentNode?.removeChild(cloned) From 6774519b22c70be91288f094f9f5ab2e78fd1fdc Mon Sep 17 00:00:00 2001 From: Jonatas Walker Date: Wed, 17 Jun 2026 18:49:05 -0300 Subject: [PATCH 2/2] test: add line-height padding regression test; use getComputedStyle directly Covers the upward-menu gap fix in getLineHeight: the measured line height must be independent of the container's vertical padding. Also drops the unnecessary `globalThis.` qualifier to satisfy lint. Co-Authored-By: Claude Opus 4.8 --- src/helpers/dom.ts | 2 +- tests/dom.browser.spec.ts | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/dom.browser.spec.ts diff --git a/src/helpers/dom.ts b/src/helpers/dom.ts index 47a5c23..083e833 100644 --- a/src/helpers/dom.ts +++ b/src/helpers/dom.ts @@ -33,7 +33,7 @@ export function getLineHeight(container: HTMLDivElement): number { cloned.append(element2) container.parentNode?.append(cloned) - const clonedStyle = globalThis.getComputedStyle(cloned) + const clonedStyle = getComputedStyle(cloned) const paddingTop = Number.parseInt(clonedStyle.paddingTop, 10) const paddingBottom = Number.parseInt(clonedStyle.paddingBottom, 10) diff --git a/tests/dom.browser.spec.ts b/tests/dom.browser.spec.ts new file mode 100644 index 0000000..a37603e --- /dev/null +++ b/tests/dom.browser.spec.ts @@ -0,0 +1,41 @@ +import { it, expect, describe, afterEach } from 'vitest' + +import { getLineHeight } from '../src/helpers/dom.ts' + +function createContainer(className: string): HTMLDivElement { + const container = document.createElement('div') + + container.className = className + document.body.append(container) + + return container +} + +describe('getLineHeight', () => { + afterEach(() => { + document.body.replaceChildren() + + for (const style of document.head.querySelectorAll('style[data-test]')) { + style.remove() + } + }) + + it('ignores the container vertical padding when measuring line height', () => { + const style = document.createElement('style') + + style.dataset.test = 'line-height' + style.textContent = [ + '.lh-base li, .lh-padded li { margin: 0; padding: 0; list-style: none; font-size: 16px; line-height: 16px }', + '.lh-padded { padding-top: 25px; padding-bottom: 25px }', + ].join('\n') + document.head.append(style) + + const base = createContainer('lh-base') + const padded = createContainer('lh-padded') + + // The measured line height must not change just because the container + // has vertical padding. Before the fix, offsetHeight / 2 counted that + // padding, inflating the value and leaving a gap for upward menus. + expect(getLineHeight(padded)).toBe(getLineHeight(base)) + }) +})