From 9e75a7348b14a48993e36c23b78b79c2b6040183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Drouet?= Date: Fri, 25 Oct 2024 21:37:38 +0200 Subject: [PATCH 1/2] refactor: replace xmlparser by htmlparser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Drouet --- Cargo.lock | 8 +------- Cargo.toml | 1 + packages/mrml-core/Cargo.toml | 4 ++-- packages/mrml-core/lib/html-compare/Cargo.toml | 2 +- packages/mrml-core/src/mj_attributes/parse.rs | 2 +- packages/mrml-core/src/mj_attributes_class/parse.rs | 2 +- packages/mrml-core/src/mj_attributes_element/parse.rs | 2 +- packages/mrml-core/src/mj_body/parse.rs | 2 +- packages/mrml-core/src/mj_breakpoint/parse.rs | 2 +- packages/mrml-core/src/mj_font/parse.rs | 2 +- packages/mrml-core/src/mj_head/parse.rs | 2 +- packages/mrml-core/src/mj_include/body/parse.rs | 4 ++-- packages/mrml-core/src/mj_include/head/parse.rs | 4 ++-- packages/mrml-core/src/mj_raw/parse.rs | 2 +- packages/mrml-core/src/mj_style/parse.rs | 2 +- packages/mrml-core/src/mjml/parse.rs | 2 +- packages/mrml-core/src/prelude/parser/mod.rs | 6 +++--- packages/mrml-core/src/prelude/parser/token.rs | 8 ++++---- 18 files changed, 26 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f06711b..ba2f40d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1192,6 +1192,7 @@ dependencies = [ "enum-as-inner", "enum_dispatch", "html-compare", + "htmlparser", "indexmap", "itertools 0.13.0", "mockito", @@ -1205,7 +1206,6 @@ dependencies = [ "tokio-test", "ureq", "url", - "xmlparser", ] [[package]] @@ -2757,12 +2757,6 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" -[[package]] -name = "xmlparser" -version = "0.13.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4" - [[package]] name = "zerocopy" version = "0.7.35" diff --git a/Cargo.toml b/Cargo.toml index 186ccb33..c7c5128b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ members = [ resolver = "2" [workspace.dependencies] +htmlparser = { version = "0.1" } similar-asserts = { version = "1.6" } diff --git a/packages/mrml-core/Cargo.toml b/packages/mrml-core/Cargo.toml index ffd7927c..b92ea6cd 100644 --- a/packages/mrml-core/Cargo.toml +++ b/packages/mrml-core/Cargo.toml @@ -18,7 +18,7 @@ travis-ci = { repository = "jdrouet/mrml", branch = "main" } [features] default = ["json", "parse", "print", "render"] json = ["dep:serde", "dep:serde_json", "indexmap/serde"] -parse = ["dep:xmlparser", "dep:thiserror"] +parse = ["dep:htmlparser", "dep:thiserror"] print = ["dep:enum_dispatch"] render = ["dep:enum-as-inner", "dep:thiserror"] async = ["dep:async-trait"] @@ -39,7 +39,7 @@ http-loader-ureq = ["dep:ureq", "http-loader-base"] async-trait = { version = "0.1", optional = true } serde = { version = "1.0", optional = true, features = ["derive"] } serde_json = { version = "1.0", optional = true } -xmlparser = { version = "0.13", optional = true } +htmlparser = { workspace = true, optional = true } indexmap = { version = "2.6", default-features = false } rustc-hash = { version = "2.0", default-features = false } thiserror = { version = "1.0", optional = true } diff --git a/packages/mrml-core/lib/html-compare/Cargo.toml b/packages/mrml-core/lib/html-compare/Cargo.toml index eecfa70f..cb479e12 100644 --- a/packages/mrml-core/lib/html-compare/Cargo.toml +++ b/packages/mrml-core/lib/html-compare/Cargo.toml @@ -12,4 +12,4 @@ repository = "https://github.com/jdrouet/mrml" colored = "2.0" css-compare = { path = "../css-compare", version = "0.1" } similar-asserts = { workspace = true } -htmlparser = "0.1" +htmlparser = { workspace = true } diff --git a/packages/mrml-core/src/mj_attributes/parse.rs b/packages/mrml-core/src/mj_attributes/parse.rs index 73bc6381..b7b9abdf 100644 --- a/packages/mrml-core/src/mj_attributes/parse.rs +++ b/packages/mrml-core/src/mj_attributes/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::MjAttributesChild; use crate::mj_attributes_all::NAME as MJ_ALL; diff --git a/packages/mrml-core/src/mj_attributes_class/parse.rs b/packages/mrml-core/src/mj_attributes_class/parse.rs index fa622c57..d518a0de 100644 --- a/packages/mrml-core/src/mj_attributes_class/parse.rs +++ b/packages/mrml-core/src/mj_attributes_class/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::{MjAttributesClass, MjAttributesClassAttributes}; use crate::prelude::hash::Map; diff --git a/packages/mrml-core/src/mj_attributes_element/parse.rs b/packages/mrml-core/src/mj_attributes_element/parse.rs index 1bdfab55..adb6835b 100644 --- a/packages/mrml-core/src/mj_attributes_element/parse.rs +++ b/packages/mrml-core/src/mj_attributes_element/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::MjAttributesElement; use crate::prelude::hash::Map; diff --git a/packages/mrml-core/src/mj_body/parse.rs b/packages/mrml-core/src/mj_body/parse.rs index cf124762..d2b37fe8 100644 --- a/packages/mrml-core/src/mj_body/parse.rs +++ b/packages/mrml-core/src/mj_body/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::MjBodyChild; use crate::comment::Comment; diff --git a/packages/mrml-core/src/mj_breakpoint/parse.rs b/packages/mrml-core/src/mj_breakpoint/parse.rs index 7a95a260..1b61d7e1 100644 --- a/packages/mrml-core/src/mj_breakpoint/parse.rs +++ b/packages/mrml-core/src/mj_breakpoint/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::MjBreakpointAttributes; #[cfg(feature = "async")] diff --git a/packages/mrml-core/src/mj_font/parse.rs b/packages/mrml-core/src/mj_font/parse.rs index e4eb20fb..91e4436f 100644 --- a/packages/mrml-core/src/mj_font/parse.rs +++ b/packages/mrml-core/src/mj_font/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::MjFontAttributes; #[cfg(feature = "async")] diff --git a/packages/mrml-core/src/mj_head/parse.rs b/packages/mrml-core/src/mj_head/parse.rs index d67c470d..6f2d83e7 100644 --- a/packages/mrml-core/src/mj_head/parse.rs +++ b/packages/mrml-core/src/mj_head/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::MjHeadChild; use crate::comment::Comment; diff --git a/packages/mrml-core/src/mj_include/body/parse.rs b/packages/mrml-core/src/mj_include/body/parse.rs index df823b08..328b9782 100644 --- a/packages/mrml-core/src/mj_include/body/parse.rs +++ b/packages/mrml-core/src/mj_include/body/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::{MjIncludeBody, MjIncludeBodyAttributes, MjIncludeBodyChild, MjIncludeBodyKind}; use crate::comment::Comment; @@ -366,7 +366,7 @@ impl crate::prelude::parser::AsyncParseElement for AsyncMrmlParse #[cfg(test)] mod tests { - use xmlparser::StrSpan; + use htmlparser::StrSpan; use crate::mj_include::body::{MjIncludeBody, MjIncludeBodyKind}; use crate::prelude::parser::memory_loader::MemoryIncludeLoader; diff --git a/packages/mrml-core/src/mj_include/head/parse.rs b/packages/mrml-core/src/mj_include/head/parse.rs index daa03ccc..ce47aa79 100644 --- a/packages/mrml-core/src/mj_include/head/parse.rs +++ b/packages/mrml-core/src/mj_include/head/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::{MjIncludeHead, MjIncludeHeadAttributes, MjIncludeHeadChild, MjIncludeHeadKind}; use crate::comment::Comment; @@ -325,7 +325,7 @@ impl MjIncludeHeadKind { #[cfg(test)] mod tests { - use xmlparser::StrSpan; + use htmlparser::StrSpan; use crate::mj_include::head::{MjIncludeHead, MjIncludeHeadKind}; use crate::prelude::parser::memory_loader::MemoryIncludeLoader; diff --git a/packages/mrml-core/src/mj_raw/parse.rs b/packages/mrml-core/src/mj_raw/parse.rs index 256a9ff5..1d16f73d 100644 --- a/packages/mrml-core/src/mj_raw/parse.rs +++ b/packages/mrml-core/src/mj_raw/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::MjRawChild; use crate::comment::Comment; diff --git a/packages/mrml-core/src/mj_style/parse.rs b/packages/mrml-core/src/mj_style/parse.rs index 65f38bfc..2cecd7ea 100644 --- a/packages/mrml-core/src/mj_style/parse.rs +++ b/packages/mrml-core/src/mj_style/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::MjStyleAttributes; #[cfg(feature = "async")] diff --git a/packages/mrml-core/src/mjml/parse.rs b/packages/mrml-core/src/mjml/parse.rs index dbf074b1..e9f2fbb2 100644 --- a/packages/mrml-core/src/mjml/parse.rs +++ b/packages/mrml-core/src/mjml/parse.rs @@ -1,4 +1,4 @@ -use xmlparser::StrSpan; +use htmlparser::StrSpan; use super::{Mjml, MjmlAttributes, MjmlChildren}; use crate::mj_body::NAME as MJ_BODY; diff --git a/packages/mrml-core/src/prelude/parser/mod.rs b/packages/mrml-core/src/prelude/parser/mod.rs index 8e1dd2a0..aa08592d 100644 --- a/packages/mrml-core/src/prelude/parser/mod.rs +++ b/packages/mrml-core/src/prelude/parser/mod.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use xmlparser::{StrSpan, Tokenizer}; +use htmlparser::{StrSpan, Tokenizer}; use self::loader::IncludeLoaderError; use super::hash::Map; @@ -56,12 +56,12 @@ pub enum Error { /// The input string should be smaller than 4GiB. #[error("size limit reached in {origin}")] SizeLimit { origin: Origin }, - /// Errors detected by the `xmlparser` crate. + /// Errors detected by the `htmlparser` crate. #[error("unable to parse next template in {origin}")] ParserError { origin: Origin, #[source] - source: xmlparser::Error, + source: htmlparser::Error, }, /// The Mjml document must have at least one element. #[error("unable to find mjml element")] diff --git a/packages/mrml-core/src/prelude/parser/token.rs b/packages/mrml-core/src/prelude/parser/token.rs index c20bc2f8..1defca8f 100644 --- a/packages/mrml-core/src/prelude/parser/token.rs +++ b/packages/mrml-core/src/prelude/parser/token.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -use xmlparser::{StrSpan, Token}; +use htmlparser::{StrSpan, Token}; use super::MrmlCursor; @@ -82,7 +82,7 @@ impl<'a> MrmlToken<'a> { })), Token::Comment { text, span } => Ok(MrmlToken::Comment(Comment { span, text })), Token::ElementEnd { - end: xmlparser::ElementEnd::Close(prefix, local), + end: htmlparser::ElementEnd::Close(prefix, local), span, } => Ok(MrmlToken::ElementClose(ElementClose { span, @@ -90,11 +90,11 @@ impl<'a> MrmlToken<'a> { local, })), Token::ElementEnd { - end: xmlparser::ElementEnd::Empty, + end: htmlparser::ElementEnd::Empty, span, } => Ok(MrmlToken::ElementEnd(ElementEnd { span, empty: true })), Token::ElementEnd { - end: xmlparser::ElementEnd::Open, + end: htmlparser::ElementEnd::Open, span, } => Ok(MrmlToken::ElementEnd(ElementEnd { span, empty: false })), Token::ElementStart { From ec27c898d2f65a4d9ef4044ed46d50413e1cdb04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Drouet?= Date: Fri, 25 Oct 2024 22:07:11 +0200 Subject: [PATCH 2/2] refactor: handle conditional tokens MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Drouet --- packages/mrml-core/src/prelude/parser/token.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/mrml-core/src/prelude/parser/token.rs b/packages/mrml-core/src/prelude/parser/token.rs index 1defca8f..2e0d7421 100644 --- a/packages/mrml-core/src/prelude/parser/token.rs +++ b/packages/mrml-core/src/prelude/parser/token.rs @@ -47,6 +47,8 @@ impl<'a> From> for Span { | Token::ElementStart { span, .. } | Token::EmptyDtd { span, .. } | Token::EntityDeclaration { span, .. } + | Token::ConditionalCommentStart { span, .. } + | Token::ConditionalCommentEnd { span, .. } | Token::ProcessingInstruction { span, .. } => span.into(), Token::Text { text } => text.into(), }