Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inputevent listener for all elements #549

Merged
merged 1 commit into from
Aug 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,23 +439,15 @@ impl_action! {
oninput(event: InputEvent) -> InputData => |this: &Element, _| {
use stdweb::web::html_element::{InputElement, TextAreaElement};
use stdweb::unstable::TryInto;
let value = match this.clone().try_into() {
Ok(input) => {
let input: InputElement = input;
input.raw_value()
}
Err(_e) => {
match this.clone().try_into() {
Ok(tae) => {
let tae: TextAreaElement = tae;
tae.value()
}
Err(_e) => {
panic!("only an InputElement or TextAreaElement can have an oninput event listener");
}
}
}
};
// Normally only InputElement or TextAreaElement can have an oninput event listener. In
// practice though any element with `contenteditable=true` may generate such events,
// therefore here we fall back to just returning the text content of the node.
// See https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event.
let v1 = this.clone().try_into().map(|input: InputElement| input.raw_value()).ok();
let v2 = this.clone().try_into().map(|input: TextAreaElement| input.value()).ok();
let v3 = this.text_content();
let value = v1.or(v2).or(v3)
.expect("only an InputElement or TextAreaElement or an element with contenteditable=true can have an oninput event listener");
InputData { value }
}
onchange(event: ChangeEvent) -> ChangeData => |this: &Element, _| {
Expand Down