A `{=html}` block holding `<style>` or `<script>` fails to parse on a page with code cells

Aperta
#1,368 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
65/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
backend

Direzione di ricerca

Start with round_trips_bare_html and write_rawblock in crates/pampa/src/writers/qmd.rs, then read the related tests for raw HTML round trips. Check how fenced {=html} blocks containing style and script elements are serialized and parsed when a document has a code cell. Done when both reproductions render successfully and preserve the raw HTML in the output.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

This issue was written by Claude while adding q2 support to
Great Docs. The output shown comes from running each repro.

Bug description

A ```{=html} block that holds a <style> or <script> element renders correctly on a page
without code cells. On a page with a code cell, the same block fails with a parse error in the
intermediate .jupyter.rmarkdown file, at the block's CSS or JavaScript.

Before running an engine, serialize_ast_to_qmd in
crates/quarto-core/src/stage/stages/engine_execution.rs writes the document as qmd, and the qmd
writer drops the block's fence. write_rawblock in crates/pampa/src/writers/qmd.rs writes HTML
bare when round_trips_bare_html accepts it, and that function accepts any raw-text element
(<pre>, <script>, <style>, <textarea>) on its opening tag alone. Its doc comment says the
reader leaves such an element's interior unparsed. But the interior is parsed as Markdown before
paragraph.rs lifts the paragraph to a RawBlock, so braces and indented lines are errors when
the intermediate file is read.

#674 reported this along with bare <style> and <script> elements, and was closed as not
planned: q2 will discourage bare elements because their contents can't be parsed reliably. This
issue is only about the fenced form, which is what an author is asked to write instead, and which
q2's own writer turns into the bare form. Keeping the fence on raw-text elements in
round_trips_bare_html, as #674 suggested, would fix it.

Great Docs wrote a <style> rule on its homepage this way, and pages with code cells failed to
render under q2. It now puts the rule in its stylesheet instead, so this no longer blocks Great
Docs.

Steps to reproduce

With Python and ipykernel available:

mkdir raw-style && cd raw-style
printf -- '---\ntitle: Raw style\n---\n\n```{=html}\n<style>\nh1 { color: red; }\n</style>\n```\n' > plain.qmd
printf -- '---\ntitle: Raw style\n---\n\n```{=html}\n<style>\nh1 { color: red; }\n</style>\n```\n\n```{python}\n1 + 1\n```\n' > code.qmd
q2 render plain.qmd
q2 render code.qmd
Actual behavior

plain.qmd renders, and plain.html has the <style> element. code.qmd fails:

error: while rendering /private/tmp/raw-style/code.qmd
Error: [Q-2-41] Curly braces are reserved for attribute syntax
     ╭─[ /private/tmp/raw-style/code.jupyter.rmarkdown:136:5 ]
     │
 136 │ h1 { color: red; }
     │     ───┬───
     │        ╰───── Curly braces are reserved for attribute syntax in Quarto markdown. To write literal braces, escape them as `\{...\}`. If you meant to attach an attribute, use `.class` / `#id` / `key="value"` syntax, e.g. `[text]{.class}`.
─────╯

error: while rendering /private/tmp/raw-style/code.qmd
Error: Parse error
     ╭─[ /private/tmp/raw-style/code.jupyter.rmarkdown:153:4 ]
     │
 153 │ :::
     │    ┬
     │    ╰── unexpected character or token here
─────╯

2 errors

A <script> block holding if (a) { and an indented b(); line fails the same way, with a parse
error at the indented line. The same block wrapped in <div> instead of <style> keeps its fence
and renders.

Expected behavior

code.qmd renders as plain.qmd does, with the <style> element in the output, as in Quarto
1.9.37.

Lingua principale
Rust
Stelle
316
Fork
23
Merge medio
11h 49m
PR unite (30g)
245

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di quarto-dev/q2

Tutte le issue di quarto-dev/q2

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.