-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathbutton.rs
More file actions
95 lines (85 loc) · 3.41 KB
/
button.rs
File metadata and controls
95 lines (85 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use webapi::element::{Element, IElement};
use webapi::event_target::{EventTarget, IEventTarget};
use webapi::html_element::{HtmlElement, IHtmlElement};
use webapi::node::{INode, Node};
use webcore::try_from::TryInto;
use webcore::value::Reference;
/// The HTML `<button>` element represents a clickable button
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button)
// https://html.spec.whatwg.org/#the-button-element
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "HTMLButtonElement")]
#[reference(subclass_of(EventTarget, Node, Element, HtmlElement))]
pub struct ButtonElement(Reference);
impl IEventTarget for ButtonElement {}
impl INode for ButtonElement {}
impl IElement for ButtonElement {}
impl IHtmlElement for ButtonElement {}
impl ButtonElement {
/// The type attribute controls the behavior of the button when it is activated.
/// It is an enumerated attribute. Allowed values: submit | reset | button
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type)
/// https://html.spec.whatwg.org/#attr-button-type
#[inline]
pub fn set_type(&self, kind: &str) {
js! { @(no_return)
@{self}.type = @{kind};
}
}
/// This Boolean attribute prevents the user from interacting with the button: it cannot be pressed or focused.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-disabled)
/// https://html.spec.whatwg.org/#attr-fe-disabled
#[inline]
pub fn set_disabled(&self, status: bool) {
js! { @(no_return)
@{self}.disabled = @{status};
}
}
/// This Boolean attribute prevents the user from interacting with the button: it cannot be pressed or focused.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-disabled)
/// https://html.spec.whatwg.org/#attr-fe-disabled
#[inline]
pub fn is_disabled(&self) -> bool {
js!(
return @{self}.disabled;
).try_into().unwrap()
}
/// The name of the button, submitted as a pair with the button’s value as part of the form data.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-name)
/// https://html.spec.whatwg.org/#attr-fe-name
#[inline]
pub fn set_name(&self, name: &str) {
js! { @(no_return)
@{self}.name = @{name};
}
}
/// Defines the value associated with the button’s name when it’s submitted with the form data.
/// This value is passed to the server in params when the form is submitted.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-value)
/// https://html.spec.whatwg.org/#attr-button-value
#[inline]
pub fn set_raw_value(&self, value: &str) {
js! { @(no_return)
@{self}.value = @{value};
}
}
}
#[cfg(all(test, feature = "web_test"))]
mod tests {
use super::ButtonElement;
use webapi::node::Node;
use webcore::try_from::TryInto;
#[test]
fn test_select_one() {
let html = r#"<button>Click me</button>"#;
let button: ButtonElement = Node::from_html(html).unwrap().try_into().unwrap();
let is_disabled = button.is_disabled();
assert_eq!(is_disabled, false);
}
}