Skip to content

Commit

Permalink
add meta_charset_utf_8, meta_color_scheme and link_stylesheet
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Nov 5, 2024
1 parent 8bcb6ea commit 0f8a85e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Added

* `meta_charset_utf_8`, `meta_color_scheme` and `link_stylesheet`


## [1.2.1] - 2024-11-05

Expand Down
20 changes: 19 additions & 1 deletion src/elt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use alloc::{borrow::Cow, string::String};

use crate::{Attribute, Element, ElementInner};
use crate::{
attr::{content, href, name, rel},
Attribute, Element, ElementInner,
};

/// Renders nothing. Useful fo conditional rendering.
///
Expand Down Expand Up @@ -47,16 +50,31 @@ pub fn meta(attributes: impl IntoIterator<Item = Attribute>) -> Element {
Element::new_void("meta", attributes)
}

/// `<meta name="viewport" content="width=device-width, initial-scale=1.0">`
pub fn meta_charset_utf_8() -> Element {
raw("<meta charset=\"UTF-8\">")
}

/// `<meta name="viewport" content="width=device-width, initial-scale=1.0">`
pub fn meta_viewport() -> Element {
raw("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
}

/// `<meta name="color-scheme content="{scheme}">
pub fn meta_color_scheme(scheme: impl Into<Cow<'static, str>>) -> Element {
meta([name("color-scheme"), content(scheme)])
}

/// `<link>`
pub fn link(attributes: impl IntoIterator<Item = Attribute>) -> Element {
Element::new_void("link", attributes)
}

/// `<link rel="stylesheet" href="{url}">`
pub fn link_stylesheet(url: impl Into<Cow<'static, str>>) -> Element {
link([rel("stylesheet"), href(url)])
}

/// `<script>`
pub fn script(attributes: impl IntoIterator<Item = Attribute>, content: &'static str) -> Element {
Element::new(
Expand Down
11 changes: 10 additions & 1 deletion tests/render_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,23 @@ fn should_render_attribute(#[case] attr: Attribute, #[case] expected: &str) {
#[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(meta([("foo", "bar").into()]), "<meta foo=\"bar\">")]
#[case(link([("foo", "bar").into()]), "<link foo=\"bar\">")]
#[case(
link_stylesheet("/styles.css"),
"<link rel=\"stylesheet\" href=\"/styles.css\">"
)]
#[case(script([("foo", "bar").into()], "alert('hello');"), "<script foo=\"bar\">alert('hello');</script>")]
#[case(script_empty([src("/foo.js")]), "<script src=\"/foo.js\"></script>")]
#[case(meta([("foo", "bar").into()]), "<meta foo=\"bar\">")]
#[case(meta_charset_utf_8(), "<meta charset=\"UTF-8\">")]
#[case(
meta_viewport(),
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
)]
#[case(
meta_color_scheme("dark"),
"<meta name=\"color-scheme\" content=\"dark\">"
)]
#[case(div([("foo", "bar").into()], ["hello".into()]), "<div foo=\"bar\">hello</div>")]
#[case(div([("foo", "bar".to_string()).into()], [text("hello".to_string())]), "<div foo=\"bar\">hello</div>")]
#[case(head([id("foo")], [text("hello")]), "<head id=\"foo\">hello</head>")]
Expand Down

0 comments on commit 0f8a85e

Please sign in to comment.