diff options
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/Cargo.toml | 1 | ||||
-rw-r--r-- | components/script/dom/htmlinputelement.rs | 41 | ||||
-rw-r--r-- | components/script/dom/htmltextareaelement.rs | 15 | ||||
-rw-r--r-- | components/script/lib.rs | 1 | ||||
-rw-r--r-- | components/script/textinput.rs | 17 |
5 files changed, 36 insertions, 39 deletions
diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index 9e9cb5ec462..491fb077c9c 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -49,7 +49,6 @@ offscreen_gl_context = "0.1.2" rand = "0.3" phf = "0.7.13" phf_macros = "0.7.13" -range = { path = "../range" } ref_filter_map = "1.0" ref_slice = "0.1.0" regex = "0.1.43" diff --git a/components/script/dom/htmlinputelement.rs b/components/script/dom/htmlinputelement.rs index 1b828c10e27..74845a1afb9 100644 --- a/components/script/dom/htmlinputelement.rs +++ b/components/script/dom/htmlinputelement.rs @@ -31,22 +31,23 @@ use dom::nodelist::NodeList; use dom::validation::Validatable; use dom::virtualmethods::VirtualMethods; use msg::constellation_msg::ConstellationChan; -use range::Range; use script_runtime::CommonScriptMsg; use script_runtime::ScriptThreadEventCategory::InputEvent; use script_thread::Runnable; use script_traits::ScriptMsg as ConstellationMsg; use std::borrow::ToOwned; use std::cell::Cell; +use std::ops::Range; use string_cache::Atom; use style::element_state::*; use textinput::KeyReaction::{DispatchInput, Nothing, RedrawSelection, TriggerDefaultAction}; use textinput::Lines::Single; use textinput::{TextInput, SelectionDirection}; -use util::str::{DOMString, search_index}; +use util::str::{DOMString}; const DEFAULT_SUBMIT_VALUE: &'static str = "Submit"; const DEFAULT_RESET_VALUE: &'static str = "Reset"; +const PASSWORD_REPLACEMENT_CHAR: char = '●'; #[derive(JSTraceable, PartialEq, Copy, Clone)] #[allow(dead_code)] @@ -174,7 +175,7 @@ pub trait LayoutHTMLInputElementHelpers { #[allow(unsafe_code)] unsafe fn size_for_layout(self) -> u32; #[allow(unsafe_code)] - unsafe fn selection_for_layout(self) -> Option<Range<isize>>; + unsafe fn selection_for_layout(self) -> Option<Range<usize>>; #[allow(unsafe_code)] unsafe fn checked_state_for_layout(self) -> bool; #[allow(unsafe_code)] @@ -207,8 +208,7 @@ impl LayoutHTMLInputElementHelpers for LayoutJS<HTMLInputElement> { InputType::InputPassword => { let text = get_raw_textinput_value(self); if !text.is_empty() { - // The implementation of selection_for_layout expects a 1:1 mapping of chars. - text.chars().map(|_| '●').collect() + text.chars().map(|_| PASSWORD_REPLACEMENT_CHAR).collect() } else { String::from((*self.unsafe_get()).placeholder.borrow_for_layout().clone()) } @@ -216,7 +216,6 @@ impl LayoutHTMLInputElementHelpers for LayoutJS<HTMLInputElement> { _ => { let text = get_raw_textinput_value(self); if !text.is_empty() { - // The implementation of selection_for_layout expects a 1:1 mapping of chars. String::from(text) } else { String::from((*self.unsafe_get()).placeholder.borrow_for_layout().clone()) @@ -233,24 +232,28 @@ impl LayoutHTMLInputElementHelpers for LayoutJS<HTMLInputElement> { #[allow(unrooted_must_root)] #[allow(unsafe_code)] - unsafe fn selection_for_layout(self) -> Option<Range<isize>> { + unsafe fn selection_for_layout(self) -> Option<Range<usize>> { if !(*self.unsafe_get()).upcast::<Element>().focus_state() { return None; } - // Use the raw textinput to get the index as long as we use a 1:1 char mapping - // in value_for_layout. - let raw = match (*self.unsafe_get()).input_type.get() { - InputType::InputText | - InputType::InputPassword => get_raw_textinput_value(self), - _ => return None - }; let textinput = (*self.unsafe_get()).textinput.borrow_for_layout(); - let selection = textinput.get_absolute_selection_range(); - let begin_byte = selection.begin(); - let begin = search_index(begin_byte, raw.char_indices()); - let length = search_index(selection.length(), raw[begin_byte..].char_indices()); - Some(Range::new(begin, length)) + + match (*self.unsafe_get()).input_type.get() { + InputType::InputPassword => { + let text = get_raw_textinput_value(self); + let sel = textinput.get_absolute_selection_range(); + + // Translate indices from the raw value to indices in the replacement value. + let char_start = text[.. sel.start].chars().count(); + let char_end = char_start + text[sel].chars().count(); + + let bytes_per_char = PASSWORD_REPLACEMENT_CHAR.len_utf8(); + Some(char_start * bytes_per_char .. char_end * bytes_per_char) + } + InputType::InputText => Some(textinput.get_absolute_selection_range()), + _ => None + } } #[allow(unrooted_must_root)] diff --git a/components/script/dom/htmltextareaelement.rs b/components/script/dom/htmltextareaelement.rs index d3eb7827031..0f764f12e7d 100644 --- a/components/script/dom/htmltextareaelement.rs +++ b/components/script/dom/htmltextareaelement.rs @@ -26,9 +26,9 @@ use dom::nodelist::NodeList; use dom::validation::Validatable; use dom::virtualmethods::VirtualMethods; use msg::constellation_msg::ConstellationChan; -use range::Range; use script_traits::ScriptMsg as ConstellationMsg; use std::cell::Cell; +use std::ops::Range; use string_cache::Atom; use style::element_state::*; use textinput::{KeyReaction, Lines, TextInput, SelectionDirection}; @@ -47,7 +47,7 @@ pub trait LayoutHTMLTextAreaElementHelpers { #[allow(unsafe_code)] unsafe fn get_value_for_layout(self) -> String; #[allow(unsafe_code)] - unsafe fn get_absolute_selection_for_layout(self) -> Option<Range<usize>>; + unsafe fn selection_for_layout(self) -> Option<Range<usize>>; #[allow(unsafe_code)] fn get_cols(self) -> u32; #[allow(unsafe_code)] @@ -63,13 +63,12 @@ impl LayoutHTMLTextAreaElementHelpers for LayoutJS<HTMLTextAreaElement> { #[allow(unrooted_must_root)] #[allow(unsafe_code)] - unsafe fn get_absolute_selection_for_layout(self) -> Option<Range<usize>> { - if (*self.unsafe_get()).upcast::<Element>().focus_state() { - Some((*self.unsafe_get()).textinput.borrow_for_layout() - .get_absolute_selection_range()) - } else { - None + unsafe fn selection_for_layout(self) -> Option<Range<usize>> { + if !(*self.unsafe_get()).upcast::<Element>().focus_state() { + return None; } + let textinput = (*self.unsafe_get()).textinput.borrow_for_layout(); + Some(textinput.get_absolute_selection_range()) } #[allow(unsafe_code)] diff --git a/components/script/lib.rs b/components/script/lib.rs index 43d42d47a95..dde2063cfc3 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -59,7 +59,6 @@ extern crate phf; #[macro_use] extern crate profile_traits; extern crate rand; -extern crate range; extern crate ref_filter_map; extern crate ref_slice; extern crate regex; diff --git a/components/script/textinput.rs b/components/script/textinput.rs index ffd707c5d27..56e3fa98631 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -8,10 +8,10 @@ use clipboard_provider::ClipboardProvider; use dom::keyboardevent::{KeyboardEvent, key_value}; use msg::constellation_msg::{ALT, CONTROL, SHIFT, SUPER}; use msg::constellation_msg::{Key, KeyModifiers}; -use range::Range; use std::borrow::ToOwned; use std::cmp::{max, min}; use std::default::Default; +use std::ops::Range; use std::usize; use util::str::DOMString; @@ -220,10 +220,12 @@ impl<T: ClipboardProvider> TextInput<T> { /// If there is no selection, returns an empty range at the insertion point. pub fn get_absolute_selection_range(&self) -> Range<usize> { match self.get_sorted_selection() { - Some((begin, _end)) => - Range::new(self.get_absolute_point_for_text_point(&begin), self.selection_len()), - None => - Range::new(self.get_absolute_insertion_point(), 0) + Some((begin, end)) => self.get_absolute_point_for_text_point(&begin) .. + self.get_absolute_point_for_text_point(&end), + None => { + let insertion_point = self.get_absolute_insertion_point(); + insertion_point .. insertion_point + } } } @@ -235,11 +237,6 @@ impl<T: ClipboardProvider> TextInput<T> { Some(text) } - /// The length of the selected text in UTF-8 bytes. - fn selection_len(&self) -> usize { - self.fold_selection_slices(0, |len, slice| *len += slice.len()) - } - /// The length of the selected text in UTF-16 code units. fn selection_utf16_len(&self) -> usize { self.fold_selection_slices(0usize, |