aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnthony Ramine <nox@nox.paris>2020-03-31 14:58:56 +0200
committerAnthony Ramine <nox@nox.paris>2020-03-31 15:02:13 +0200
commit6fe294fa5bab1b0ccd80d6dfb53be85adfaec4ad (patch)
tree6a177fee39ac5ba1c3c2de58e3d11657b2044d2e
parent409bd3d989c1877f8e0a5da92bd758dd20ac724b (diff)
downloadservo-6fe294fa5bab1b0ccd80d6dfb53be85adfaec4ad.tar.gz
servo-6fe294fa5bab1b0ccd80d6dfb53be85adfaec4ad.zip
Make LayoutNodeHelpers::text_content return a cow
-rw-r--r--components/layout/wrapper.rs2
-rw-r--r--components/layout_2020/dom_traversal.rs14
-rw-r--r--components/layout_2020/flow/construct.rs8
-rw-r--r--components/layout_thread/dom_wrapper.rs6
-rw-r--r--components/layout_thread_2020/dom_wrapper.rs6
-rw-r--r--components/script/dom/node.rs12
-rw-r--r--components/script_layout_interface/wrapper_traits.rs3
7 files changed, 32 insertions, 19 deletions
diff --git a/components/layout/wrapper.rs b/components/layout/wrapper.rs
index af8d6b6a0c7..60eace1f16a 100644
--- a/components/layout/wrapper.rs
+++ b/components/layout/wrapper.rs
@@ -130,7 +130,7 @@ where
});
}
- TextContent::Text(self.node_text_content().into_boxed_str())
+ TextContent::Text(self.node_text_content().into_owned().into_boxed_str())
}
fn restyle_damage(self) -> RestyleDamage {
diff --git a/components/layout_2020/dom_traversal.rs b/components/layout_2020/dom_traversal.rs
index 020282f50ff..ef4570a85bb 100644
--- a/components/layout_2020/dom_traversal.rs
+++ b/components/layout_2020/dom_traversal.rs
@@ -17,6 +17,7 @@ use script_layout_interface::wrapper_traits::{
};
use script_layout_interface::HTMLCanvasDataSource;
use servo_arc::Arc as ServoArc;
+use std::borrow::Cow;
use std::marker::PhantomData as marker;
use std::sync::{Arc, Mutex};
use style::dom::{OpaqueNode, TNode};
@@ -59,7 +60,12 @@ pub(super) trait TraversalHandler<'dom, Node>
where
Node: 'dom,
{
- fn handle_text(&mut self, node: Node, text: String, parent_style: &ServoArc<ComputedValues>);
+ fn handle_text(
+ &mut self,
+ node: Node,
+ text: Cow<'dom, str>,
+ parent_style: &ServoArc<ComputedValues>,
+ );
/// Or pseudo-element
fn handle_element(
@@ -166,7 +172,7 @@ fn traverse_pseudo_element_contents<'dom, Node>(
for item in items {
match item {
PseudoElementContentItem::Text(text) => {
- handler.handle_text(node, text, pseudo_element_style)
+ handler.handle_text(node, text.into(), pseudo_element_style)
},
PseudoElementContentItem::Replaced(contents) => {
let item_style = anonymous_style.get_or_insert_with(|| {
@@ -351,7 +357,7 @@ impl Drop for BoxSlot<'_> {
pub(crate) trait NodeExt<'dom>: 'dom + Copy + LayoutNode<'dom> + Send + Sync {
fn is_element(self) -> bool;
- fn as_text(self) -> Option<String>;
+ fn as_text(self) -> Option<Cow<'dom, str>>;
/// Returns the image if it’s loaded, and its size in image pixels
/// adjusted for `image_density`.
@@ -378,7 +384,7 @@ where
self.to_threadsafe().as_element().is_some()
}
- fn as_text(self) -> Option<String> {
+ fn as_text(self) -> Option<Cow<'dom, str>> {
if self.is_text_node() {
Some(self.to_threadsafe().node_text_content())
} else {
diff --git a/components/layout_2020/flow/construct.rs b/components/layout_2020/flow/construct.rs
index fe9465f775e..186a4b4d4ff 100644
--- a/components/layout_2020/flow/construct.rs
+++ b/components/layout_2020/flow/construct.rs
@@ -16,6 +16,7 @@ use crate::style_ext::{ComputedValuesExt, DisplayGeneratingBox, DisplayInside, D
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use rayon_croissant::ParallelIteratorExt;
use servo_arc::Arc;
+use std::borrow::Cow;
use std::convert::{TryFrom, TryInto};
use style::properties::ComputedValues;
use style::selector_parser::PseudoElement;
@@ -286,7 +287,12 @@ where
}
}
- fn handle_text(&mut self, node: Node, input: String, parent_style: &Arc<ComputedValues>) {
+ fn handle_text(
+ &mut self,
+ node: Node,
+ input: Cow<'dom, str>,
+ parent_style: &Arc<ComputedValues>,
+ ) {
let (leading_whitespace, mut input) = self.handle_leading_whitespace(&input);
if leading_whitespace || !input.is_empty() {
// This text node should be pushed either to the next ongoing
diff --git a/components/layout_thread/dom_wrapper.rs b/components/layout_thread/dom_wrapper.rs
index a0eb66a7fd8..e3197c8e78a 100644
--- a/components/layout_thread/dom_wrapper.rs
+++ b/components/layout_thread/dom_wrapper.rs
@@ -67,6 +67,7 @@ use selectors::sink::Push;
use servo_arc::{Arc, ArcBorrow};
use servo_atoms::Atom;
use servo_url::ServoUrl;
+use std::borrow::Cow;
use std::fmt;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
@@ -1110,9 +1111,8 @@ impl<'ln> ThreadSafeLayoutNode<'ln> for ServoThreadSafeLayoutNode<'ln> {
self.node
}
- fn node_text_content(&self) -> String {
- let this = unsafe { self.get_jsmanaged() };
- return this.text_content();
+ fn node_text_content(self) -> Cow<'ln, str> {
+ unsafe { self.get_jsmanaged().text_content() }
}
fn selection(&self) -> Option<Range<ByteIndex>> {
diff --git a/components/layout_thread_2020/dom_wrapper.rs b/components/layout_thread_2020/dom_wrapper.rs
index c79aa891214..7d57d7f511e 100644
--- a/components/layout_thread_2020/dom_wrapper.rs
+++ b/components/layout_thread_2020/dom_wrapper.rs
@@ -67,6 +67,7 @@ use selectors::sink::Push;
use servo_arc::{Arc, ArcBorrow};
use servo_atoms::Atom;
use servo_url::ServoUrl;
+use std::borrow::Cow;
use std::fmt;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
@@ -1117,9 +1118,8 @@ impl<'ln> ThreadSafeLayoutNode<'ln> for ServoThreadSafeLayoutNode<'ln> {
self.node
}
- fn node_text_content(&self) -> String {
- let this = unsafe { self.get_jsmanaged() };
- return this.text_content();
+ fn node_text_content(self) -> Cow<'ln, str> {
+ unsafe { self.get_jsmanaged().text_content() }
}
fn selection(&self) -> Option<Range<ByteIndex>> {
diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs
index 07b7d60cbe6..251f6fcab18 100644
--- a/components/script/dom/node.rs
+++ b/components/script/dom/node.rs
@@ -87,7 +87,7 @@ use servo_arc::Arc;
use servo_atoms::Atom;
use servo_url::ServoUrl;
use smallvec::SmallVec;
-use std::borrow::ToOwned;
+use std::borrow::Cow;
use std::cell::{Cell, UnsafeCell};
use std::cmp;
use std::default::Default;
@@ -1326,7 +1326,7 @@ pub trait LayoutNodeHelpers<'dom> {
unsafe fn init_style_and_layout_data(self, _: OpaqueStyleAndLayoutData);
unsafe fn take_style_and_layout_data(self) -> OpaqueStyleAndLayoutData;
- fn text_content(self) -> String;
+ fn text_content(self) -> Cow<'dom, str>;
fn selection(self) -> Option<Range<usize>>;
fn image_url(self) -> Option<ServoUrl>;
fn image_density(self) -> Option<f64>;
@@ -1456,17 +1456,17 @@ impl<'dom> LayoutNodeHelpers<'dom> for LayoutDom<'dom, Node> {
val
}
- fn text_content(self) -> String {
+ fn text_content(self) -> Cow<'dom, str> {
if let Some(text) = self.downcast::<Text>() {
- return text.upcast().data_for_layout().to_owned();
+ return text.upcast().data_for_layout().into();
}
if let Some(input) = self.downcast::<HTMLInputElement>() {
- return input.value_for_layout().into_owned();
+ return input.value_for_layout();
}
if let Some(area) = self.downcast::<HTMLTextAreaElement>() {
- return area.value_for_layout();
+ return area.value_for_layout().into();
}
panic!("not text!")
diff --git a/components/script_layout_interface/wrapper_traits.rs b/components/script_layout_interface/wrapper_traits.rs
index 67b107a86d7..462d11f2c7e 100644
--- a/components/script_layout_interface/wrapper_traits.rs
+++ b/components/script_layout_interface/wrapper_traits.rs
@@ -17,6 +17,7 @@ use net_traits::image::base::{Image, ImageMetadata};
use range::Range;
use servo_arc::Arc;
use servo_url::ServoUrl;
+use std::borrow::Cow;
use std::fmt::Debug;
use std::sync::Arc as StdArc;
use style::attr::AttrValue;
@@ -262,7 +263,7 @@ pub trait ThreadSafeLayoutNode<'dom>:
/// data flags, and we have this annoying trait separation between script and layout :-(
unsafe fn unsafe_get(self) -> Self::ConcreteNode;
- fn node_text_content(&self) -> String;
+ fn node_text_content(self) -> Cow<'dom, str>;
/// If the insertion point is within this node, returns it. Otherwise, returns `None`.
fn selection(&self) -> Option<Range<ByteIndex>>;