bcef714805
`{{url|alt|key="quoted value"}}` syntax was already parsed end-to-end
through to `TransclusionNode::attrs`, but the value retained the
surrounding `"…"` (or `'…'`) characters verbatim. The HTML renderer
then escaped them into `"`, producing `style=""border: 1px""`
instead of clean `style="border: 1px"`.
Extracted a small `insert_attr` helper that splits on `=`, trims, and
strips matching single- or double-quote pairs from the value before
inserting. Unquoted values pass through unchanged.
Tests: 4 new in crates/nuwiki-core/tests/transclusion_attrs.rs
covering double-quote stripping, single-quote stripping, unquoted
pass-through, and the clean-HTML rendering shape (no `"`).
Note: this commit covers the "transclusion attrs" half of the
original Cluster 8 audit. The "multi-line list-item continuation
paragraphs" half requires lexer + parser changes (continuation lines
indented past the marker should attach to the prior list item
instead of becoming a sibling paragraph) and is deferred.
Gates: 429 Rust / 39 Neovim / 12 Vim.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
61 lines
2.0 KiB
Rust
61 lines
2.0 KiB
Rust
//! Cluster 8 — transclusion attribute parsing. The basic
|
|
//! `{{url|alt|key=val}}` shape was already wired; this test pins
|
|
//! down quote-stripping on quoted values (`key="quoted value"`)
|
|
//! so an HTML renderer doesn't double-emit `"` artefacts.
|
|
|
|
use nuwiki_core::ast::{BlockNode, InlineNode};
|
|
use nuwiki_core::render::{HtmlRenderer, Renderer};
|
|
use nuwiki_core::syntax::vimwiki::VimwikiSyntax;
|
|
use nuwiki_core::syntax::SyntaxPlugin;
|
|
|
|
fn parse(src: &str) -> nuwiki_core::ast::DocumentNode {
|
|
VimwikiSyntax::new().parse(src)
|
|
}
|
|
|
|
fn first_transclusion(doc: &nuwiki_core::ast::DocumentNode) -> &nuwiki_core::ast::TransclusionNode {
|
|
let para = match &doc.children[0] {
|
|
BlockNode::Paragraph(p) => p,
|
|
_ => panic!("expected paragraph"),
|
|
};
|
|
for child in ¶.children {
|
|
if let InlineNode::Transclusion(t) = child {
|
|
return t;
|
|
}
|
|
}
|
|
panic!("no transclusion in paragraph");
|
|
}
|
|
|
|
#[test]
|
|
fn transclusion_attrs_strip_double_quotes() {
|
|
let doc = parse(r#"{{cat.png|cat|style="border: 1px"}}"#);
|
|
let t = first_transclusion(&doc);
|
|
assert_eq!(t.url, "cat.png");
|
|
assert_eq!(t.alt.as_deref(), Some("cat"));
|
|
assert_eq!(
|
|
t.attrs.get("style").map(String::as_str),
|
|
Some("border: 1px")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn transclusion_attrs_strip_single_quotes() {
|
|
let doc = parse("{{cat.png|cat|class='thumb'}}");
|
|
let t = first_transclusion(&doc);
|
|
assert_eq!(t.attrs.get("class").map(String::as_str), Some("thumb"));
|
|
}
|
|
|
|
#[test]
|
|
fn transclusion_attrs_leave_unquoted_values_alone() {
|
|
let doc = parse("{{cat.png|cat|width=200}}");
|
|
let t = first_transclusion(&doc);
|
|
assert_eq!(t.attrs.get("width").map(String::as_str), Some("200"));
|
|
}
|
|
|
|
#[test]
|
|
fn transclusion_renders_clean_html_with_quoted_attr() {
|
|
let doc = parse(r#"{{cat.png|cat|style="border: 1px"}}"#);
|
|
let out = HtmlRenderer::new().render_to_string(&doc).unwrap();
|
|
assert!(out.contains(r#"style="border: 1px""#), "got: {out}");
|
|
assert!(!out.contains("""));
|
|
}
|