Skip to content

Fix panic when clicking on range inputs #318

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

Merged
merged 2 commits into from
Dec 20, 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
50 changes: 30 additions & 20 deletions src/browser/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,26 @@ fn set_html_input_element_value(
input: &web_sys::HtmlInputElement,
value: &str,
) -> Result<(), &'static str> {
// https://www.w3schools.com/tags/att_input_value.asp
if input.type_().as_str() == "file" {
return Err(r#"The value attribute cannot be used with <input type="file">."#);
}
// In some cases we need to set selection manually because
// otherwise the cursor would jump at the end on some platforms.

// `selectionStart` and `selectionEnd`
// - "If this element is an input element, and selectionStart does not apply to this element, return null."
// - https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionstart
// - => return values if the element type is:
// - `text`, `search`, `url`, `tel`, `password` and probably also `week`, `month`
// - https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement
// - https://html.spec.whatwg.org/multipage/input.html#do-not-apply
let selection_update_required = match input.type_().as_str() {
// https://www.w3schools.com/tags/att_input_value.asp
"file" => return Err(r#"The value attribute cannot be used with <input type="file">."#),
"text" | "password" | "search" | "tel" | "url" | "week" | "month" => true,
_ => false,
};

let input_is_active =
document().active_element().as_ref() == input.dyn_ref::<web_sys::Element>();
// We don't want to set selection in inactive input because
// that input would "steal" focus from the active element on some platforms.
if input_is_active {
// We need to set selection manually because otherwise the cursor would jump at the end on some platforms.

// `selectionStart` and `selectionEnd`
// - "If this element is an input element, and selectionStart does not apply to this element, return null."
// - https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionstart
// - => return values if the element type is:
// - `text`, `search`, `url`, `tel`, `password` and probably also `week`, `month`
// - https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement
// - https://html.spec.whatwg.org/multipage/input.html#do-not-apply
if selection_update_required && is_active(input) {
let selection_start = input
.selection_start()
.expect("get `HtmlInputElement` selection start");
Expand All @@ -224,14 +225,21 @@ fn set_html_input_element_value(
Ok(())
}

/// Return true if passed element is active.
fn is_active(element: &web_sys::Element) -> bool {
document().active_element().as_ref() == Some(element)
}

/// Similar to `get_value`
#[allow(dead_code)]
pub fn get_checked(target: &web_sys::EventTarget) -> Result<bool, &'static str> {
if let Some(input) = target.dyn_ref::<web_sys::HtmlInputElement>() {
// https://www.w3schools.com/tags/att_input_checked.asp
return match input.type_().as_str() {
"file" => Err(r#"The checked attribute can be used with <input type="checkbox"> and <input type="radio">."#),
_ => Ok(input.checked())
"file" => Err(
r#"The checked attribute can be used with <input type="checkbox"> and <input type="radio">."#,
),
_ => Ok(input.checked()),
};
}
if let Some(input) = target.dyn_ref::<web_sys::HtmlMenuItemElement>() {
Expand All @@ -246,8 +254,10 @@ pub fn set_checked(target: &web_sys::EventTarget, value: bool) -> Result<(), &'s
if let Some(input) = target.dyn_ref::<web_sys::HtmlInputElement>() {
// https://www.w3schools.com/tags/att_input_checked.asp
return match input.type_().as_str() {
"file" => Err(r#"The checked attribute can be used with <input type="checkbox"> and <input type="radio">."#),
_ => Ok(input.set_checked(value))
"file" => Err(
r#"The checked attribute can be used with <input type="checkbox"> and <input type="radio">."#,
),
_ => Ok(input.set_checked(value)),
};
}
if let Some(input) = target.dyn_ref::<web_sys::HtmlMenuItemElement>() {
Expand Down