chrome.js: fit the title without counting its end padding twice
Fable's P3.4 on estilo #34. The fit was right only because two errors cancelled. At zero width a field's scroll width already holds both its paddings (measured in Chromium and WebKit: text + 12.8px), and the script added the end padding again; from 800px, max-inline-size: 100% then trimmed 7.4px back off, because the field's pulled-back start margin makes its form that much narrower than the field. Below 800px, where there is no such margin, nothing trimmed it, and a short title's field came out 6.4px wider than it needed. The fit is now the zero-width scroll width plus the borders and 2px for the caret, and from 800px the cap is calc(100% + 0.4rem + 1px), giving the margin back. The comment says what the measure holds. The Node model of the field now holds both paddings, as the engines do; the old script fails it (162px for 156px). TestTheWideTitleCapAllowsForItsPulledBackMargin holds the cap. Checked in Chromium and WebKit at 1280, 1000, 390 and 320px with Latin, kanji, 20 Ws, 20 is, Arabic (RTL), mixed CJK, emoji and a long title: every fitted field is 2.5px wider than the narrowest width at which it does not scroll, and with the caret at the end the text does not move. The long title is capped where it was and scrolls.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+31
−14
chrome/chrome.css+1 −0chrome/chrome.js+9 −8chrome/chrome.test.mjs+7 −6chrome/stack_test.go+14 −0
diff --git a/chrome/chrome.css b/chrome/chrome.css| index 55ac7f2..557e881 100644 |
| --- a/chrome/chrome.css |
| +++ b/chrome/chrome.css |
| @@ -410,6 +410,7 @@ |
| block-size: 24px; |
| padding-block: 0; |
| margin-inline-start: calc(-0.4rem - 1px); /* the field's padding and border pulled back: the text sits on the edge */ |
| + max-inline-size: calc(100% + 0.4rem + 1px); /* the form is that much narrower than the field: the cap gives it back */ |
| font-size: 16px; |
| line-height: 24px; |
| font-weight: 600; |
diff --git a/chrome/chrome.js b/chrome/chrome.js| index b86c1ed..d820315 100644 |
| --- a/chrome/chrome.js |
| +++ b/chrome/chrome.js |
| @@ -91,20 +91,21 @@ |
| // The stacked bar's title field is as wide as its title (Paul: "It |
| // should grow according to the title, not start out full-width"). The |
| // field measures itself: at zero width its scroll width is its own text |
| - // as it draws it, start padding included; an overflowing field leaves |
| - // its end padding out of that, so that and the borders go on top, with |
| - // 2px for the caret at the end of the text. field-sizing: content, a |
| - // canvas and a mirror span in the same font all came out short in |
| - // WebKit; this did not. The width is capped by CSS (max-inline-size), so |
| - // a longer title still scrolls inside the field. Nothing after the |
| - // title on its row moves when it changes: the stack takes the room. |
| + // as it draws it with both its paddings (Chromium and WebKit alike), so |
| + // only the borders go on top, and 2px for the caret at the end of the |
| + // text. field-sizing: content, a canvas and a mirror span in the same |
| + // font all came out short in WebKit; this did not. CSS caps the width |
| + // at the room the field has (max-inline-size, which from 800px allows |
| + // for the field's pulled-back start margin), so a longer title still |
| + // scrolls inside the field. Nothing after the title on its row moves |
| + // when it changes: the stack takes the room. |
| const FIT = '[estilo-bar-title-input][size]' |
| const CARET = 2 |
| const fit = (input) => { |
| const cs = getComputedStyle(input) |
| const px = (v) => parseFloat(v) || 0 |
| input.style.inlineSize = '0px' |
| - const w = input.scrollWidth + px(cs.paddingInlineEnd) + px(cs.borderInlineStartWidth) + px(cs.borderInlineEndWidth) |
| + const w = input.scrollWidth + px(cs.borderInlineStartWidth) + px(cs.borderInlineEndWidth) |
| input.style.inlineSize = Math.ceil(w + CARET) + 'px' |
| } |
| const fitAll = () => { Array.prototype.forEach.call(document.querySelectorAll(FIT), fit) } |
diff --git a/chrome/chrome.test.mjs b/chrome/chrome.test.mjs| index b43214c..07c5186 100644 |
| --- a/chrome/chrome.test.mjs |
| +++ b/chrome/chrome.test.mjs |
| @@ -144,10 +144,10 @@ test("Escape is heard before a menu that stops propagation", () => { |
| }); |
| // The stacked bar's title field. Its text draws 140px wide with 6px of |
| -// padding each side and 1px borders. As a field does, its scroll width |
| -// at zero width is the text plus its start padding (146), and at any |
| -// other width at least that width, so a measure that skips the zeroing |
| -// reads the old width back instead. |
| +// padding each side and 1px borders. As a field does in Chromium and |
| +// WebKit (measured), its scroll width at zero width is the text and both |
| +// its paddings (152), and at any other width at least that width, so a |
| +// measure that skips the zeroing reads the old width back instead. |
| function titleField(attrs = { size: "17" }) { |
| const input = new El("input", { "estilo-bar-title-input": "", value: "Untitled document", ...attrs }); |
| input.text = 140; |
| @@ -155,7 +155,7 @@ function titleField(attrs = { size: "17" }) { |
| Object.defineProperty(input, "scrollWidth", { |
| get() { |
| const w = parseFloat(this.style.inlineSize); |
| - return Number.isNaN(w) || w > 0 ? Math.max(400, this.text + 6) : this.text + 6; |
| + return Number.isNaN(w) || w > 0 ? Math.max(400, this.text + 12) : this.text + 12; |
| }, |
| }); |
| return input; |
| @@ -166,7 +166,8 @@ test("a stacked title field is fitted to its text when the script runs", () => { |
| const fitted = titleField(); |
| const plain = titleField({}); |
| load(new El("", {}, [fitted, plain]), { getComputedStyle: fieldStyle }); |
| - // 146 at zero width, + 6 end padding + 2 border + 2 for the caret. |
| + // 152 at zero width (the end padding already in it), + 2 border + 2 |
| + // for the caret. |
| assert.equal(fitted.style.inlineSize, "156px"); |
| assert.equal(plain.style.inlineSize, "", "a one-row bar's title (no size) is left alone"); |
| }); |
diff --git a/chrome/stack_test.go b/chrome/stack_test.go| index c15db3b..0325533 100644 |
| --- a/chrome/stack_test.go |
| +++ b/chrome/stack_test.go |
| @@ -344,3 +344,17 @@ func TestTheControlsKeepTheirBoundaryInForcedColours(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// From 800px the title field is pulled back by its padding and border |
| +// (a negative start margin), so its form is that much narrower than the |
| +// field, and a cap of 100% of the form trimmed chrome.js's fitted width |
| +// by 7.4px. The cap allows for the margin, so the fit stands as measured. |
| +func TestTheWideTitleCapAllowsForItsPulledBackMargin(t *testing.T) { |
| + src := string(CSS()) |
| + src = src[strings.Index(src, barStackMarker):] |
| + wide := src[strings.Index(src, "@media (min-width: 800px) {"):] |
| + rule := regexp.MustCompile(`\[estilo-bar~="stacked"\] \[estilo-bar-title-input\] \{[^}]*\}`).FindString(wide) |
| + if !strings.Contains(rule, "margin-inline-start: calc(-0.4rem - 1px);") || !strings.Contains(rule, "max-inline-size: calc(100% + 0.4rem + 1px);") { |
| + t.Errorf("the wide title field's cap does not give back its pulled-back margin:\n%s", rule) |
| + } |
| +} |