Compare commits
15 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 01d9e06d63 | |||
| d26ef729eb | |||
| f08eda2758 | |||
| 190e00ed62 | |||
| bb7dedc9eb | |||
| 59b81d637b | |||
| ec7a461758 | |||
| 353934381b | |||
| 0167d3cea3 | |||
| ec7bc571b1 | |||
| 7290a23c85 | |||
| 7b9fae31cd | |||
| ceefa426f6 | |||
| abd0cc6247 | |||
| 099f07ebac |
10 changed files with 1250 additions and 53 deletions
32
CHANGELOG.md
32
CHANGELOG.md
|
|
@ -17,6 +17,38 @@ A dependency update to an incompatible version is considered a breaking change.
|
|||
|
||||
## Unreleased
|
||||
|
||||
## v0.2.0 - 2025-01-01
|
||||
|
||||
### Changed
|
||||
|
||||
- **(breaking)** Updated `axum-core` dependency to `0.5.0`
|
||||
- Relaxed lower bound on `http` dependency to `1.0.0`
|
||||
|
||||
## v0.1.3 - 2024-12-21
|
||||
|
||||
### Added
|
||||
|
||||
- `html::attr::Rel`
|
||||
|
||||
### Fixed
|
||||
|
||||
- Rendering of HTML comments
|
||||
|
||||
## v0.1.2 - 2024-12-14
|
||||
|
||||
### Added
|
||||
|
||||
- `Attr::set`
|
||||
- `html::attr`
|
||||
|
||||
### Deprecated
|
||||
|
||||
- `Attr::new` in favor of `Attr::set`
|
||||
- `Attr::id` in favor of `html::attr::id`
|
||||
- `Attr::class` in favor of `html::attr::class`
|
||||
- `Attr::style` in favor of `html::attr::style`
|
||||
- `Attr::data` in favor of `html::attr::data_x`
|
||||
|
||||
## v0.1.1 - 2024-12-08
|
||||
|
||||
### Added
|
||||
|
|
|
|||
20
Cargo.toml
20
Cargo.toml
|
|
@ -1,10 +1,10 @@
|
|||
[package]
|
||||
name = "el"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
version = "0.2.0"
|
||||
edition = "2024"
|
||||
authors = ["Garmelon <garmelon@plugh.de>"]
|
||||
description = "Write and manipulate HTML elements as data"
|
||||
repository = "https://github.com/Garmelon/el"
|
||||
repository = "https://git.plugh.de/Garmelon/el"
|
||||
license = "MIT OR Apache-2.0"
|
||||
keywords = ["html", "svg", "mathml", "hiccup"]
|
||||
categories = ["web-programming", "template-engine"]
|
||||
|
|
@ -13,16 +13,18 @@ categories = ["web-programming", "template-engine"]
|
|||
axum = ["dep:axum-core", "dep:http"]
|
||||
|
||||
[dependencies]
|
||||
axum-core = { version = "0.4.5", optional = true }
|
||||
http = { version = "1.1.0", optional = true }
|
||||
axum-core = { version = "0.5.0", optional = true }
|
||||
http = { version = "1.0.0", optional = true }
|
||||
|
||||
[lints]
|
||||
rust.unsafe_code = { level = "forbid", priority = 1 }
|
||||
# Lint groups
|
||||
rust.deprecated_safe = "warn"
|
||||
rust.future_incompatible = "warn"
|
||||
rust.keyword_idents = "warn"
|
||||
rust.rust_2018_idioms = "warn"
|
||||
rust.deprecated-safe = "warn"
|
||||
rust.future-incompatible = "warn"
|
||||
rust.keyword-idents = "warn"
|
||||
rust.nonstandard-style = "warn"
|
||||
rust.refining-impl-trait = "warn"
|
||||
rust.rust-2018-idioms = "warn"
|
||||
rust.unused = "warn"
|
||||
# Individual lints
|
||||
rust.let_underscore_drop = "warn"
|
||||
|
|
|
|||
18
README.md
18
README.md
|
|
@ -9,19 +9,18 @@ and named after a small helper function I once wrote in JS.
|
|||
## Show me a simple example
|
||||
|
||||
```rs
|
||||
use el::{Attr, Render, html::*};
|
||||
use el::{Render, html::*};
|
||||
|
||||
let page: String = html((
|
||||
head((
|
||||
meta(Attr::new("charset", "utf-8")),
|
||||
meta((
|
||||
Attr::new("name", "viewport"),
|
||||
Attr::new("content", "width=device-width, initial-scale=1"),
|
||||
attr::name("viewport"),
|
||||
attr::content("width=device-width, initial-scale=1"),
|
||||
)),
|
||||
title("Example page"),
|
||||
)),
|
||||
body((
|
||||
h1((Attr::id("heading"), "Example page")),
|
||||
h1((attr::id("heading"), "Example page")),
|
||||
p(("This is an example for a ", em("simple"), " web page.")),
|
||||
)),
|
||||
))
|
||||
|
|
@ -36,7 +35,7 @@ See the top-level crate documentation for more info.
|
|||
|
||||
## But what about that small helper function?
|
||||
|
||||
Here it is in full, for posteriority:
|
||||
Here it is in full, for posterity:
|
||||
|
||||
```js
|
||||
function el(name, attributes, ...children) {
|
||||
|
|
@ -53,12 +52,11 @@ Use it like so:
|
|||
```js
|
||||
const page = el("html", {},
|
||||
el("head", {},
|
||||
el("meta", { charset: "utf-8" }),
|
||||
el("meta", {
|
||||
name: "viewport",
|
||||
content: "width=device-width, initial-scale=1",
|
||||
name: "viewport",
|
||||
content: "width=device-width, initial-scale=1",
|
||||
}),
|
||||
el("title", {}, "Example page")
|
||||
el("title", {}, "Example page"),
|
||||
),
|
||||
el("body", {},
|
||||
el("h1", { id: "heading" }, "Example page"),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use axum_core::response::IntoResponse;
|
||||
use http::{header, HeaderValue, StatusCode};
|
||||
use http::{HeaderValue, StatusCode, header};
|
||||
|
||||
use crate::{Document, Render};
|
||||
|
||||
|
|
|
|||
|
|
@ -58,10 +58,9 @@ pub fn is_valid_raw_text(tag_name: &str, text: &str) -> bool {
|
|||
// "[...] followed by characters that case-insensitively match the tag
|
||||
// name of the element [...]"
|
||||
//
|
||||
// Note: Since we know that tag names are ascii-only, we can convert
|
||||
// both to lowercase for a case-insensitive comparison without weird
|
||||
// unicode shenanigans.
|
||||
if potential_tag_name.to_ascii_lowercase() != tag_name.to_ascii_lowercase() {
|
||||
// Note: Since we know that tag names are ascii-only, we can use an
|
||||
// ASCII-based case insensitive comparison without unicode shenanigans.
|
||||
if !potential_tag_name.eq_ignore_ascii_case(tag_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::collections::{btree_map::Entry, BTreeMap, HashMap};
|
||||
use std::collections::{BTreeMap, HashMap, btree_map::Entry};
|
||||
|
||||
/// The kind of an element.
|
||||
///
|
||||
|
|
@ -301,7 +301,7 @@ impl Attr {
|
|||
/// When this attribute is added to an [`Element`] through
|
||||
/// [`ElementComponent::add_to_element`] and an attribute of the same name
|
||||
/// already exists, it replaces that attribute's value.
|
||||
pub fn new(name: impl ToString, value: impl ToString) -> Self {
|
||||
pub fn set(name: impl ToString, value: impl ToString) -> Self {
|
||||
Self {
|
||||
name: name.to_string(),
|
||||
value: value.to_string(),
|
||||
|
|
@ -309,6 +309,16 @@ impl Attr {
|
|||
}
|
||||
}
|
||||
|
||||
/// Create or replace an attribute.
|
||||
///
|
||||
/// When this attribute is added to an [`Element`] through
|
||||
/// [`ElementComponent::add_to_element`] and an attribute of the same name
|
||||
/// already exists, it replaces that attribute's value.
|
||||
#[deprecated = "use `Attr::set` instead"]
|
||||
pub fn new(name: impl ToString, value: impl ToString) -> Self {
|
||||
Self::set(name, value)
|
||||
}
|
||||
|
||||
/// Create or append to an attribute.
|
||||
///
|
||||
/// When this attribute is added to an [`Element`] through
|
||||
|
|
@ -330,28 +340,31 @@ impl Attr {
|
|||
/// When rendering an empty attribute as HTML, the value can be omitted:
|
||||
/// `name=""` is equivalent to just `name`.
|
||||
pub fn yes(name: impl ToString) -> Self {
|
||||
Self::new(name, "")
|
||||
Self::set(name, "")
|
||||
}
|
||||
|
||||
/// Create (or replace) an `id` attribute.
|
||||
///
|
||||
/// `Attr::id(id)` is equivalent to `Attr::new("id", id)`.
|
||||
#[deprecated = "use `html::attr::id` instead"]
|
||||
pub fn id(id: impl ToString) -> Self {
|
||||
Self::new("id", id)
|
||||
Self::set("id", id)
|
||||
}
|
||||
|
||||
/// Create (or append) to a `class` attribute.
|
||||
/// Create (or append to) a `class` attribute.
|
||||
///
|
||||
/// `Attr::class(class)` is equivalent to
|
||||
/// `Attr::append("class", class, " ")`.
|
||||
#[deprecated = "use `html::attr::class` instead"]
|
||||
pub fn class(class: impl ToString) -> Self {
|
||||
Self::append("class", class, " ")
|
||||
}
|
||||
|
||||
/// Create (or append) to a `style` attribute.
|
||||
/// Create (or append to) a `style` attribute.
|
||||
///
|
||||
/// `Attr::style(style)` is equivalent to
|
||||
/// `Attr::append("style", style, ";")`.
|
||||
#[deprecated = "use `html::attr::style` instead"]
|
||||
pub fn style(style: impl ToString) -> Self {
|
||||
Self::append("style", style, ";")
|
||||
}
|
||||
|
|
@ -362,8 +375,9 @@ impl Attr {
|
|||
/// `Attr::new(format!("data-{name}"), value)`.
|
||||
///
|
||||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*
|
||||
#[deprecated = "use `html::attr::data_x` instead"]
|
||||
pub fn data(name: impl ToString, value: impl ToString) -> Self {
|
||||
Self::new(format!("data-{}", name.to_string()), value)
|
||||
Self::set(format!("data-{}", name.to_string()), value)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -393,7 +407,7 @@ impl ElementComponent for Attr {
|
|||
impl ElementComponent for HashMap<String, String> {
|
||||
fn add_to_element(self, element: &mut Element) {
|
||||
for (name, value) in self {
|
||||
Attr::new(name, value).add_to_element(element);
|
||||
Attr::set(name, value).add_to_element(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -401,7 +415,7 @@ impl ElementComponent for HashMap<String, String> {
|
|||
impl ElementComponent for BTreeMap<String, String> {
|
||||
fn add_to_element(self, element: &mut Element) {
|
||||
for (name, value) in self {
|
||||
Attr::new(name, value).add_to_element(element);
|
||||
Attr::set(name, value).add_to_element(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -487,8 +501,12 @@ element_component_tuple!(C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11);
|
|||
element_component_tuple!(C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12);
|
||||
element_component_tuple!(C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13);
|
||||
element_component_tuple!(C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14);
|
||||
element_component_tuple!(C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, C15);
|
||||
element_component_tuple!(C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, C15, C16);
|
||||
element_component_tuple!(
|
||||
C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, C15
|
||||
);
|
||||
element_component_tuple!(
|
||||
C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, C15, C16
|
||||
);
|
||||
|
||||
/// A full HTML document including doctype.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
//! Definitions for all non-deprecated HTML elements
|
||||
//! Definitions for HTML elements and attributes
|
||||
//! ([MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element)).
|
||||
//!
|
||||
//! Deprecated HTML elements are not included.
|
||||
|
||||
pub mod attr;
|
||||
|
||||
use crate::{Element, ElementComponent, ElementKind};
|
||||
|
||||
|
|
|
|||
1107
src/html/attr.rs
Normal file
1107
src/html/attr.rs
Normal file
File diff suppressed because it is too large
Load diff
65
src/lib.rs
65
src/lib.rs
|
|
@ -33,19 +33,18 @@
|
|||
//! ## Usage example
|
||||
//!
|
||||
//! ```
|
||||
//! use el::{Attr, Render, html::*};
|
||||
//! use el::{Render, html::*};
|
||||
//!
|
||||
//! let page: String = html((
|
||||
//! head((
|
||||
//! meta(Attr::new("charset", "utf-8")),
|
||||
//! meta((
|
||||
//! Attr::new("name", "viewport"),
|
||||
//! Attr::new("content", "width=device-width, initial-scale=1"),
|
||||
//! attr::name("viewport"),
|
||||
//! attr::content("width=device-width, initial-scale=1"),
|
||||
//! )),
|
||||
//! title("Example page"),
|
||||
//! )),
|
||||
//! body((
|
||||
//! h1((Attr::id("heading"), "Example page")),
|
||||
//! h1((attr::id("heading"), "Example page")),
|
||||
//! p(("This is an example for a ", em("simple"), " web page.")),
|
||||
//! )),
|
||||
//! ))
|
||||
|
|
@ -85,7 +84,7 @@ pub use self::{element::*, render::*};
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{html::*, Attr, Element, Render};
|
||||
use crate::{Attr, Content, Element, Render, html::*};
|
||||
|
||||
#[test]
|
||||
fn simple_website() {
|
||||
|
|
@ -131,9 +130,11 @@ mod tests {
|
|||
|
||||
assert!(script("hello </script> world").render_to_string().is_err());
|
||||
|
||||
assert!(script("hello </ScRiPt ... world")
|
||||
.render_to_string()
|
||||
.is_err());
|
||||
assert!(
|
||||
script("hello </ScRiPt ... world")
|
||||
.render_to_string()
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -150,10 +151,10 @@ mod tests {
|
|||
fn attributes() {
|
||||
assert_eq!(
|
||||
input((
|
||||
Attr::new("name", "tentacles"),
|
||||
Attr::new("type", "number"),
|
||||
Attr::new("min", 10),
|
||||
Attr::new("max", 100),
|
||||
Attr::set("name", "tentacles"),
|
||||
attr::TypeInput::Number,
|
||||
attr::min(10),
|
||||
Attr::append("max", 100, "FOOBAA"),
|
||||
))
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
|
|
@ -161,21 +162,55 @@ mod tests {
|
|||
);
|
||||
|
||||
assert_eq!(
|
||||
input((Attr::new("name", "horns"), Attr::yes("checked")))
|
||||
input((Attr::set("name", "horns"), Attr::yes("checked")))
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
r#"<input checked name="horns">"#,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
p((
|
||||
attr::id("foo"),
|
||||
attr::id("bar"),
|
||||
attr::class("foo"),
|
||||
attr::class("bar"),
|
||||
))
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
r#"<p class="foo bar" id="bar"></p>"#,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn always_lowercase() {
|
||||
assert_eq!(
|
||||
Element::normal("HTML")
|
||||
.with(Attr::new("LANG", "EN"))
|
||||
.with(Attr::set("LANG", "EN"))
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
r#"<html lang="EN"></html>"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn comments() {
|
||||
assert_eq!(
|
||||
html(("<!--abc--> ", Content::comment("abc")))
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
r#"<html><!--abc--> <!--abc--></html>"#,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
html(Content::comment("Hello <!-- world -->!"))
|
||||
.render_to_string()
|
||||
.unwrap(),
|
||||
r#"<html><!--Hello <!== world ==>!--></html>"#,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
html(Content::comment("-><!-")).render_to_string().unwrap(),
|
||||
r#"<html><!-- -><!- --></html>"#,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
use std::{error, fmt};
|
||||
|
||||
use crate::{
|
||||
check,
|
||||
Document, check,
|
||||
element::{Content, Element, ElementKind},
|
||||
Document,
|
||||
};
|
||||
|
||||
/// The cause of an [`Error`].
|
||||
|
|
@ -246,6 +245,8 @@ fn render_text<W: fmt::Write>(w: &mut W, text: &str) -> Result<()> {
|
|||
}
|
||||
|
||||
fn render_comment<W: fmt::Write>(w: &mut W, text: &str) -> Result<()> {
|
||||
write!(w, "<!--")?;
|
||||
|
||||
// A comment...
|
||||
// - must not start with the string ">"
|
||||
// - must not start with the string "->"
|
||||
|
|
@ -269,6 +270,7 @@ fn render_comment<W: fmt::Write>(w: &mut W, text: &str) -> Result<()> {
|
|||
write!(w, " ")?;
|
||||
}
|
||||
|
||||
write!(w, "-->")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue