frontend: let a figure be sized by its height as well as its width
A writer with a tall panel does not want to shrink it from the left edge: what
they know is "this one is too tall". The format had a width and nothing else, so
the only way to make a figure shorter was to guess a percentage and check the
page — and the previous commit's answer, "type 638px", asked them to do the
arithmetic themselves.
So `{height=300px}` joins `{width=…}`, in the same attribute block, and the
editor grows a second box beside the first. Both are typeable as well as
pre-set, both write through the reader's own parse function, and the hint beside
them says what the picture will measure on the page.
Three rules, and the point of all three is that a figure cannot be stretched:
* **a width is exact, a height is a cap.** A width has a reference — the column,
or a length the writer chose — while nothing measures a height against
anything, and enlarging a picture past its own pixels only makes it soft. In
the flow of a sentence a height is always a downscale, which comes to the same
thing as exact.
* **given one, the other follows from the picture's own shape** — `max-height`
for a height, `width` for a width — so the height is never written by hand.
* **given both, they are a box to fit inside**, not a box to fill. Two numbers
that need not have the picture's proportions can only mean "no bigger than",
and the check asserts the drawn rectangle still matches the file's ratio.
Whichever box is left empty keeps the other's setting, so a size can be set in
two steps, and clearing either goes back to the natural size.
`check-image.mjs`: 26 checks to 36. Two of the new ones found real bugs while
being written. The first: with both a width and a height in one `{…}` block,
neither side parsed — the patterns were anchored to the whole block, so a figure
with two numbers silently printed at its natural size. The attribute block is now
split into declarations first. The second was mine: returning "contain" for a
height-only figure left a 1276×2000 box around a small picture, which the caption
then sat under. A height is a cap, and a cap needs no contain.
The MCP tool takes the same two arguments, so a model can write either number,
and a percentage height is refused with the reason it cannot work: a sentence is
flowed text, so there is nothing for it to be a percentage of and the browser
ignores it. v8 is deployed and DSH has verified both numbers round-tripping.
This commit is contained in:
@@ -1045,6 +1045,39 @@ async def _run(url: str | None = None, token: str | None = None) -> int:
|
||||
bare["markdown"] == f"{{width=60%}}",
|
||||
bare["markdown"],
|
||||
)
|
||||
|
||||
# A height, which is how a tall panel is made short: the width follows
|
||||
# the picture's own shape rather than being stretched to fit.
|
||||
sized = await call(
|
||||
session,
|
||||
"image_upload",
|
||||
data_base64=GIF_1PX_BASE64,
|
||||
caption="宽高都写",
|
||||
width="70%",
|
||||
height="300",
|
||||
)
|
||||
check(
|
||||
"a height is written beside the width, in pixels",
|
||||
sized["markdown"]
|
||||
== f"{{width=70% height=300px}}",
|
||||
sized["markdown"],
|
||||
)
|
||||
tall = await call(
|
||||
session, "image_upload", data_base64=GIF_1PX_BASE64, caption="只限高", height="240px"
|
||||
)
|
||||
check(
|
||||
"a height on its own is enough",
|
||||
tall["markdown"] == f"{{height=240px}}",
|
||||
tall["markdown"],
|
||||
)
|
||||
message = await expect_error(
|
||||
session, "image_upload", data_base64=GIF_1PX_BASE64, height="50%"
|
||||
)
|
||||
check(
|
||||
"a percentage height is refused, with the reason it cannot work",
|
||||
"没有参照物" in message,
|
||||
message,
|
||||
)
|
||||
message = await expect_error(
|
||||
session, "image_upload", data_base64=GIF_1PX_BASE64, width="70em"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user