Skip to content

fix: scope parser, render and options to each process() call - #421

Open
voischev wants to merge 3 commits into
masterfrom
fix/parser-render-leak
Open

voischev wants to merge 3 commits into
masterfrom
fix/parser-render-leak

Conversation

@voischev

@voischev voischev commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Problem 1: custom parser/render leaked globally

process() assigned options.parser / options.render to the module-level defaults, so a single call changed every later call in the same Node.js process, including brand-new instances:

posthtml().process('x', { sync: true, render: () => 'CUSTOM RENDER' })
posthtml().process('<b>x</b>', { sync: true }).html // => 'CUSTOM RENDER'

Problem 2: per-call state on a shared instance

process() stored per-call state on this (options, source, and with a naive fix also parser/render), and plugins received it via Object.assign(tree, this). With two concurrent async calls on one instance, plugins of the first call saw the second call's tree.options and tree.parser.

Fix

  • Module defaults are const.
  • process() builds a per-call context from an explicit list of tree API fields (version, name, plugins, messages, walk, match from the instance; options, source, parser, render for the call) and passes it to plugins; the result renders with that call's renderer.
  • Other properties set on the processor instance are no longer copied onto the tree (before, Object.assign(tree, this) copied everything).
  • Performance: same as master (≈5.3 µs per process() of a small document with 3 plugins, 50k-call median); a { ...this } spread was 3–6% slower, so it is not used.
  • instance.options / instance.source are still set (last call) for backward compatibility. instance.parser / instance.render stay the defaults.

Tests (test/options.js)

Custom functions apply to the call and are exposed to plugins; no leak into other instances; no persistence to the next call; concurrent async calls keep their own options/parser/render; instance parser/render unchanged; instance options kept for compatibility. On master 3 of these fail (and the leak breaks unrelated tests in the same run); on the first version of this PR the concurrency test failed.

Downstream impact (important for 0.17)

htmlnano's suite only passed on master because of the leak: htmlnano.process() passes its own posthtml-parser@0.12, which then became the global default for later posthtml([htmlnano()]) calls in the test run. With this fix, 21 htmlnano tests (<plaintext>, SVG CDATA) use posthtml's own parser 0.13 / htmlparser2 12 and fail. The same output already happens on master for a fresh process (first call), so this is a real parser 0.13 behavior difference for htmlnano, not a regression of this PR. htmlnano pins posthtml@^0.16.5, so its users are unaffected until it upgrades.

Also checked with this build: gulp-posthtml (13 tests) and Eleventy 3.1.8 (1392 tests) pass. npm run check passes, coverage 100%.

🤖 Generated with Claude Code

options.parser and options.render reassigned the module-level defaults, so
one call with a custom parser or renderer changed the output of every later
process() call in the same Node.js process, including other instances.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Concurrent process() calls on one instance overwrote each other's
options, parser and render, so plugins of one call could see another
call's settings. Build a context per call for tree.options, tree.parser,
tree.render and tree.source. instance.options and instance.source are
still set for backward compatibility; instance.parser/render stay the
defaults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@voischev voischev changed the title fix: scope custom parser and render to a single process call Nov 26, 2026
Spreading the instance copied any property set on it and was a few percent
slower per call. List the tree API fields explicitly instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant