Match style as a whole attribute name in HTML paste

Found while testing crates/engine/src/cmd/richclip.rs:230-242 (attr, used by tag_style)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-10-24 14:35:42 +00:00
co-authored by Claude Haiku 5.5
parent 38d2ade2b7
commit 744c01f66f
+19 -1
View File
@@ -231,7 +231,11 @@ fn weight_bold(style: &str) -> Option<bool> {
fn attr<'a>(tag: &'a str, key: &str) -> Option<&'a str> {
let lower = tag.to_ascii_lowercase();
let at = lower.find(key)?;
let at = lower.match_indices(key).find_map(|(at, _)| {
let named = lower[..at].ends_with(|c: char| c.is_ascii_whitespace());
let eq = tag.get(at + key.len()..)?.trim_start().starts_with('=');
(named && eq).then_some(at)
})?;
let after = tag.get(at + key.len()..)?.trim_start();
let after = after.strip_prefix('=')?.trim_start();
let quote = after.chars().next()?;
@@ -382,6 +386,20 @@ mod tests {
assert_eq!(heavy.paragraphs[0].runs[0].props.bold, Some(true));
}
#[test]
fn style_attribute_is_found_after_attributes_containing_style() {
for tag in [
"<span class=\"style1\" style=\"font-weight:bold\">x</span>",
"<span data-style=\"x\" style=\"font-weight:bold\">x</span>",
"<span class=\"mystyle\" style=\"font-weight:bold\">x</span>",
] {
let body = html_to_body(tag).unwrap();
assert_eq!(body.paragraphs[0].runs[0].props.bold, Some(true), "{tag}");
}
let none = html_to_body("<span class=\"style\">x</span>").unwrap();
assert_eq!(none.paragraphs[0].runs[0].props.bold, None);
}
#[test]
fn bold_italic_and_underline_round_trip() {
let mut body = TextBody::from_text("Hi there");