Skip to content

Commit

Permalink
add more attributes for forms
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Nov 3, 2024
1 parent b4c143b commit 2375f4e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Anchor (`a`) and related attributes (`href`, `target` and `download`)
* Image (`img`) and related attributes (`src`, `alt`, `width` and `height`)
* Lists (`ul`, `ol` and `li`)
* Forms (`form`, `input`) and related attributes (`action`, `method`)
* Forms (`form`, `input`) and related attributes
(`action`, `method`, `placeholder`, `for`, `value`, `min`, `max`, `minlength`, `maxlength`, `multiple`)
* 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
50 changes: 50 additions & 0 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,53 @@ pub fn method_get() -> Attribute {
pub fn method_post() -> Attribute {
Attribute::new("method", "post")
}

/// `for` attribute
pub fn for_(value: impl Into<Cow<'static, str>>) -> Attribute {
Attribute::new("for", value)
}

/// `value` attribute
pub fn value(value: impl Into<Cow<'static, str>>) -> Attribute {
Attribute::new("value", value)
}

/// `required` attribute
pub fn required() -> Attribute {
Attribute::new_flag("required")
}

/// `pattern` attribute
pub fn pattern(value: impl Into<Cow<'static, str>>) -> Attribute {
Attribute::new("pattern", value)
}

/// `min` attribute
pub fn min(value: impl Into<Cow<'static, str>>) -> Attribute {
Attribute::new("min", value)
}

/// `max` attribute
pub fn max(value: impl Into<Cow<'static, str>>) -> Attribute {
Attribute::new("max", value)
}

/// `minlength` attribute
pub fn minlength(value: impl Into<Cow<'static, str>>) -> Attribute {
Attribute::new("minlength", value)
}

/// `maxlength` attribute
pub fn maxlength(value: impl Into<Cow<'static, str>>) -> Attribute {
Attribute::new("maxlength", value)
}

/// `multiple` attribute
pub fn multiple() -> Attribute {
Attribute::new_flag("multiple")
}

/// `placeholder` attribute
pub fn placeholder(value: impl Into<Cow<'static, str>>) -> Attribute {
Attribute::new("placeholder", value)
}
10 changes: 10 additions & 0 deletions tests/render_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ fn should_render_html_document() {
#[case(action("something"), "action=\"something\"")]
#[case(method_get(), "method=\"get\"")]
#[case(method_post(), "method=\"post\"")]
#[case(for_("foo"), "for=\"foo\"")]
#[case(value("hello"), "value=\"hello\"")]
#[case(required(), "required")]
#[case(pattern("foobar"), "pattern=\"foobar\"")]
#[case(min("value"), "min=\"value\"")]
#[case(max("value"), "max=\"value\"")]
#[case(minlength("value"), "minlength=\"value\"")]
#[case(maxlength("value"), "maxlength=\"value\"")]
#[case(multiple(), "multiple")]
#[case(placeholder("hello"), "placeholder=\"hello\"")]
fn should_render_attribute(#[case] attr: Attribute, #[case] expected: &str) {
assert_eq!(attr.to_string(), expected);
}
Expand Down

0 comments on commit 2375f4e

Please sign in to comment.