Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/unreleased/2026-11-28-web-export-inline-links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **`--html` keeps styled spans inside inline links on the same line.** `BASE_CSS` now applies `display: inline; position: static` to all descendants of a text element (`[data-rune-static] .t *`) instead of direct children only, so styled `<span>` runs inside `<a href="…">` stay inline with surrounding prose while standalone link containers keep their flex layout.
5 changes: 3 additions & 2 deletions crates/rune/src/runtime/web/export/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use std::fmt::Write;
use super::ExportNode;

/// The rule every exported element starts from: a Rune element is a flex row, positioned, with
/// its border inside its size. A text element (class `t`) holds inline runs instead.
pub(crate) const BASE_CSS: &str = "[data-rune-static],[data-rune-static] *{box-sizing:border-box;display:flex;position:relative;margin:0;padding:0;min-width:0;border:0 solid transparent}[data-rune-static] a{color:inherit;text-decoration:none}[data-rune-static] .t>*{display:inline;position:static}[data-rune-static] img{display:block}:where([data-rune-static]) img{height:auto}[data-rune-static] svg{display:block;position:absolute;inset:0}";
/// its border inside its size. A text element (class `t`) holds inline runs (including styled
/// spans nested inside inline links) instead.
pub(crate) const BASE_CSS: &str = "[data-rune-static],[data-rune-static] *{box-sizing:border-box;display:flex;position:relative;margin:0;padding:0;min-width:0;border:0 solid transparent}[data-rune-static] a{color:inherit;text-decoration:none}[data-rune-static] .t *{display:inline;position:static}[data-rune-static] img{display:block}:where([data-rune-static]) img{height:auto}[data-rune-static] svg{display:block;position:absolute;inset:0}";

/// The value a property takes where a width sets nothing, matching `BASE_CSS`.
fn initial(prop: &str) -> &'static str {
Expand Down
87 changes: 86 additions & 1 deletion crates/rune/src/runtime/web/export/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn folds_widths_into_media_rules_and_hides_what_a_width_lacks() {
assert!(media.contains("flex-direction:row"), "{media}");
assert!(!body.contains("Wide only"), "the body is the narrowest tree: {body}");
assert!(wide.iter().any(|n| n.inner == "Wide only"));
assert!(css.contains(".t>*{display:inline"), "a text element's runs stay inline: {css}");
assert!(css.contains(".t *{display:inline;position:static}"), "a text element's inline runs and nested spans stay inline: {css}");
assert!(body.contains("<p class=\"t"), "{body}");
let opened = body.matches('<').count() - body.matches("</").count();
assert_eq!(opened, body.matches("</").count(), "every element closes: {body}");
Expand Down Expand Up @@ -462,3 +462,88 @@ fn html_export_keeps_numeric_leading_and_punctuation_anchor_ids_and_styles_them_
assert!(!node.attrs.contains(" id="), "invalid key {key:?} must not emit id: {}", node.attrs);
}
}

#[test]
fn styled_links_inside_paragraphs_and_headings_stay_inline_while_link_containers_keep_flex() {
let mut rt = Runtime::new(
(),
|_, _| {
let accent = crate::color::hex("#009fff").unwrap();
crate::col().key("root").w_full().role("main")
.child(
crate::rich(
"See the setup guide and rune_export docs for details.",
vec![
crate::Span::new(8, 19).color(accent).underline(accent),
crate::Span::new(24, 35).mono(),
crate::Span::new(36, 40).weight(700.0),
],
)
.key("prose")
.font(crate::tok::type_::BODY)
.link(crate::Link::new(8..19, "https://example.com/setup"))
.link(crate::Link::new(24..40, "https://example.com/export")),
)
.child(
crate::rich("Read the API", vec![crate::Span::new(9, 12).color(accent)])
.key("heading")
.role("heading")
.level(2)
.font(crate::tok::type_::TITLE)
.link(crate::Link::new(9..12, "/api")),
)
.child(
crate::col()
.key("card")
.href("/guide")
.gap(8.0)
.child(crate::text("Guide").font(crate::tok::type_::BODY))
.child(
crate::rich("With bold copy", vec![crate::Span::new(5, 9).weight(700.0)])
.font(crate::tok::type_::BODY),
),
)
},
(600.0, 400.0),
Env::default(),
);
rt.page_mode();
let nodes = taken(&mut rt, 600.0);
let (body, css) = page(&[(600.0, nodes.clone())]);

// Paragraph wraps styled spans inside <a>, all under .t.
let prose = nodes.iter().find(|n| n.path.ends_with("prose")).expect("prose");
assert!(prose.text, "paragraph exports with class t");
assert_eq!(
prose.inner,
"See the <a href=\"https://example.com/setup\"><span style=\"color:#009fff;text-decoration:underline #009fff\">setup guide</span></a> and <a href=\"https://example.com/export\"><span style=\"font-family:var(--rune-mono)\">rune_export</span> <span style=\"font-weight:700\">docs</span></a> for details.",
);

// Heading also exports with class t and nested <a><span>.
let heading = nodes.iter().find(|n| n.path.ends_with("heading")).expect("heading");
assert_eq!(heading.tag, "h2");
assert!(heading.text, "heading exports with class t");
assert_eq!(heading.inner, "Read the <a href=\"/api\"><span style=\"color:#009fff\">API</span></a>");

// BASE_CSS uses a descendant selector under .t after the global flex rule so grandchildren
// like `.t a span` get `display:inline;position:static` instead of `display:flex`.
let base_flex = css.find("[data-rune-static] *{box-sizing:border-box;display:flex;position:relative").expect("base flex rule");
let inline_rule = css.find("[data-rune-static] .t *{display:inline;position:static}").expect("descendant inline rule");
assert!(inline_rule > base_flex, "inline rule must follow base flex rule: {css}");
assert!(!css.contains(".t>*"), "direct-child selector would leave `.t a span` as display:flex: {css}");

// Standalone link container stays outside `.t` and keeps its flex column layout; its inner
// text children remain `.t` elements so their own styled spans stay inline.
let card = nodes.iter().find(|n| n.path.ends_with("card")).expect("card link container");
assert_eq!(card.tag, "a");
assert!(!card.text, "standalone link container must not carry class t");
assert!(card.decls.contains(&("flex-direction", "column".into())), "{:?}", card.decls);
assert!(card.decls.contains(&("row-gap", "8px".into())), "{:?}", card.decls);
let card_open = body.split("<a ").find(|s| s.contains("id=\"card\"")).expect("card tag");
let card_tag = card_open.split('>').next().unwrap();
assert!(!card_tag.contains("class=\"t ") && !card_tag.contains("class=\"t\""), "card <a> must not have class t: <a {card_tag}>");
assert!(
body.contains("<span class=\"t ") && body.contains("With <span style=\"font-weight:700\">bold</span> copy"),
"rich text inside standalone link container keeps its own .t scope: {body}",
);
}
Loading