Skip to content

Commit

Permalink
add download with name
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Nov 1, 2024
1 parent ce8b440 commit 54d0e8d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Meta (`meta`, `title`)
* Text (`h1` to `h6`, and `text`)
* Container (`div`)
* Anchor (`a`) and related attributes (`href`, `download`)
* Anchor (`a`) and related attributes (`href`, `target` and `download`)
* Escape hatches (`raw` and `raw_unsafe`)
* implement `From<(&'static str, Cow<'static, str)>` for `Attribute`
* implement `From<&'static str>` and `From<String>` for `Element`
Expand Down
30 changes: 30 additions & 0 deletions src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@ use alloc::{borrow::Cow, string::String};

use crate::Attribute;

pub enum Target {
Blank,
Self_,
Parent,
Top,
Frame(Cow<'static, str>),
}

pub fn target(target: Target) -> Attribute {
Attribute::new(
"target",
match target {
Target::Blank => "_blank".into(),
Target::Self_ => "_self".into(),
Target::Parent => "_parent".into(),
Target::Top => "_top".into(),
Target::Frame(name) => name,
},
)
}

/// Alias for `target(Target::Blank)`
pub fn target_blank() -> Attribute {
target(Target::Blank)
}

impl<T: Into<Cow<'static, str>>> From<(&'static str, T)> for Attribute {
fn from((key, value): (&'static str, T)) -> Self {
Attribute::new(key, value)
Expand All @@ -24,6 +50,10 @@ pub fn download() -> Attribute {
Attribute::new_flag("download")
}

pub fn download_with_name(name: impl Into<Cow<'static, str>>) -> Attribute {
Attribute::new("download", name)
}

pub fn class<'a>(classes: impl IntoIterator<Item = &'a str>) -> Attribute {
let mut values = String::new();
let mut iter = classes.into_iter();
Expand Down
7 changes: 7 additions & 0 deletions tests/render_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ fn should_render_html_document() {
#[case(class(["foo", "bar"]), "class=\"foo bar\"")]
#[case(href("foo"), "href=\"foo\"")]
#[case(download(), "download")]
#[case(download_with_name("myfile.txt"), "download=\"myfile.txt\"")]
#[case(target(Target::Blank), "target=\"_blank\"")]
#[case(target_blank(), "target=\"_blank\"")]
#[case(target(Target::Self_), "target=\"_self\"")]
#[case(target(Target::Top), "target=\"_top\"")]
#[case(target(Target::Parent), "target=\"_parent\"")]
#[case(target(Target::Frame("myframe".into())), "target=\"myframe\"")]
fn should_render_attribute(#[case] attr: Attribute, #[case] expected: &str) {
let string = div([attr], []).to_string();
assert_eq!(string, format!("<div {expected}></div>"));
Expand Down

0 comments on commit 54d0e8d

Please sign in to comment.