1use htmlparser::StrSpan;
2
3use super::MjFontAttributes;
4#[cfg(feature = "async")]
5use crate::prelude::parser::AsyncMrmlParser;
6use crate::prelude::parser::{Error, MrmlCursor, MrmlParser, ParseAttributes, WarningKind};
7
8#[inline(always)]
9fn parse_attributes(cursor: &mut MrmlCursor<'_>) -> Result<MjFontAttributes, Error> {
10 let mut result = MjFontAttributes::default();
11
12 while let Some(attrs) = cursor.next_attribute()? {
13 match (attrs.local.as_str(), attrs.value) {
14 ("name", Some(value)) => result.name = value.to_string(),
15 ("href", Some(value)) => result.href = value.to_string(),
16 _ => cursor.add_warning(WarningKind::UnexpectedAttribute, attrs.span),
17 }
18 }
19
20 Ok(result)
21}
22
23impl ParseAttributes<MjFontAttributes> for MrmlParser<'_> {
24 fn parse_attributes(
25 &self,
26 cursor: &mut MrmlCursor<'_>,
27 _tag: &StrSpan<'_>,
28 ) -> Result<MjFontAttributes, Error> {
29 parse_attributes(cursor)
30 }
31}
32
33#[cfg(feature = "async")]
34impl ParseAttributes<MjFontAttributes> for AsyncMrmlParser {
35 fn parse_attributes(
36 &self,
37 cursor: &mut MrmlCursor<'_>,
38 _tag: &StrSpan<'_>,
39 ) -> Result<MjFontAttributes, Error> {
40 parse_attributes(cursor)
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use crate::mj_font::MjFont;
47
48 crate::should_sync_parse!(
49 success,
50 MjFont,
51 r#"<mj-font name="Comic" href="https://jolimail.io" />"#
52 );
53
54 crate::should_sync_parse!(
55 unexpected_attribute,
56 MjFont,
57 r#"<mj-font unknown="whatever" />"#,
58 1
59 );
60}