Skip to content

feat(vue): accept HTML from templates with foreign spans as static blocks - #803

Merged
JuroOravec merged 2 commits into
mainfrom
feat/vue-static-foreign-html
Nov 19, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
feat/vue-static-foreign-html

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

(Opus 5.5)

An interactive component can now hold HTML from a template with foreign spans (tags another engine renders, such as Django's {% %} tags). This works on the serialize path and in an Events Render. Before, such HTML raised unsupported typed render part: str or prepared Vue rendering does not yet support foreign template spans. This is change C3 of the citry-django redesign (citry-django/docs/vue-redesign.md). It builds on #800 and #801.

<c-modal title="Help">{% translate "Close" %}</c-modal>

Plan

Prior art

  • component_render.py _get_compiled_template (origin/main 3053-3063) and _compile_nested_template (3290-3297). A template with foreign spans compiles for static output, so its parts are plain str. Under vue_render_active() (Events) these functions raised. _render_body refused raw strings through _BodyFrame.direct_prepared (3417, 3456, 3517), and so did _vue/capture.py::_validate_prepared_render (1503).
  • _vue/direct_capture.py part loop. A Markup value becomes one opaque HTML record (3081-3120). The record is checked by reject_cross_boundary_html and reject_noscript_in_opaque_html (opaque_html.py:36-118), with refusals under svg/math and text-only elements. The browser inserts it with createStaticVNode (client.js <citry-opaque-html>), and the template only holds <citry-opaque-html :record="$citryPrepared.opaqueHtml.key">. A plain str fell through to unsupported typed render part (3590).
  • _ignored_contents_html (#c-ignore/v-pre contents) writes typed parts as static HTML. This change reuses its rules for which bindings need Vue.
  • _vue/events.py (857-1116). Stylesheet owners come from render_to_occurrence. An unknown render id falls back to the root occurrence. Opaque records already get the settled security_javascript/security_csp scans (965-988).
  • CitryRender.needs_browser_runtime() (C2, feat(serialize): return asset tags apart from the HTML with deps_position="separate" #801) answers "does this subtree need Vue".

Chosen design

  • Serialize path. When the part loop meets a non-empty plain str, write_static_html_run builds a run. It joins the string with every following part that _static_part_html can write as static HTML: more strings, Markup, static typed parts, and static component renders. Static components keep their root markers (css_data() markers included). A part that needs Vue raises _NeedsVue, which ends the run, and that part is walked as usual.
  • What a run becomes.
    • HTML whitespace alone becomes template text. Vue condenses it like authored whitespace, so the page still hydrates when line breaks sit at the app root.
    • Text without tags becomes a text binding.
    • Anything else becomes one opaque record with origin markup, so the browser checks it for <noscript> again.
  • Merged dependencies. The render ids of static components inside a run map to the occurrence that holds the block, so their stylesheets load with it. A <c-mark> ends the run, because a Render finds a mark by its occurrence.
  • Events path. Templates with foreign spans compile for static output under Vue too, and plain strings are kept. The same writer handles them.
  • Named refusals:
    • a run that is not a complete fragment, read as the browser's parser reads it (reject_cross_boundary_html);
    • a component that needs Vue inside an element of the HTML: the run stops at that component and is incomplete, and the message names the component;
    • <script> (new reject_script_in_opaque_html, which also catches uppercase and svg/template spellings) and <noscript>;
    • svg/math and text-only parents;
    • Vue attributes that the author wrote in a template with foreign spans, found by reading the source with the spans blanked.

Alternatives rejected

  • Each maximal run of plain strings as its own block, with static components kept as occurrences. <li>{% trans %}<c-icon/></li> would split across blocks and be refused.
  • The whole part list as one block. Templates with foreign spans have line breaks between tags. Any region holding an interactive component would then hold it in the same list as the HTML and be refused.
  • Parse the strings into Vue element parts (the design's rejected "wrap the Django elements"). Django attribute values would become part of Vue definitions.

What would show the design is wrong

Common Django templates that put an interactive component inside a Django-written element, inside an interactive component. Each one raises the refusal. Counting them while migrating the citry-django test project measures this.

Deviations from the design

  • The design asked for an error whenever an interactive component sits inside a Django block in a fill. Here, a component that sits between balanced pieces of HTML stays an ordinary Vue component. It hydrates with zero mismatches, and the reviewer verified this in Chromium. The error applies only when the component sits inside an element of the HTML. citry-django's out-of-scope table should say so.
  • <noscript> and Vue attributes in the author's own template with foreign spans are refused too. The second keeps the design's rule that an interactive component's own template with Django tags raises: it raised before, and without this check it would now fail silently.

Security (Mechanism 7)

Places touched:

  • text and markup;
  • parser rules browsers differ on;
  • markers for Citry's browser code;
  • JavaScript and JSON;
  • the CSP nonce and the opt-in output checks.

Who writes the input: the template author and app code. The other engine escapes the request values it writes. The HTML reaches the browser only as data, in the opaque record, never as Vue template source. Tests use mustache, v-html, @click, :title and #default strings, and the browser test checks that none of them runs or renders. security_csp="strict" refuses an inline handler in the block, and security_javascript="warn" reports it.

The independent reviewer probed these routes in Chromium:

  • hydrated and mounted serialize;
  • an Events Render;
  • forged <!--[-->, data-cid-*, citry-opaque-html, <template c-render-id>, citry-vue-* ids, and a config <script>;
  • parser differences (<select>, <tbody> text, <p><div>, <plaintext>, <xmp>, CDATA, svg/math);
  • JSON escaping;
  • __proto__ keys.

It found no hole. Pages it cannot adopt fall back to data-allow-mismatch rebuilds. Firefox and WebKit were not tested. The Mechanism 8 survey kinds checked: template-inj, trusted-shape, sanitizer-bypass, xss-rawtext, hydration, ssr-serialize, mxss, dom-clobber, proto. None was found. docs/design/trust_boundaries.md has a new row. The docs/security/reviews/ record follows after merge.

Fixed during the review:

  • Vue attributes in a foreign template were silently dropped (now refused).
  • DirectSlotRender was missing from the flattener, so a static component with a fill inside a Django element was refused.
  • The "component inside an element" message showed for HTML that was simply unbalanced.
  • The script message's claim that the browser never runs the script was wrong on a first hydrating load.
  • Prose fixes.

Versioning

Input that raised now works, so this is a minor release. No new public names: reject_script_in_opaque_html is private and the foreign-span hooks are already Experimental. Input that already worked behaves the same. Raw strings that were refused before, for example from extension hooks, are now accepted as trusted HTML. This matches what the static serializer already does with a raw str. The changelog line is under Unreleased and says it prepares the citry-django integration.

Benchmarks

The question: does the path for templates without foreign spans slow down by more than 3%? Budget: about 10 minutes. Base is origin/main (git archive) and head is this branch. The runs alternate in fresh processes, with the PyPI release build of citry-core 2.1.2. The client scenario runner (benchmarks/client_scenario.py) is stale, because it uses the removed c-$c-props. So the Vue serialize rows use the large and small benchmark fixtures (str(ProjectPage), a hydrating Vue page).

Benchmark Base Head Change
Large Vue page (test_benchmark_citry), median of 11 × min of 15 68.02 ms 68.71 ms +1.0%
Small page (test_benchmark_citry_small), median of 11 × min of 40 0.14 ms 0.14 ms -0.7%
slot_renders.py --scenario filled, min / median 21.40 / 21.94 ms 21.46 / 21.70 ms +0.3% / -1.1%
slot_renders.py --scenario fallback, min / median 21.29 / 26.58 ms 20.66 / 26.55 ms -3.0% / -0.1%

All are within noise and under the 3% threshold.

Checks

  • ruff check, ruff format --check, and mypy on packages/py/citry/citry, also with --platform linux: clean.
  • tests/test_vue_static_foreign_html.py: 26 passed. These cover the fill cases, the static component with a fill or <c-provide>, marks, the Events Render, each refusal, Vue syntax injection, and the CSP and JavaScript checks.
  • tests/e2e/test_vue_static_foreign_html_e2e.py in Chromium: 2 passed (hydrated and mounted). Each run hydrates or mounts the page, checks that injected Vue syntax stays inert, and runs two Events renders.
  • tests/test_vue_migration_coverage.py: one test updated to the new contract (raw strings are kept).
  • The rest of the non-e2e suite: 14985 passed. Two failures are environment-only (a newer tzdata and a newer pydantic in the venv) and fail on origin/main too.
  • Related Vue e2e files in Chromium (opaque HTML, fill replacement, hydration regions, ignore, separate assets, targeting, benchmark page): 53 passed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom

JuroOravec and others added 2 commits November 19, 2026 17:14
…ocks

An interactive component can now hold HTML that Citry did not compile,
such as a fill written in a template whose {% %} tags Django renders. The
Vue writer joins each plain string part with the following parts that
need no Vue into one opaque HTML record, which the browser inserts as a
static node and never compiles. Static components inside keep their root
markers, and their stylesheets load with the component that holds the
block. An Events Render compiles such templates for static output too.

A block that is not a complete fragment, that has an interactive
component inside one of its elements, or that holds <script> or
<noscript> raises an error that names the case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
Refuse Vue attributes that the author wrote in a template with foreign
spans when its output reaches a Vue app, since the static block would drop
them silently. A static component that takes a fill now joins the block,
a <c-mark> keeps its Vue occurrence, and the "component inside an element"
message is used only when a part that needs Vue ends the block. The script
refusal and the docs now say the script would run only on a first
hydrating load. New tests cover these cases, each spelling of <script>,
line breaks at the app root, and a mounted (not hydrated) page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
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