Skip to content

Commit

Permalink
allow to group nodes together without having to wrap them in a div
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Oct 31, 2024
1 parent 60f4460 commit 9311e8a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>` for `Node`
* implement `From<[Node; N]>` and `From<Vec<Node>>` for `Node` (group nodes without wrapping them in a `div`)

[Unreleased]: https://github.com/jcornaz/fun-html/compare/...HEAD
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum NodeInner {
},
Text(Cow<'static, str>),
Raw(Cow<'static, str>),
Multiple(Vec<Node>),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -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(())
}
Expand Down
22 changes: 20 additions & 2 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,26 @@ pub fn raw_unsafe(html: String) -> Node {
NodeInner::Raw(html.into()).into()
}

impl<T: Into<Cow<'static, str>>> From<T> for Node {
fn from(value: T) -> Self {
impl From<&'static str> for Node {
fn from(value: &'static str) -> Self {
text(value)
}
}

impl From<String> for Node {
fn from(value: String) -> Self {
text(value)
}
}

impl<const N: usize> From<[Node; N]> for Node {
fn from(value: [Node; N]) -> Self {
Vec::from(value).into()
}
}

impl From<Vec<Node>> for Node {
fn from(value: Vec<Node>) -> Self {
Self(NodeInner::Multiple(value))
}
}
1 change: 1 addition & 0 deletions tests/render_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "<div>a</div><div>b</div>")]
#[case(raw("<my-component></my-component>"), "<my-component></my-component>")]
#[case(raw_unsafe("<my-component></my-component>".to_string()), "<my-component></my-component>")]
#[case(div([("foo", "bar").into()], ["hello".into()]), "<div foo=\"bar\">hello</div>")]
Expand Down

0 comments on commit 9311e8a

Please sign in to comment.