summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2023-10-08 08:57:37 +0300
committerLars Wirzenius <liw@liw.fi>2023-10-09 18:22:21 +0300
commit5d4e16236a3f6a6e726273a6bec344de1b5f0645 (patch)
tree7fe1ff892ddffee2f6f27425343a29867e5289b1 /src
parent54b70a0eaba4272f60a90386a0ca693bf30887f3 (diff)
downloadsubplot-5d4e16236a3f6a6e726273a6bec344de1b5f0645.tar.gz
fix: only one of each attribute per HTML element
Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r--src/html.rs36
1 files changed, 34 insertions, 2 deletions
diff --git a/src/html.rs b/src/html.rs
index 2562b9b..3232724 100644
--- a/src/html.rs
+++ b/src/html.rs
@@ -7,6 +7,7 @@ use line_col::LineColLookup;
use log::{debug, trace};
use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag};
use serde::{Deserialize, Serialize};
+use std::collections::HashMap;
use std::fmt::Write as _;
use std::io::Write;
use std::path::{Path, PathBuf};
@@ -381,9 +382,14 @@ impl Element {
}
fn serialize_attrs_to_buf(&self, buf: &mut String) -> Result<(), std::fmt::Error> {
+ let mut attrs = Attributes::default();
for attr in self.attrs.iter() {
- write!(buf, " {}", attr.name())?;
- if let Some(value) = attr.value() {
+ attrs.push(attr);
+ }
+
+ for (name, value) in attrs.iter() {
+ write!(buf, " {}", name)?;
+ if !value.is_empty() {
write!(buf, "=\"{}\"", encode_double_quoted_attribute(value))?;
}
}
@@ -492,6 +498,32 @@ impl ElementTag {
}
}
+#[derive(Debug, Default, Clone)]
+struct Attributes {
+ attrs: HashMap<String, String>,
+}
+
+impl Attributes {
+ fn push(&mut self, attr: &Attribute) {
+ if let Some(new_value) = attr.value() {
+ if let Some(old_value) = self.attrs.get_mut(attr.name()) {
+ assert!(!old_value.is_empty());
+ old_value.push(',');
+ old_value.push_str(new_value);
+ } else {
+ self.attrs.insert(attr.name().into(), new_value.into());
+ }
+ } else {
+ assert!(!self.attrs.contains_key(attr.name()));
+ self.attrs.insert(attr.name().into(), "".into());
+ }
+ }
+
+ fn iter(&self) -> impl Iterator<Item = (&String, &String)> {
+ self.attrs.iter()
+ }
+}
+
/// An attribute of an HTML element.
#[derive(Clone, Debug)]
pub struct Attribute {