Skip to content

Add a document-end rewrite handler #89

Description

@dv00d00

Context

PR #85 completed element rewriting but intentionally left document-level finalization out of scope. A successful EOF hook covers a common streaming proxy need: append a final script, marker, or trailer even when the source has no explicit html/body closing tag.

Scope

Add one document-end callback invoked exactly once after successful input completion. It may append trusted HTML or escaped text. Matching html or body is not equivalent because malformed and fragment-like inputs may contain neither.

Doctype mutation is explicitly not planned in this issue. There is no demonstrated consumer that justifies source-range mutation semantics for malformed and duplicate doctypes; open a separate issue if that need appears.

Constraints

  • Emit only after pending rewrite events are resolved.
  • Participate in output backpressure.
  • Define ordering after element After insertions and implicit frame closure.
  • Do not run on cancellation, parser/resource-limit failure, callback failure, or output failure.
  • Plans without the hook gain no per-token overhead.

Acceptance

  • Explicit and implicit html/body closure, fragments, and empty input.
  • Whole-buffer and streaming APIs produce identical output across every chunk split.
  • Ordering relative to element mutations is pinned.
  • Cancellation, resource-limit, and output-failure finalization tests.
  • Focused disabled-handler and active EOF-insertion allocation/throughput measurement.

Narrowed from the original combined doctype/document-end proposal during 2026-08-20 triage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions