URLs and strings
url names a file the page loads and refuses a script URL, and quoted writes text as an escaped CSS string.
URLs
const fn url(href: &'static str) -> Url names a file the page loads, and Url writes it as url("..."). background_image, list_style_image, mask_image, content, and the other properties that load an image take it, as does FontFace::new, which Fonts covers. Url::as_str returns the text as written in the program.
use hypersig::style::value::{quoted, url};
assert_eq!(url("/paper.png").to_string(), r#"url("/paper.png")"#);
assert_eq!(
url("https://cdn.example/grain.webp").to_string(),
r#"url("https://cdn.example/grain.webp")"#
);
assert_eq!(quoted("→").to_string(), r#""→""#);
// A quote, a backslash, `<`, and a control character are escaped.
assert_eq!(quoted("say \"hi\"").to_string(), r#""say \"hi\"""#);
assert_eq!(quoted("a\nb").to_string(), r#""a\a b""#);
assert_eq!(quoted("</style>").to_string(), r#""\3c /style>""#);url accepts a path the application serves, such as /paper.png, a URL relative to the page, such as paper.png, and an http or https URL on another host, including one that starts with //.
The URLs url refuses
url panics on an empty URL, a scheme other than http or https, and a space or a control character. That refuses javascript:, and also data: and mailto:. In a const the panic is E0080, so a URL written as a constant is checked by cargo build:
use hypersig::style::value::{Url, url};
// error[E0080]: evaluation panicked: a URL a page loads is a path or an `http` or
// `https` URL, with no space or control character
const TRACKER: Url = url("javascript:alert(1)");Outside a const, the panic happens when the line runs. url takes only &'static str, text written in the program, so a URL built at run time is E0308:
use hypersig::style::Style;
use hypersig::style::value::url;
let avatar: String = format!("/avatars/{}.png", 42);
// error[E0308]: mismatched types
// expected `&str`, found `String`
let style = Style::new().background_image(url(avatar));The compiler's hint, url(&avatar), leads to E0597 instead, because url needs text that lives for the whole program.
Each distinct URL is a declaration of its own, with its own rule in the stylesheet, so a URL per item would add one rule per item. An image that differs per item, such as an avatar, belongs in an <img> whose El::src takes a SafeUrl.
Nothing checks that the file exists or what the host serves. A path no service answers loads nothing, the element shows no image, and the browser's console shows the 404.
Paths under a base
A path that starts with a single / is written under the Base the page is served under, in the first render and in every patch. Under Base::new("/app"), url("/paper.png") loads /app/paper.png. A relative URL follows the page's own path, and a URL on another host is written as given:
use hypersig::html::div;
use hypersig::style::Style;
use hypersig::style::value::url;
use hypersig::{Base, Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Note {}
const APP: Base = Base::new("/app");
let page = Page::new("Note", Note {}).child(
div().css(Style::new().background_image(url("/paper.png"))),
);
// A path from the root goes under the base the page is served under.
assert!(page.render_under(APP).contains(r#"url("/app/paper.png")"#));
assert!(page.render().contains(r#"url("/paper.png")"#));Strings
fn quoted(text: impl Into<String>) -> Quoted writes text as a double-quoted CSS string. content, font_family, list_style_type, text_overflow, and the other properties that take a string take a Quoted. It escapes every character that could end the string or the stylesheet:
| Character | Written | Why |
|---|---|---|
" | \" | It would end the string |
| A backslash | Two backslashes | It would start an escape |
< | \3c | It could close the <style> element the rules are written into |
| A control character, such as a newline | A hex escape, such as \a | A newline would end the string, and the text after it would be read as rules |
Unlike url, quoted takes a String built at run time, and any text is safe in it. Each distinct text is still a rule of its own.
Example: links to other sites
The list marks each item with the string "→ " in place of a bullet, and each link ends with an icon loaded by content: url(...) on its ::after pseudo-element, which Pseudo-elements covers:
use hypersig::html::{El, a, li, ul};
use hypersig::style::value::{quoted, rem, url};
use hypersig::style::{PseudoElement, Style};
use hypersig::{Page, SafeUrl, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Docs {}
/// A list of links to other sites, each item marked with an arrow and
/// followed by an icon.
fn elsewhere(links: &[(&str, SafeUrl)]) -> El<Docs> {
ul().css(Style::new().list_style_type(quoted("→ ")).padding_left(rem(1.5)))
.children(links.iter().map(|(text, href)| {
li().child(
a().href(href.clone())
.css(Style::new().pseudo(
PseudoElement::After,
Style::new()
.content(url("/icons/external.svg"))
.margin_left(rem(0.25)),
))
.text(*text),
)
}))
}
let html = Page::new("Docs", Docs {})
.child(elsewhere(&[("MDN", SafeUrl::new("https://developer.mozilla.org"))]))
.render();Next, CSS-wide keywords covers inherit and the other keywords every property accepts.