Skip to content

test: add onEndTag behavior tests for HTMLRewriter - #43241

Closed
robobun wants to merge 2 commits into
mainfrom
robobun/5a52b2db/htmlrewriter-onendtag-behavior-tests
Closed

robobun wants to merge 2 commits into
mainfrom
robobun/5a52b2db/htmlrewriter-onendtag-behavior-tests

Conversation

@robobun

@robobun robobun commented Sep 18, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Fix

  • Add the three behavior tests from htmlrewriter: release end-tag handler resources after each rewrite #31583 to test/js/workerd/html-rewriter.test.js, at the same place in the file. The commit credits the author.
  • Add two tests after review: the html option with markup as content, and an element closed by the end tag of its parent. The void element test also asserts the error class and name.
  • No source file changes. The tests pin existing behavior: they pass on a release build of main (08a2340) and on bun 1.3.14.
  • Verified: bun bd test test/js/workerd/html-rewriter.test.js (191 pass, 0 fail).

Background

  • element.onEndTag(fn) runs fn when an end tag closes that element. fn receives an EndTag with before(), after(), remove() and name.
  • lol-html is the streaming HTML rewriter behind HTMLRewriter. If the input ends before an element is closed, the handler does not run. If the end tag of a parent closes the element, the handler runs with that end tag.
  • A void element (<br>, <img>) cannot have an end tag. onEndTag() throws an Error named HTMLRewriterError for it (src/runtime/api/html_rewriter.rs:3034).
Notes

Coverage of onEndTag on main before this PR:

  • endTag.before(): only inside an async handler ("async onEndTag handler").
  • endTag.after(): only with html: false, inside the "GC pressure mid-rewrite" test.
  • endTag.remove(): none.
  • An element that is never closed: none.
  • An element closed by the end tag of its parent: none on main. HTMLRewriter: make element.onEndTag() on nested elements linear #42234 (open) covers it for nested elements.
  • A void element: none.

Changes to the tests from #31583:

  • "callback fires only for elements whose end tag appears" is now "callback does not fire when the input ends before the element is closed". The body is unchanged. The old name was not exact: for <section><div>b</section> the callback of the div fires with the section end tag.
  • "throws on void elements" asserts instanceof Error and name === "HTMLRewriterError" in addition to the message.

Observed output for the html option, the same on main and on 1.3.14:

call output for <div>a</div>z
end.before("<b>Y</b>", { html: true }) <div>a<b>Y</b></div>z
end.before("<b>Y</b>") or { html: false } <div>a&lt;b&gt;Y&lt;/b&gt;</div>z
end.after("<b>Y</b>", { html: true }) <div>a</div><b>Y</b>z
end.after("<b>Y</b>") or { html: false } <div>a</div>&lt;b&gt;Y&lt;/b&gt;z

The leak test from #31583 is not part of this PR. test/js/workerd/html-rewriter-leak.test.ts already has "onEndTag callbacks are released after the rewrite" from #33048, which counts protected callbacks exactly and also runs on debug builds.


no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/workerd/html-rewriter.test.js

Add the three onEndTag behavior tests from #31583 to
test/js/workerd/html-rewriter.test.js. They cover endTag.before(),
endTag.after() and endTag.remove(), an element whose end tag never
appears, and the "No end tag." error for a void element.

The tests pin behavior that main already has. They need no source change.

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
@robobun

robobun commented Sep 18, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: the tests are pushed, with the review suggestions applied in 911fad5. CI runs now.

How to verify: run bun bd test test/js/workerd/html-rewriter.test.js -t "onEndTag:". The five new tests pass on main (08a2340) with no source change, so this PR has no fail-before state. Three of them come from #31583, which is closed because #33048 already fixed its leak.

@coderabbitai

coderabbitai Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Essentials

Run ID: f4a6bf93-f9be-43f8-8b2e-16aeb769224d

📥 Commits

Reviewing files that changed from the base of the PR and between 08a2340 and 9e0876b.

📒 Files selected for processing (1)
  • test/js/workerd/html-rewriter.test.js

Included review availability: Your plan provides up to 10 included reviews per hour; 2 remain after this review.


Walkthrough

The test suite adds coverage for onEndTag mutations, callback behavior for unclosed elements, and registration errors on void elements.

Changes

HTML rewriter end-tag behavior

