Skip to content

Draw a Markdown file's HTML as its author wrote it, with only its script taken out - #7317

Merged
max-sixty merged 3 commits into
mainfrom
readme-preview-formatting
Nov 26, 2026
Merged

max-sixty merged 3 commits into
mainfrom
readme-preview-formatting

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Draw a Markdown file's HTML as its author wrote it, with only its script taken out

A file's preview kept only the HTML GitHub's sanitizer allows, which Leaf has no reason to copy: Max asked for the file's HTML without a feature that imitates GitHub. DOMPurify now runs with its own defaults, which take out script, event handlers, javascript: links and elements HTML doesn't define, such as Leaf's widgets. ALLOW_DATA_ATTR: false stays, since Leaf's own marks are data-lf-* and a file's would speak for Leaf. GitHub's tag and attribute lists, the class filter, the table-alignment and checkbox hooks, and GitHub's user-content- prefix go: every id and <a name> in a file becomes a place of the preview, as the prefixed ones did.

What a user sees differently: a README's style, class, fields, <video>, <u>, <center> and inline <svg> now draw. Of the 200 corpus READMEs, 79 draw differently from GitHub's list, most through the styles 45 of them write, such as round contributor avatars (border-radius: 50%). None of the 200 holds a script or a handler. An rel="opener" on a link stays as written. An inline SVG whose parts point at each other by id loses those references, since its ids become places (one of the 200, mermaid's).

The browser test now checks that a file's style, class and fields draw, and that a Leaf widget, a data- attribute and every script route still go.

The rest of a file's HTML draws in the page's own tree, so a hostile file can still restyle or cover the page, point a <label for> at a page control, or take the tab elsewhere with a form, as a link can. markdownFileFragment's header says so. Forbidding those tags and attributes would stop part of it while an inline position: fixed box still covers the page; only a document of its own would stop all of it, and comments can't reach inside one. None of them appears in the 200 READMEs but one form.

…ipt taken out

A file's preview kept only the HTML GitHub's sanitizer allows, which Leaf has no reason to copy: Max asked for the file's HTML without a feature that imitates GitHub. DOMPurify now runs with its own defaults, which take out script, event handlers, `javascript:` links and elements HTML doesn't define, such as Leaf's widgets. `ALLOW_DATA_ATTR: false` stays, since Leaf's own marks are `data-lf-*` and a file's would speak for Leaf. GitHub's tag and attribute lists, the class filter, the table-alignment and checkbox hooks, and GitHub's `user-content-` prefix go: every `id` and `<a name>` in a file becomes a place of the preview, as the prefixed ones did.

What a user sees differently: a README's `style`, `class`, fields, `<video>`, `<u>`, `<center>` and inline `<svg>` now draw. Of the 200 corpus READMEs, 79 draw differently from GitHub's list, most through the styles 45 of them write, such as round contributor avatars (`border-radius: 50%`). None of the 200 holds a script or a handler. An `rel="opener"` on a link stays as written. An inline SVG whose parts point at each other by id loses those references, since its ids become places (one of the 200, mermaid's).

The browser test now checks that a file's style, class and fields draw, and that a Leaf widget, a `data-` attribute and every script route still go.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DOMPurify's defaults keep more than the file's own look. The preview is drawn into the page's light DOM, so some of what now survives reaches the whole Leaf page and not just the preview. I ran the vendored DOMPurify 3.5.0 with { ALLOW_DATA_ATTR: false } to check:

  • A <style> element survives whenever it isn't the file's very first node (DOMPurify only drops a leading one). Since the preview has no shadow root, <h1>T</h1><style>body{display:none}</style> restyles the entire page: Leaf's panel, threads and every other widget. Most READMEs open with HTML such as <p align="center">, so a <style> inside one would survive.
  • References to the page's ids. preparePreview now turns every id in the file into data-lf-place, so any for, form, popovertarget, aria-labelledby or list in the file can only resolve to an element of the page. For example, <label for="…"> clicks a page control and popovertarget opens a page popover. tabindex also survives and changes the page's tab order. The old list dropped all of these for that reason.
  • <form action method> survives with working fields, so pressing Enter in a README's field takes the Leaf tab to the form's action, whether that is outside the page or a same-origin route.

This is the PR's own reasoning for keeping data- attributes out: a file's markup shouldn't act or speak for Leaf. Applied the same way, these go too, while styles, classes, fields and media still draw as intended. The suggestion below does that. I checked that it keeps style/class attributes, inputs and buttons, and drops the <style> element together with its CSS text. Rendering the preview in a shadow root would scope styles and ids in one step, but comments would then have to anchor across the shadow boundary.

An inline style="position: fixed; inset: 0" (the case the old test pinned) and a file class that matches one of Leaf's own selectors can still cover or imitate Leaf's controls. Keeping style and class looks like the call you made in the description, so I'm only pointing it out here.

Comment thread browser/runtime/lib/markdown.js Resolved
DOMPurify's defaults stop what runs script and what speaks for Leaf. A file's HTML still draws in the page's light DOM, so a hostile file can restyle or cover the page, point a label at a page control, or navigate the tab with a form. The review proposed forbidding `<style>`, `<form>`, `for`, `form`, `popovertarget` and `tabindex`; an inline `position: fixed` box still covers the page, so the header says what remains rather than adding a partial list. None of those appears in the 200 corpus READMEs but one form.
@max-sixty
max-sixty merged commit d4beb41 into main Nov 26, 2026
8 checks passed
@max-sixty
max-sixty deleted the readme-preview-formatting branch November 26, 2026 22:23
leaf-agent added a commit that referenced this pull request Nov 27, 2026
…7336)

`test_a_double_click_on_the_rendered_copy_types_at_its_line_in_the_editor`
has failed on `main` since #7317, which landed ten minutes after #7315
added the test. #7317 switched a Markdown file's preview to DOMPurify's
defaults, which keep the file's `<style>`. The test's README holds
`<style>.Gizmo { color: red }</style>` ahead of
`<strong>Gizmo</strong>`, so `word_point` took the style's text node
first. That range draws no box, so the double-click pressed the window's
corner. It selected the page's heading ("Notes"), and the editor never
took focus.

`word_point` now skips any text that draws no box and raises if no drawn
copy of the word exists. The product's double-click mapping needs no
change: with the press on the visible "Gizmo", every word in the test
lands on its line. The test fails on `main` and passes with this change.
So do the other callers of `word_point`, in `test_reading_place.py`,
`test_draft_editing.py`, `test_render_files.py` and
`test_threads_in_panel.py`.

Refs #7334 — automated triage

Co-authored-by: leaf-agent <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants