Skip to content

fix(html): read raw text, link tags and the base from one html5ever pass - #501

Merged
tobocop2 merged 7 commits into
integration/0927from
fix/link-extraction-raw-text
Sep 29, 2026
Merged

tobocop2 merged 7 commits into
integration/0927from
fix/link-extraction-raw-text

Conversation

@tobocop2

@tobocop2 tobocop2 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Problem

Link extraction knew only four raw-text elements, so links and a <base> inside xmp, iframe, noembed, noframes, plaintext or after <script/> counted as real. The base came from the first <base href> in source order, not the one a browser uses. A tag with many thousands of attributes made extraction quadratic.

One read of the page

html5ever reads the page once, with scripting off. The exceptions are a page decoded again from a declared non-UTF-8 charset and a body cut to max_body_size. That read decides the raw text the HTML parser must not parse, the real link tags and where each comment ends.

The base a browser uses

The base is the first <base href> in the finished document: a <base> in a table moves in front of it, and a <frameset> drops the body with its <base>. Links, images, assets and meta refresh all use it.

A bound on attributes

Attributes past the 1,024th of one tag become spaces before the parser reads the page, and the HTML parser reads the same text, so the cost grows linearly.

Replaces the link-extraction halves of #232, #313 and #325.

Closes #201
Closes #269
Closes #287

Link extraction decided raw text with a hand-written scanner that knew
script, style, textarea and title, then ran html5ever over the page a
second time to find the real link tags. Links and a <base href> inside
xmp, iframe, noembed, noframes and plaintext, after <script/>, and in a
script inside SVG foreignObject were read as real, and a <!-- inside
such text hid every link after it.

The page is now read once by html5ever's tokenizer, steered by its tree
builder with scripting off. That read reports the raw-text ranges (from
the builder's RawData and Plaintext answers), the <a> start tags, the
span of each comment, and the first <base href> in tree order. The mask
overwrites each < of raw text with a space and places the existing
abrupt-comment patches for tl from html5ever's comment spans, so the
masked text keeps the source's byte length. Link extraction reuses the
tags from that read instead of scanning again.

The base comes from a minimal element tree, walked once the parse ends:
a <base> in a table is placed in front of it, a frameset removes the
body with its <base>, and template contents are not in the document.
Links, images, feeds, assets, the sitemap fallback and the meta refresh
target all resolve against it. The markdown pre-pass keeps its own
source-order lookup.

html5ever compares each new attribute name of a tag with every earlier
one, so one tag with n distinct names costs n^2/2 comparisons. Before
each piece is fed, the attributes past the 1,024th of one tag are
overwritten with spaces, following the tag through html5ever's own
attribute states so a > in a quoted value does not end it, and tl
parses the same bounded text. Measured on the box (debug build), a tag
with 16k/32k/64k distinct names took 1.7/7.2/24.1 s at the base and
35/65/133 ms here.

Closes #201
Closes #269
Closes #287
…und uncovered

Four production call sites had no test that would fail if they broke: the
attribute bound surviving alongside raw-text masking in the same document, a
crawl's own page extraction masking raw text the way a scrape already does,
a downloaded asset resolving against the page's base href, and the metadata
raw-body fallback reading the page's own text through extract_page_data.
The meta refresh check read every final HTML response with html5ever, and
extraction then read the same page again; a scrape read it a third time for
the render hint. The check now keeps its read of the page, the redirect
outcome carries it, and crawl and scrape extraction reuse it. It is dropped
when the charset re-decode replaces the body, and a body cut to
max_body_size is read again because its length differs. The render hint
reads the document extraction already parsed. Tag attributes are stored as
owned strings so the read can cross to the blocking pool.

A bogus comment (`<?`, `<!x`, `</3`, `<![CDATA[` in HTML) runs to its first
`>`, but tl read a tag at any `<` inside it; every `<` after the opener is
now masked. CDATA sections in SVG and MathML are recorded by the scan and
masked like raw text. A comment right after an empty end tag `</>` is
patched at its own opener.

A test now covers the parse-error return inside script text
(`<script>a\0<a href="/x">`), and the changelog says that an `href` past the
1,024th attribute of an `<a>` or `<base>` is not read.
…count

Two hunks of the one-read change had no test that fails when they break.
A page with both a Refresh header and a meta refresh now has a test that
the header wins and the body is not read for its meta refresh. A scraped
page of 60 words with an empty SPA mount now has a test that it gets no
render hint, so a word count that does not reach the hint turns it red.

The read is only detached by the redirect check, which wasm does not
compile, so the detach step is now compiled out on wasm instead of
allowed as dead code there.

The changelog names the two pages that are still read twice: a page
decoded again from a declared non-UTF-8 charset, and a body cut to
max_body_size.
…text

The map fallback for an HTML page resolves its links against the URL
that served the page (#391) and keeps the page's own base from the one
read of the page. The redirect follower's per-page user agent (#499) is
passed through on the frontier fetch and the wasm scrape. Both changelog
entries are kept.
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.

1 participant