Layer / File(s) Summary
End-tag callback coverage
test/js/workerd/html-rewriter.test.js
Tests cover inserting content before or after an end tag, removing the end tag, ignoring callbacks for unclosed elements, and throwing "No end tag." for void elements.

Suggested reviewers: jarred-sumner

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 9e087

The test-only change covers the intended HTML rewriter behaviors with observable assertions. No actionable merge risk remains.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely identifies the added HTMLRewriter onEndTag behavior tests. It matches the main change.
Description check ✅ Passed The description explains the problem, test changes, expected behavior, scope, and verification results. It does not use the template headings exactly, but it provides the required information and is c…

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread test/js/workerd/html-rewriter.test.js Resolved
Comment thread test/js/workerd/html-rewriter.test.js Resolved
Comment thread test/js/workerd/html-rewriter.test.js Resolved
- Add a test for the html option of endTag.before() and endTag.after().
  It uses markup as content, so escaped and raw output differ.
- Add a test for an element that the end tag of its parent closes. The
  callback fires with that end tag.
- Assert the class and the name of the "No end tag." error, not only
  its message.
- Rename the unclosed element test. A callback can fire without the
  element's own end tag, so the old name was not exact.
@robobun

robobun commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator Author

Review follow-up: I checked the three optional suggestions against main and 1.3.14, and all three hold. 911fad5 applies them.

  • The html option: a new test inserts markup through endTag.before() and endTag.after() with both states of the option, so escaped and raw output differ.
  • The void element error: the test now asserts the Error class and the HTMLRewriterError name with the message.
  • An element closed by the end tag of its parent: a new test pins that its callback fires with that end tag. The EOF test has a more exact name.

The threads have the details and are resolved.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM — the second commit addresses each of the inline comments from the earlier review, and the new tests pin behavior the runtime already implements.

What was reviewed:

  • The { html } option matrix now uses markup-bearing content (<b>Y</b>) for both before and after, so the escaped vs. unescaped outputs are distinguishable; the expected strings match content_type() in src/runtime/api/html_rewriter.rs (Html iff html is truthy, Text otherwise).
  • The implicit-close case (<section><div>b</section>) is now covered alongside the EOF-truncation case, asserting both output and the end tag name passed to the handler.
  • The void-element test asserts toBeInstanceOf(Error) plus exact name/message, which match create_lolhtml_error (HTMLRewriterError) and the "No end tag." throw at the end_tag_handlers() None branch.
Extended reasoning...

Overview

The PR adds five it(...) cases to the existing describe("HTMLRewriter") block in test/js/workerd/html-rewriter.test.js and touches no runtime code. The tests cover Element.onEndTag: before/after/remove on the end tag (including an unclosed trailing element), the { html } content-type option matrix for before and after, the negative contract that the callback does not fire when input ends before the element closes, the implicit-close case where a parent's </section> closes an open <div>, and the HTMLRewriterError / "No end tag." throw for a void element.

Security risks

None. The change is confined to a test file, uses in-process new Response(...) input, and makes no network or filesystem access.

Level of scrutiny

Low-to-moderate. Because no source changes ship, the relevant question is whether the tests are non-vacuous and pin the correct existing behavior. I checked the asserted values against the runtime: content_type() in src/runtime/api/html_rewriter.rs returns Html only when html is truthy, matching the six-entry matrix; create_lolhtml_error sets name to HTMLRewriterError on a plain Error, and on_end_tag_ throws "No end tag." when end_tag_handlers() is None (the void-element case). The void-element test captures the error inside the handler and asserts it after transform completes, so it fails if the handler does not run or does not throw. The implicit-close test asserts end.name is "section", consistent with lol-html invoking closed elements' end-tag handlers with the closing tag.

Other factors

All three inline comments from the prior review are addressed by commit 911fad5: markup-bearing content with both html states, the implicitly-closed variant, and class/name assertions on the error. The file's local conventions (it, module-scope imports, toEqual over a single object for the matrix) are followed. The changed path is not covered by .github/CODEOWNERS. I was unable to execute the test file in this environment, so the pass claim rests on the PR's stated bun bd test run plus CI; the static cross-check against the runtime source gives me confidence the expected values are correct.

@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Folded into #49196, which combines the open test-only PRs into one. The commit 675fbc11d in that PR is this change, unchanged.

Closing this one in favor of #49196.

@robobun robobun closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant