fix(formats): support small and big tags and relative/large font-size keywords in HTML

This commit is contained in:
Divyanshu Gupta
2026-10-15 23:45:12 +05:30
parent fd0bb2fb1e
commit 9e477da0ee
+19 -2
View File
@@ -496,11 +496,12 @@ fn css_len_pt(v: &str) -> Option<f32> {
match v.as_str() {
"xx-small" => Some(7.0),
"x-small" => Some(7.5),
"small" => Some(10.0),
"small" | "smaller" => Some(10.0),
"medium" => Some(12.0),
"large" => Some(13.5),
"large" | "larger" => Some(13.5),
"x-large" => Some(18.0),
"xx-large" => Some(24.0),
"xxx-large" => Some(36.0),
_ => num(&v).map(|p| p * 0.75),
}
}
@@ -1011,6 +1012,8 @@ impl<'r> Builder<'r> {
"code" | "kbd" | "tt" | "samp" => fmt.code = true,
"sup" => fmt.sup = true,
"sub" => fmt.sub = true,
"small" => fmt.size = Some(fmt.size.map_or(10.0, |s| (s * 0.833).max(1.0))),
"big" => fmt.size = Some(fmt.size.map_or(14.0, |s| (s * 1.2).min(1638.0))),
"mark" => fmt.background = Some(wordcraft_doc::Rgb(255, 255, 0)),
"font" => {
if let Some(c) = attr(attrs, "color").and_then(parse_color) {
@@ -1785,4 +1788,18 @@ mod tests {
fn entities() {
assert_eq!(unescape("a &lt;b&gt; &#65;&#x42; &bogus; &"), "a <b> AB &bogus; &");
}
#[test]
fn small_big_and_css_font_sizes() {
let f = parse("<p><small>small</small> <big>big</big> <span style=\"font-size: xxx-large\">huge</span> <span style=\"font-size: smaller\">tiny</span></p>");
let FBlock::Para(p) = &f.blocks[0] else { panic!() };
let sz = |i: usize| match &p.inlines[i] {
Inline::Text(_, f) => f.size,
_ => None,
};
assert_eq!(sz(0), Some(10.0));
assert_eq!(sz(2), Some(14.0));
assert_eq!(sz(4), Some(36.0));
assert_eq!(sz(6), Some(10.0));
}
}