diff --git a/CHANGELOG.md b/CHANGELOG.md index e22bd59..6a44ee2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,14 +7,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] +### Added + * Escape HTML strings * `id` and `class` attributes * Nodes * Html document (`html`, `head` and `body`) * Meta (`title`) - * Text (`h1`, `h2`, `h3`, `h4`, `h5`, `h6` and `text`) + * Text (`h1`, `h2`, `h3`, `h4`, `h5`, `h6`, and `text`) * Container (`div`) * Escape hatches (`raw and `raw_unsafe`) * `Node`, `Attribute` and `Document` types +* implement `From<(&'static str, Cow<'static, str)>` for `Attribute` +* implement `From<&'static str>` and `From` for `Node` +* implement `From<[Node; N]>` and `From>` for `Node` (group nodes without wrapping them in a `div`) [Unreleased]: https://github.com/jcornaz/fun-html/compare/...HEAD diff --git a/src/lib.rs b/src/lib.rs index 6b8952c..6fcf92b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,7 @@ enum NodeInner { }, Text(Cow<'static, str>), Raw(Cow<'static, str>), + Multiple(Vec), } #[derive(Debug, Clone)] @@ -107,6 +108,11 @@ impl Display for Node { } NodeInner::Text(text) => write!(f, "{}", html_escape::encode_text(text))?, NodeInner::Raw(raw) => write!(f, "{raw}")?, + NodeInner::Multiple(nodes) => { + for node in nodes { + write!(f, "{node}")?; + } + } } Ok(()) } diff --git a/src/nodes.rs b/src/nodes.rs index bd39d0f..48ff8d2 100644 --- a/src/nodes.rs +++ b/src/nodes.rs @@ -96,8 +96,26 @@ pub fn raw_unsafe(html: String) -> Node { NodeInner::Raw(html.into()).into() } -impl>> From for Node { - fn from(value: T) -> Self { +impl From<&'static str> for Node { + fn from(value: &'static str) -> Self { text(value) } } + +impl From for Node { + fn from(value: String) -> Self { + text(value) + } +} + +impl From<[Node; N]> for Node { + fn from(value: [Node; N]) -> Self { + Vec::from(value).into() + } +} + +impl From> for Node { + fn from(value: Vec) -> Self { + Self(NodeInner::Multiple(value)) + } +} diff --git a/tests/render_spec.rs b/tests/render_spec.rs index a3d0102..c3c1e27 100644 --- a/tests/render_spec.rs +++ b/tests/render_spec.rs @@ -41,6 +41,7 @@ fn should_render_attribute(#[case] attr: Attribute, #[case] expected: &str) { #[case(text("hello".to_string()), "hello")] #[case("hello".into(), "hello")] #[case("hello".to_string().into(), "hello")] +#[case([div([], ["a".into()]), div([], ["b".into()])].into(), "
a
b
")] #[case(raw(""), "")] #[case(raw_unsafe("".to_string()), "")] #[case(div([("foo", "bar").into()], ["hello".into()]), "
hello
")]