Skip to content

Commit

Permalink
check tag name of void elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Nov 1, 2024
1 parent df7ae32 commit a5a8c86
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ impl Element {
attributes: impl IntoIterator<Item = Attribute>,
children: impl IntoIterator<Item = Element>,
) -> Self {
debug_assert!(
!tag.is_empty() && tag.chars().all(|c| !c.is_whitespace()),
"invalid attribute name: '{tag}'"
);
assert_valid_tag_name(tag);
Self(ElementInner::Parent {
tag,
attributes: attributes.into_iter().map(Into::into).collect(),
Expand All @@ -76,13 +73,21 @@ impl Element {
///
/// [void]: https://developer.mozilla.org/en-US/docs/Glossary/Void_element
pub fn new_void(tag: &'static str, attributes: impl IntoIterator<Item = Attribute>) -> Self {
assert_valid_tag_name(tag);
Self(ElementInner::Void {
tag,
attributes: attributes.into_iter().collect(),
})
}
}

fn assert_valid_tag_name(tag: &str) {
debug_assert!(
!tag.is_empty() && tag.chars().all(|c| !c.is_whitespace()),
"invalid tag name: '{tag}'"
);
}

impl From<ElementInner> for Element {
fn from(value: ElementInner) -> Self {
Self(value)
Expand Down
9 changes: 9 additions & 0 deletions tests/render_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,12 @@ fn should_panic_for_invalid_tag_name(
) {
Element::new(name, [], []);
}

#[rstest]
#[cfg(debug_assertions)]
#[should_panic]
fn should_panic_for_invalid_void_element_name(
#[values("hello world", "hello\tworld", "hello\nworld", "")] name: &'static str,
) {
Element::new_void(name, []);
}

0 comments on commit a5a8c86

Please sign in to comment.