diff options
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/dom/activation.rs | 95 | ||||
-rw-r--r-- | components/script/dom/blob.rs | 4 | ||||
-rw-r--r-- | components/script/dom/document.rs | 10 | ||||
-rw-r--r-- | components/script/dom/element.rs | 10 | ||||
-rw-r--r-- | components/script/dom/htmlbuttonelement.rs | 9 | ||||
-rw-r--r-- | components/script/dom/htmlelement.rs | 17 | ||||
-rw-r--r-- | components/script/dom/htmlinputelement.rs | 103 | ||||
-rw-r--r-- | components/script/dom/htmllabelelement.rs | 11 | ||||
-rw-r--r-- | components/script/dom/node.rs | 41 | ||||
-rw-r--r-- | components/script/dom/webidls/Element.webidl | 3 | ||||
-rw-r--r-- | components/script/dom/webidls/HTMLInputElement.webidl | 8 | ||||
-rw-r--r-- | components/script/dom/window.rs | 8 | ||||
-rw-r--r-- | components/script/layout_interface.rs | 3 | ||||
-rw-r--r-- | components/script/textinput.rs | 34 |
14 files changed, 286 insertions, 70 deletions
diff --git a/components/script/dom/activation.rs b/components/script/dom/activation.rs index 6fd1882c579..9fd94ae6690 100644 --- a/components/script/dom/activation.rs +++ b/components/script/dom/activation.rs @@ -29,55 +29,70 @@ pub trait Activatable { // https://html.spec.whatwg.org/multipage/#implicit-submission fn implicit_submission(&self, ctrlKey: bool, shiftKey: bool, altKey: bool, metaKey: bool); +} + +/// Whether an activation was initiated via the click() method +#[derive(PartialEq)] +pub enum ActivationSource { + FromClick, + NotFromClick, +} - // https://html.spec.whatwg.org/multipage/#run-synthetic-click-activation-steps - fn synthetic_click_activation(&self, +// https://html.spec.whatwg.org/multipage/#run-synthetic-click-activation-steps +pub fn synthetic_click_activation(element: &Element, ctrlKey: bool, shiftKey: bool, altKey: bool, - metaKey: bool) { - let element = self.as_element(); - // Step 1 - if element.click_in_progress() { - return; - } - // Step 2 - element.set_click_in_progress(true); - // Step 3 - self.pre_click_activation(); + metaKey: bool, + source: ActivationSource) { + // Step 1 + if element.click_in_progress() { + return; + } + // Step 2 + element.set_click_in_progress(true); + // Step 3 + let activatable = element.as_maybe_activatable(); + if let Some(a) = activatable { + a.pre_click_activation(); + } - // Step 4 - // https://html.spec.whatwg.org/multipage/#fire-a-synthetic-mouse-event - let win = window_from_node(element); - let target = element.upcast(); - let mouse = MouseEvent::new(win.r(), - DOMString::from("click"), - EventBubbles::DoesNotBubble, - EventCancelable::NotCancelable, - Some(win.r()), - 1, - 0, - 0, - 0, - 0, - ctrlKey, - shiftKey, - altKey, - metaKey, - 0, - None); - let event = mouse.upcast::<Event>(); - event.fire(target); + // Step 4 + // https://html.spec.whatwg.org/multipage/#fire-a-synthetic-mouse-event + let win = window_from_node(element); + let target = element.upcast::<EventTarget>(); + let mouse = MouseEvent::new(win.r(), + DOMString::from("click"), + EventBubbles::DoesNotBubble, + EventCancelable::NotCancelable, + Some(win.r()), + 1, + 0, + 0, + 0, + 0, + ctrlKey, + shiftKey, + altKey, + metaKey, + 0, + None); + let event = mouse.upcast::<Event>(); + if source == ActivationSource::FromClick { + event.set_trusted(false); + } + target.dispatch_event(event); - // Step 5 + // Step 5 + if let Some(a) = activatable { if event.DefaultPrevented() { - self.canceled_activation(); + a.canceled_activation(); } else { // post click activation - self.activation_behavior(event, target); + a.activation_behavior(event, target); } - - // Step 6 - element.set_click_in_progress(false); } + + // Step 6 + element.set_click_in_progress(false); } diff --git a/components/script/dom/blob.rs b/components/script/dom/blob.rs index 7e8f350b837..dc24cbb01fc 100644 --- a/components/script/dom/blob.rs +++ b/components/script/dom/blob.rs @@ -124,14 +124,12 @@ impl Blob { blobPropertyBag: &BlobBinding::BlobPropertyBag) -> Fallible<Root<Blob>> { // TODO: accept other blobParts types - ArrayBuffer or ArrayBufferView or Blob - // FIXME(ajeffrey): convert directly from a DOMString to a Vec<u8> - let bytes: Vec<u8> = String::from(blobParts).into_bytes(); let typeString = if is_ascii_printable(&blobPropertyBag.type_) { &*blobPropertyBag.type_ } else { "" }; - Ok(Blob::new(global, bytes, &typeString.to_ascii_lowercase())) + Ok(Blob::new(global, blobParts.into(), &typeString.to_ascii_lowercase())) } pub fn get_data(&self) -> &DataSlice { diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index e0f4c0c5592..db565414ce1 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -4,6 +4,7 @@ use devtools_traits::CSSError; use document_loader::{DocumentLoader, LoadType}; +use dom::activation::{ActivationSource, synthetic_click_activation}; use dom::attr::{Attr, AttrValue}; use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::DOMRectBinding::DOMRectMethods; @@ -1083,9 +1084,12 @@ impl Document { Key::Space if !prevented && state == KeyState::Released => { let maybe_elem = target.downcast::<Element>(); if let Some(el) = maybe_elem { - if let Some(a) = el.as_maybe_activatable() { - a.synthetic_click_activation(ctrl, alt, shift, meta); - } + synthetic_click_activation(el, + false, + false, + false, + false, + ActivationSource::NotFromClick) } } Key::Enter if !prevented && state == KeyState::Released => { diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 3eec2575bc3..25d9f0d45b8 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -1421,6 +1421,16 @@ impl ElementMethods for Element { rect.size.height.to_f64_px()) } + // https://drafts.csswg.org/cssom-view/#dom-element-scrollwidth + fn ScrollWidth(&self) -> i32 { + self.upcast::<Node>().get_scroll_area().size.width + } + + // https://drafts.csswg.org/cssom-view/#dom-element-scrollheight + fn ScrollHeight(&self) -> i32 { + self.upcast::<Node>().get_scroll_area().size.height + } + // https://drafts.csswg.org/cssom-view/#dom-element-clienttop fn ClientTop(&self) -> i32 { self.upcast::<Node>().get_client_rect().origin.y diff --git a/components/script/dom/htmlbuttonelement.rs b/components/script/dom/htmlbuttonelement.rs index 31d4b0a8740..661fb3d6f68 100644 --- a/components/script/dom/htmlbuttonelement.rs +++ b/components/script/dom/htmlbuttonelement.rs @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use dom::activation::Activatable; +use dom::activation::{Activatable, ActivationSource, synthetic_click_activation}; use dom::attr::Attr; use dom::bindings::codegen::Bindings::HTMLButtonElementBinding; use dom::bindings::codegen::Bindings::HTMLButtonElementBinding::HTMLButtonElementMethods; @@ -256,6 +256,11 @@ impl Activatable for HTMLButtonElement { node.query_selector_iter(DOMString::from("button[type=submit]")).unwrap() .filter_map(Root::downcast::<HTMLButtonElement>) .find(|r| r.form_owner() == owner) - .map(|s| s.r().synthetic_click_activation(ctrlKey, shiftKey, altKey, metaKey)); + .map(|s| synthetic_click_activation(s.r().as_element(), + ctrlKey, + shiftKey, + altKey, + metaKey, + ActivationSource::NotFromClick)); } } diff --git a/components/script/dom/htmlelement.rs b/components/script/dom/htmlelement.rs index 7c37c3ca9e2..aa2ee98ad1f 100644 --- a/components/script/dom/htmlelement.rs +++ b/components/script/dom/htmlelement.rs @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use dom::activation::{ActivationSource, synthetic_click_activation}; use dom::attr::Attr; use dom::attr::AttrValue; use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods; @@ -9,7 +10,6 @@ use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull; use dom::bindings::codegen::Bindings::EventHandlerBinding::OnErrorEventHandlerNonNull; use dom::bindings::codegen::Bindings::HTMLElementBinding; use dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods; -use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods; use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; use dom::bindings::error::{Error, ErrorResult}; use dom::bindings::inheritance::Castable; @@ -200,15 +200,14 @@ impl HTMLElementMethods for HTMLElement { // https://html.spec.whatwg.org/multipage/#dom-click fn Click(&self) { - if let Some(i) = self.downcast::<HTMLInputElement>() { - if i.Disabled() { - return; - } + if !self.upcast::<Element>().get_disabled_state() { + synthetic_click_activation(self.upcast::<Element>(), + false, + false, + false, + false, + ActivationSource::FromClick) } - // https://www.w3.org/Bugs/Public/show_bug.cgi?id=27430 ? - self.upcast::<Element>() - .as_maybe_activatable() - .map(|a| a.synthetic_click_activation(false, false, false, false)); } // https://html.spec.whatwg.org/multipage/#dom-focus diff --git a/components/script/dom/htmlinputelement.rs b/components/script/dom/htmlinputelement.rs index bda81450d3d..03c3bf83d0c 100644 --- a/components/script/dom/htmlinputelement.rs +++ b/components/script/dom/htmlinputelement.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use caseless::compatibility_caseless_match_str; -use dom::activation::Activatable; +use dom::activation::{Activatable, ActivationSource, synthetic_click_activation}; use dom::attr::{Attr, AttrValue}; use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods; @@ -68,6 +68,14 @@ enum ValueMode { Filename, } +#[derive(JSTraceable, PartialEq, Copy, Clone)] +#[derive(HeapSizeOf)] +enum SelectionDirection { + Forward, + Backward, + None +} + #[dom_struct] pub struct HTMLInputElement { htmlelement: HTMLElement, @@ -83,6 +91,8 @@ pub struct HTMLInputElement { // https://html.spec.whatwg.org/multipage/#concept-input-value-dirty-flag value_dirty: Cell<bool>, + selection_direction: Cell<SelectionDirection>, + // TODO: selected files for file input } @@ -132,6 +142,7 @@ impl HTMLInputElement { textinput: DOMRefCell::new(TextInput::new(Single, DOMString::new(), chan, None)), activation_state: DOMRefCell::new(InputActivationState::new()), value_dirty: Cell::new(false), + selection_direction: Cell::new(SelectionDirection::None) } } @@ -164,6 +175,31 @@ impl HTMLInputElement { } } + // this method exists so that the functions SetSelectionStart() and SetSelectionEnd() + // don't needlessly allocate strings + fn set_selection_range(&self, start: u32, end: u32, direction: &SelectionDirection) { + let mut text_input = self.textinput.borrow_mut(); + + let mut start = start as usize; + let mut end = end as usize; + + let text_end = text_input.get_content().len(); + if start > text_end { + start = text_end; + } + if end > text_end { + end = text_end; + } + + if start >= end { + start = end; + } + + text_input.selection_begin = Some(text_input.get_text_point_for_absolute_point(start)); + text_input.edit_point = text_input.get_text_point_for_absolute_point(end); + self.selection_direction.set(*direction); + } + } pub trait LayoutHTMLInputElementHelpers { @@ -444,6 +480,64 @@ impl HTMLInputElementMethods for HTMLInputElement { self.upcast::<HTMLElement>().labels() } } + + // https://html.spec.whatwg.org/multipage/#dom-input-selectionstart + fn SelectionStart(&self) -> u32 { + let text_input = self.textinput.borrow(); + let selection_start = match text_input.selection_begin { + Some(selection_begin_point) => { + text_input.get_absolute_point_for_text_point(&selection_begin_point) + }, + None => text_input.get_absolute_insertion_point() + }; + + selection_start as u32 + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionstart + fn SetSelectionStart(&self, start: u32) { + self.set_selection_range(start, self.SelectionEnd(), &self.selection_direction.get()); + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend + fn SelectionEnd(&self) -> u32 { + let text_input = self.textinput.borrow(); + text_input.get_absolute_insertion_point() as u32 + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectionend + fn SetSelectionEnd(&self, end: u32) { + self.set_selection_range(self.SelectionStart(), end, &self.selection_direction.get()); + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection + fn SelectionDirection(&self) -> DOMString { + match self.selection_direction.get() { + SelectionDirection::Forward => DOMString::from("forward"), + SelectionDirection::Backward => DOMString::from("backward"), + SelectionDirection::None => DOMString::from("none"), + } + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-selectiondirection + fn SetSelectionDirection(&self, direction: DOMString) { + self.SetSelectionRange(self.SelectionStart(), self.SelectionEnd(), Some(direction)); + } + + // https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange + fn SetSelectionRange(&self, start: u32, end: u32, direction: Option<DOMString>) { + let selection_direction = match direction { + Some(selection_direction) => { + match &*selection_direction { + "forward" => SelectionDirection::Forward, + "backward" => SelectionDirection::Backward, + _ => SelectionDirection::None, + } + }, + None => SelectionDirection::None, + }; + self.set_selection_range(start, end, &selection_direction); + } } @@ -997,7 +1091,12 @@ impl Activatable for HTMLInputElement { match submit_button { Some(ref button) => { if button.is_instance_activatable() { - button.synthetic_click_activation(ctrlKey, shiftKey, altKey, metaKey) + synthetic_click_activation(button.as_element(), + ctrlKey, + shiftKey, + altKey, + metaKey, + ActivationSource::NotFromClick) } } None => { diff --git a/components/script/dom/htmllabelelement.rs b/components/script/dom/htmllabelelement.rs index 01e05761cf2..7042b98f0d7 100644 --- a/components/script/dom/htmllabelelement.rs +++ b/components/script/dom/htmllabelelement.rs @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use dom::activation::Activatable; +use dom::activation::{Activatable, ActivationSource, synthetic_click_activation}; use dom::attr::AttrValue; use dom::bindings::codegen::Bindings::HTMLLabelElementBinding; use dom::bindings::codegen::Bindings::HTMLLabelElementBinding::HTMLLabelElementMethods; @@ -63,9 +63,12 @@ impl Activatable for HTMLLabelElement { // https://html.spec.whatwg.org/multipage/#run-post-click-activation-steps fn activation_behavior(&self, _event: &Event, _target: &EventTarget) { - self.upcast::<Element>() - .as_maybe_activatable() - .map(|a| a.synthetic_click_activation(false, false, false, false)); + synthetic_click_activation(self.upcast::<Element>(), + false, + false, + false, + false, + ActivationSource::NotFromClick); } // https://html.spec.whatwg.org/multipage/#implicit-submission diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 717be0331d5..fed47bb7b24 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -18,6 +18,7 @@ use dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods; use dom::bindings::codegen::Bindings::NodeBinding::{NodeConstants, NodeMethods}; use dom::bindings::codegen::Bindings::NodeListBinding::NodeListMethods; use dom::bindings::codegen::Bindings::ProcessingInstructionBinding::ProcessingInstructionMethods; +use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; use dom::bindings::codegen::UnionTypes::NodeOrString; use dom::bindings::conversions::{self, DerivedFrom}; use dom::bindings::error::{Error, ErrorResult, Fallible}; @@ -37,6 +38,7 @@ use dom::documentfragment::DocumentFragment; use dom::documenttype::DocumentType; use dom::element::{Element, ElementCreator}; use dom::eventtarget::EventTarget; +use dom::htmlbodyelement::HTMLBodyElement; use dom::htmlcollection::HTMLCollection; use dom::htmlelement::HTMLElement; use dom::nodelist::NodeList; @@ -45,8 +47,11 @@ use dom::range::WeakRangeVec; use dom::text::Text; use dom::virtualmethods::{VirtualMethods, vtable_for}; use dom::window::Window; +use euclid::point::Point2D; use euclid::rect::Rect; +use euclid::size::Size2D; use heapsize::{HeapSizeOf, heap_size_of}; +use html5ever::tree_builder::QuirksMode; use js::jsapi::{JSContext, JSObject, JSRuntime}; use layout_interface::{LayoutChan, Msg}; use libc::{self, c_void, uintptr_t}; @@ -577,6 +582,42 @@ impl Node { window_from_node(self).client_rect_query(self.to_trusted_node_address()) } + // https://drafts.csswg.org/cssom-view/#dom-element-scrollwidth + // https://drafts.csswg.org/cssom-view/#dom-element-scrollheight + // https://drafts.csswg.org/cssom-view/#dom-element-scrolltop + // https://drafts.csswg.org/cssom-view/#dom-element-scrollleft + pub fn get_scroll_area(&self) -> Rect<i32> { + // Step 1 + let document = self.owner_doc(); + // Step 3 + let window = document.window(); + + let html_element = document.GetDocumentElement(); + + let is_body_element = html_element.r().and_then(|root| { + let node = root.upcast::<Node>(); + node.children().find(|child| { child.is::<HTMLBodyElement>() }).map(|node| { + *node.r() == *self + }) + }).unwrap_or(false); + + let scroll_area = window.scroll_area_query(self.to_trusted_node_address()); + + match (document != window.Document(), is_body_element, document.quirks_mode(), + html_element.r() == self.downcast::<Element>()) { + // Step 2 && Step 5 + (true, _, _, _) | (_, false, QuirksMode::Quirks, true) => Rect::zero(), + // Step 6 && Step 7 + (false, false, _, true) | (false, true, QuirksMode::Quirks, _) => { + Rect::new(Point2D::new(window.ScrollX(), window.ScrollY()), + Size2D::new(max(window.InnerWidth(), scroll_area.size.width), + max(window.InnerHeight(), scroll_area.size.height))) + }, + // Step 9 + _ => scroll_area + } + } + // https://dom.spec.whatwg.org/#dom-childnode-before pub fn before(&self, nodes: Vec<NodeOrString>) -> ErrorResult { // Step 1. diff --git a/components/script/dom/webidls/Element.webidl b/components/script/dom/webidls/Element.webidl index 900c2eb24dc..c5c395536ac 100644 --- a/components/script/dom/webidls/Element.webidl +++ b/components/script/dom/webidls/Element.webidl @@ -77,6 +77,9 @@ partial interface Element { DOMRectList getClientRects(); DOMRect getBoundingClientRect(); + readonly attribute long scrollWidth; + readonly attribute long scrollHeight; + readonly attribute long clientTop; readonly attribute long clientLeft; readonly attribute long clientWidth; diff --git a/components/script/dom/webidls/HTMLInputElement.webidl b/components/script/dom/webidls/HTMLInputElement.webidl index d213334ef83..2e1c6215f2b 100644 --- a/components/script/dom/webidls/HTMLInputElement.webidl +++ b/components/script/dom/webidls/HTMLInputElement.webidl @@ -62,13 +62,13 @@ interface HTMLInputElement : HTMLElement { readonly attribute NodeList labels; //void select(); - // attribute unsigned long selectionStart; - // attribute unsigned long selectionEnd; - // attribute DOMString selectionDirection; + attribute unsigned long selectionStart; + attribute unsigned long selectionEnd; + attribute DOMString selectionDirection; //void setRangeText(DOMString replacement); //void setRangeText(DOMString replacement, unsigned long start, unsigned long end, // optional SelectionMode selectionMode = "preserve"); - //void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction); + void setSelectionRange(unsigned long start, unsigned long end, optional DOMString direction); // also has obsolete members }; diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index eeb62d833c6..df7d1f92de7 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -1110,6 +1110,13 @@ impl Window { self.layout_rpc.hit_test().node_address } + pub fn scroll_area_query(&self, node: TrustedNodeAddress) -> Rect<i32> { + self.reflow(ReflowGoal::ForScriptQuery, + ReflowQueryType::NodeScrollGeometryQuery(node), + ReflowReason::Query); + self.layout_rpc.node_scroll_area().client_rect + } + pub fn resolved_style_query(&self, element: TrustedNodeAddress, pseudo: Option<PseudoElement>, @@ -1463,6 +1470,7 @@ fn debug_reflow_events(id: PipelineId, goal: &ReflowGoal, query_type: &ReflowQue ReflowQueryType::ContentBoxesQuery(_n) => "\tContentBoxesQuery", ReflowQueryType::HitTestQuery(_n, _o) => "\tHitTestQuery", ReflowQueryType::NodeGeometryQuery(_n) => "\tNodeGeometryQuery", + ReflowQueryType::NodeScrollGeometryQuery(_n) => "\tNodeScrollGeometryQuery", ReflowQueryType::ResolvedStyleQuery(_, _, _) => "\tResolvedStyleQuery", ReflowQueryType::OffsetParentQuery(_n) => "\tOffsetParentQuery", ReflowQueryType::MarginStyleQuery(_n) => "\tMarginStyleQuery", diff --git a/components/script/layout_interface.rs b/components/script/layout_interface.rs index 9a8cddf97e1..0d063f47bc1 100644 --- a/components/script/layout_interface.rs +++ b/components/script/layout_interface.rs @@ -104,6 +104,8 @@ pub trait LayoutRPC { fn content_boxes(&self) -> ContentBoxesResponse; /// Requests the geometry of this node. Used by APIs such as `clientTop`. fn node_geometry(&self) -> NodeGeometryResponse; + /// Requests the scroll geometry of this node. Used by APIs such as `scrollTop`. + fn node_scroll_area(&self) -> NodeGeometryResponse; /// Requests the node containing the point of interest fn hit_test(&self) -> HitTestResponse; /// Query layout for the resolved value of a given CSS property @@ -165,6 +167,7 @@ pub enum ReflowQueryType { ContentBoxesQuery(TrustedNodeAddress), HitTestQuery(Point2D<f32>, bool), NodeGeometryQuery(TrustedNodeAddress), + NodeScrollGeometryQuery(TrustedNodeAddress), ResolvedStyleQuery(TrustedNodeAddress, Option<PseudoElement>, Atom), OffsetParentQuery(TrustedNodeAddress), MarginStyleQuery(TrustedNodeAddress), diff --git a/components/script/textinput.rs b/components/script/textinput.rs index 02fb79e04a8..f407592a0ef 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -36,7 +36,7 @@ pub struct TextInput<T: ClipboardProvider> { /// Current cursor input point pub edit_point: TextPoint, /// Beginning of selection range with edit_point as end that can span multiple lines. - selection_begin: Option<TextPoint>, + pub selection_begin: Option<TextPoint>, /// Is this a multiline input? multiline: bool, #[ignore_heap_size_of = "Can't easily measure this generic type"] @@ -500,12 +500,40 @@ impl<T: ClipboardProvider> TextInput<T> { } pub fn get_absolute_insertion_point(&self) -> usize { + self.get_absolute_point_for_text_point(&self.edit_point) + } + + pub fn get_absolute_point_for_text_point(&self, text_point: &TextPoint) -> usize { self.lines.iter().enumerate().fold(0, |acc, (i, val)| { - if i < self.edit_point.line { + if i < text_point.line { acc + val.len() + 1 // +1 for the \n } else { acc } - }) + self.edit_point.index + }) + text_point.index + } + + pub fn get_text_point_for_absolute_point(&self, abs_point: usize) -> TextPoint { + let mut index = abs_point; + let mut line = 0; + + let last_line_idx = self.lines.len() - 1; + self.lines.iter().enumerate().fold(0, |acc, (i, val)| { + if i != last_line_idx { + let line_end = max(val.len(), 1); + let new_acc = acc + line_end; + if abs_point > new_acc && index > line_end { + index -= line_end + 1; + line += 1; + } + new_acc + } else { + acc + } + }); + + TextPoint { + line: line, index: index + } } } |