Skip to content

Commit

Permalink
build(deps): bump htmlparser to 0.2.1 (#489)
Browse files Browse the repository at this point in the history
* build(deps): bump htmlparser to 0.2.1

Signed-off-by: Jérémie Drouet <[email protected]>

* refacor: apply clippy suggestion

Signed-off-by: Jérémie Drouet <[email protected]>

* refactor: handle using attribute without value in async

Signed-off-by: Jérémie Drouet <[email protected]>

---------

Signed-off-by: Jérémie Drouet <[email protected]>
  • Loading branch information
jdrouet authored Nov 4, 2024
1 parent bf95153 commit a166d3c
Show file tree
Hide file tree
Showing 84 changed files with 700 additions and 305 deletions.
459 changes: 339 additions & 120 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ members = [
resolver = "2"

[workspace.dependencies]
htmlparser = { version = "0.1" }
htmlparser = { version = "0.2" }
similar-asserts = { version = "1.6" }

[workspace.package]
Expand Down
4 changes: 2 additions & 2 deletions packages/mrml-core/lib/html-compare/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl<'a> std::fmt::Display for Error<'a> {
let span_start = expected.span.end();
let span_end = expected_attributes
.iter()
.map(|attr| attr.value.end())
.filter_map(|attr| attr.value.as_ref().map(|value| value.end()))
.max()
.unwrap_or(span_start);
writeln!(
Expand All @@ -223,7 +223,7 @@ impl<'a> std::fmt::Display for Error<'a> {
let span_start = generated.span.end();
let span_end = generated_attributes
.iter()
.map(|attr| attr.value.end())
.filter_map(|attr| attr.value.as_ref().map(|value| value.end()))
.max()
.unwrap_or(span_start);
writeln!(
Expand Down
40 changes: 30 additions & 10 deletions packages/mrml-core/lib/html-compare/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,12 @@ fn compare_attributes<'a>(
let exp_keys = exp_attrs
.iter()
.filter(|attr| {
!(["style", "class"].contains(&attr.local.as_str()) && attr.value.as_str().is_empty())
// if attribute is `class` or `style`, and the value is empty, we can ignore it
if ["class", "style"].contains(&attr.local.as_str()) {
attr.value.map_or(false, |v| !v.is_empty())
} else {
true
}
})
.map(|attr| (attr.local.as_str(), attr.local))
.collect::<BTreeMap<_, _>>();
Expand Down Expand Up @@ -218,15 +223,26 @@ fn compare_attributes<'a>(
.get(key)
.map(|gen_value| (*exp_value, *gen_value))
}) {
if exp_attr.local == "style" {
compare_attr_styles(exp_attr.value, gen_attr.value)?;
} else if exp_attr.local == "class" {
compare_attr_classes(exp_attr.value, gen_attr.value)?;
} else if exp_attr.value.as_str() != gen_attr.value.as_str() {
return Err(ErrorKind::InvalidAttributeValue {
expected: exp_attr.clone(),
generated: exp_attr.clone(),
});
match (exp_attr.value, gen_attr.value) {
(Some(exp_value), Some(gen_value)) => {
if exp_attr.local == "style" {
compare_attr_styles(exp_value, gen_value)?
} else if exp_attr.local == "class" {
compare_attr_classes(exp_value, gen_value)?
} else if exp_value.as_str() != gen_value.as_str() {
return Err(ErrorKind::InvalidAttributeValue {
expected: exp_attr.clone(),
generated: exp_attr.clone(),
});
}
}
(None, Some(inner)) | (Some(inner), None) if !inner.as_str().is_empty() => {
return Err(ErrorKind::InvalidAttributeValue {
expected: exp_attr.clone(),
generated: exp_attr.clone(),
});
}
_ => {}
}
}

Expand Down Expand Up @@ -417,6 +433,10 @@ pub fn compare<'a>(expected: &'a str, generated: &'a str) -> Result<(), Error<'a
}

pub fn assert_similar(expected: &str, generated: &str) {
println!("=== expected");
println!("{expected}");
println!("=== generated");
println!("{generated}");
if let Err(error) = compare(expected, generated) {
panic!("{error}");
}
Expand Down
5 changes: 3 additions & 2 deletions packages/mrml-core/lib/html-compare/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use htmlparser::StrSpan;
pub struct Attribute<'a> {
pub prefix: StrSpan<'a>,
pub local: StrSpan<'a>,
pub value: StrSpan<'a>,
pub value: Option<StrSpan<'a>>,
pub span: StrSpan<'a>,
}

Expand All @@ -31,7 +31,8 @@ impl<'a> Attribute<'a> {
Some(htmlparser::Token::ElementEnd { end, span }) => {
return (result, ElementEnd { end, span })
}
_ => panic!("invalid token in attributes"),
None => panic!("expected attribute that was not found."),
other => panic!("invalid token in attributes: {other:?}"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_accordion/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod tests {
#[test]
fn serialize() {
let mut elt = MjAccordion::default();
elt.attributes.insert("margin".into(), "42px".into());
elt.attributes.insert("margin".into(), Some("42px".into()));
elt.children.push(MjAccordionChild::MjAccordionElement(
MjAccordionElement::default(),
));
Expand Down
5 changes: 4 additions & 1 deletion packages/mrml-core/src/mj_accordion/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ impl<'root> Render<'root> for Renderer<'root, MjAccordion, ()> {
}

fn raw_attribute(&self, key: &str) -> Option<&'root str> {
self.element.attributes.get(key).map(|v| v.as_str())
match self.element.attributes.get(key) {
Some(Some(inner)) => Some(inner),
_ => None,
}
}

fn tag(&self) -> Option<&str> {
Expand Down
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_accordion_element/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ mod tests {
fn serialize() {
let mut elt = MjAccordionElement::default();
elt.attributes
.insert("margin".to_string(), "12px".to_string());
.insert("margin".to_string(), Some("12px".to_string()));
elt.children.title = Some(MjAccordionTitle::new(
Default::default(),
vec![Text::from("Hello".to_string())],
Expand Down
5 changes: 4 additions & 1 deletion packages/mrml-core/src/mj_accordion_element/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ impl<'root> Render<'root> for Renderer<'root, MjAccordionElement, MjAccordionEle
}

fn raw_attribute(&self, key: &str) -> Option<&'root str> {
self.element.attributes.get(key).map(|v| v.as_str())
match self.element.attributes.get(key) {
Some(Some(inner)) => Some(inner),
_ => None,
}
}

fn tag(&self) -> Option<&str> {
Expand Down
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_accordion_text/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod tests {
fn serialize() {
let mut elt = MjAccordionText::default();
elt.attributes
.insert("margin".to_string(), "12px".to_string());
.insert("margin".to_string(), Some("12px".to_string()));
elt.children.push(MjRawChild::Text(Text::from("Hello")));
elt.children.push(MjRawChild::Text(Text::from("World")));
assert_eq!(
Expand Down
5 changes: 4 additions & 1 deletion packages/mrml-core/src/mj_accordion_text/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ impl<'root> Render<'root> for Renderer<'root, MjAccordionText, MjAccordionTextEx
}

fn raw_attribute(&self, key: &str) -> Option<&'root str> {
self.element.attributes.get(key).map(|v| v.as_str())
match self.element.attributes.get(key) {
Some(Some(inner)) => Some(inner),
_ => None,
}
}

fn tag(&self) -> Option<&str> {
Expand Down
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_accordion_title/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod tests {
fn serialize() {
let mut elt = MjAccordionTitle::default();
elt.attributes
.insert("margin".to_string(), "12px".to_string());
.insert("margin".to_string(), Some("12px".to_string()));
elt.children.push(Text::from("Hello"));
elt.children.push(Text::from("World"));
assert_eq!(
Expand Down
5 changes: 4 additions & 1 deletion packages/mrml-core/src/mj_accordion_title/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ impl<'root> Render<'root> for Renderer<'root, MjAccordionTitle, MjAccordionTitle
}

fn raw_attribute(&self, key: &str) -> Option<&'root str> {
self.element.attributes.get(key).map(|v| v.as_str())
match self.element.attributes.get(key) {
Some(Some(inner)) => Some(inner),
_ => None,
}
}

fn tag(&self) -> Option<&str> {
Expand Down
19 changes: 9 additions & 10 deletions packages/mrml-core/src/mj_attributes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl MjAttributes {
child
.attributes
.iter()
.map(|(k, v)| (k.as_str(), v.as_str()))
.filter_map(|(k, v)| v.as_deref().map(|inner| (k.as_str(), inner)))
})
}

Expand All @@ -43,11 +43,10 @@ impl MjAttributes {
.iter()
.filter_map(|child| child.as_mj_attributes_class())
.flat_map(|child| {
child
.attributes
.others
.iter()
.map(move |(k, v)| (child.attributes.name.as_str(), k.as_str(), v.as_str()))
child.attributes.others.iter().filter_map(move |(k, v)| {
v.as_deref()
.map(|inner| (child.attributes.name.as_str(), k.as_str(), inner))
})
})
}

Expand All @@ -56,10 +55,10 @@ impl MjAttributes {
.iter()
.filter_map(|child| child.as_mj_attributes_element())
.flat_map(|child| {
child
.attributes
.iter()
.map(move |(k, v)| (child.name.as_str(), k.as_str(), v.as_str()))
child.attributes.iter().filter_map(move |(k, v)| {
v.as_deref()
.map(|inner| (child.name.as_str(), k.as_str(), inner))
})
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/mrml-core/src/mj_attributes_all/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ mod tests {
#[test]
fn serialize() {
let mut elt = MjAttributesAll::default();
elt.attributes.insert("margin-bottom".into(), "20px".into());
elt.attributes
.insert("margin-bottom".into(), Some("20px".into()));
assert_eq!(
serde_json::to_string(&elt).unwrap(),
r#"{"type":"mj-all","attributes":{"margin-bottom":"20px"}}"#
Expand Down
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_attributes_class/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod tests {
);
elt.attributes
.others
.insert("margin-bottom".into(), "20px".into());
.insert("margin-bottom".into(), Some("20px".into()));
assert_eq!(
serde_json::to_string(&elt).unwrap(),
r#"{"type":"mj-class","attributes":{"name":"classname","margin-bottom":"20px"}}"#
Expand Down
7 changes: 3 additions & 4 deletions packages/mrml-core/src/mj_attributes_class/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::marker::PhantomData;

use crate::prelude::hash::Map;
use crate::prelude::{Component, StaticTag};
use crate::prelude::{AttributeMap, Component, StaticTag};

#[cfg(feature = "json")]
mod json;
Expand All @@ -25,7 +24,7 @@ impl StaticTag for MjAttributesClassTag {
pub struct MjAttributesClassAttributes {
pub name: String,
#[cfg_attr(feature = "json", serde(flatten))]
pub others: Map<String, String>,
pub others: AttributeMap,
}

pub type MjAttributesClass =
Expand All @@ -37,7 +36,7 @@ impl MjAttributesClassAttributes {
fn new(name: String) -> Self {
Self {
name,
others: Map::default(),
others: AttributeMap::default(),
}
}
}
20 changes: 11 additions & 9 deletions packages/mrml-core/src/mj_attributes_class/parse.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use htmlparser::StrSpan;

use super::{MjAttributesClass, MjAttributesClassAttributes};
use crate::prelude::hash::Map;
use crate::prelude::parser::{parse_attributes_map, Error, MrmlCursor, MrmlParser, ParseElement};
#[cfg(feature = "async")]
use crate::prelude::parser::{AsyncMrmlParser, AsyncParseElement};
use crate::prelude::AttributeMap;

#[inline(always)]
fn parse<'a>(cursor: &mut MrmlCursor<'a>, tag: StrSpan<'a>) -> Result<MjAttributesClass, Error> {
let mut others: Map<String, String> = parse_attributes_map(cursor)?;
let name: String = others
.remove("name")
.ok_or_else(|| Error::MissingAttribute {
name: "name",
origin: cursor.origin(),
position: tag.into(),
})?;
let mut others: AttributeMap = parse_attributes_map(cursor)?;
let name: String =
others
.remove("name")
.and_then(|v| v)
.ok_or_else(|| Error::MissingAttribute {
name: "name",
origin: cursor.origin(),
position: tag.into(),
})?;
let attributes = MjAttributesClassAttributes { name, others };

let ending = cursor.assert_element_end()?;
Expand Down
3 changes: 2 additions & 1 deletion packages/mrml-core/src/mj_attributes_element/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ mod tests {
name: "name".into(),
..Default::default()
};
elt.attributes.insert("margin-bottom".into(), "20px".into());
elt.attributes
.insert("margin-bottom".into(), Some("20px".into()));
assert_eq!(
serde_json::to_string(&elt).unwrap(),
r#"{"type":"mj-element","name":"name","attributes":{"margin-bottom":"20px"}}"#
Expand Down
6 changes: 3 additions & 3 deletions packages/mrml-core/src/mj_attributes_element/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::hash::Map;
use crate::prelude::{hash::Map, AttributeMap};

#[cfg(feature = "json")]
mod json;
Expand All @@ -10,7 +10,7 @@ mod print;
#[derive(Clone, Debug, Default)]
pub struct MjAttributesElement {
pub name: String,
pub attributes: Map<String, String>,
pub attributes: AttributeMap,
}

impl MjAttributesElement {
Expand All @@ -27,7 +27,7 @@ impl MjAttributesElement {
&self.name
}

pub fn attributes(&self) -> &Map<String, String> {
pub fn attributes(&self) -> &AttributeMap {
&self.attributes
}
}
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_attributes_element/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::prelude::parser::{AsyncMrmlParser, AsyncParseElement};

#[inline]
fn parse<'a>(cursor: &mut MrmlCursor<'a>, tag: StrSpan<'a>) -> Result<MjAttributesElement, Error> {
let attributes: Map<String, String> = parse_attributes_map(cursor)?;
let attributes: Map<String, Option<String>> = parse_attributes_map(cursor)?;
let ending = cursor.assert_element_end()?;
if !ending.empty {
cursor.assert_element_close()?;
Expand Down
4 changes: 2 additions & 2 deletions packages/mrml-core/src/mj_attributes_element/print.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude::hash::Map;
use crate::prelude::print::PrintableElement;
use crate::prelude::AttributeMap;

impl PrintableElement for super::MjAttributesElement {
type Attrs = Map<String, String>;
type Attrs = AttributeMap;
type Children = ();

fn tag(&self) -> &str {
Expand Down
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_body/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod tests {
#[test]
fn serialize() {
let mut elt = MjBody::default();
elt.attributes.insert("margin".into(), "42px".into());
elt.attributes.insert("margin".into(), Some("42px".into()));
elt.children
.push(MjBodyChild::Text(Text::from("Hello World!")));
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_body/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod tests {
fn with_children() {
let mut item = crate::mj_body::MjBody::default();
item.attributes
.insert("background-color".to_string(), "red".to_string());
.insert("background-color".to_string(), Some("red".to_string()));
item.children
.push(crate::mj_body::MjBodyChild::from(crate::node::Node::from(
"span",
Expand Down
5 changes: 4 additions & 1 deletion packages/mrml-core/src/mj_body/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ impl<'root> Renderer<'root, MjBody, ()> {

impl<'root> Render<'root> for Renderer<'root, MjBody, ()> {
fn raw_attribute(&self, key: &str) -> Option<&'root str> {
self.element.attributes.get(key).map(|v| v.as_str())
match self.element.attributes.get(key) {
Some(Some(inner)) => Some(inner),
_ => None,
}
}

fn default_attribute(&self, key: &str) -> Option<&'static str> {
Expand Down
Loading

0 comments on commit a166d3c

Please sign in to comment.