aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom
diff options
context:
space:
mode:
authorAnthony Ramine <nox@nox.paris>2020-03-31 22:32:35 +0200
committerAnthony Ramine <nox@nox.paris>2020-04-01 11:40:34 +0200
commitfc07a5147cd26fa3d8778b3366358f8ae5bcee36 (patch)
treea5a1c0768a3796b4ff33f1796fb1d21d7ffe414e /components/script/dom
parentf712b0bcf8ee2694170d3e92f03fb87539f81324 (diff)
downloadservo-fc07a5147cd26fa3d8778b3366358f8ae5bcee36.tar.gz
servo-fc07a5147cd26fa3d8778b3366358f8ae5bcee36.zip
Make LayoutNodeHelpers::composed_parent_node_ref be safe
For clarity, I introduce <LayoutDom<Element>>::parent_node_ref to contain the remaining unsafety bits out of composed_parent_node_ref which is more complex than just a field access.
Diffstat (limited to 'components/script/dom')
-rw-r--r--components/script/dom/element.rs41
-rw-r--r--components/script/dom/node.rs17
2 files changed, 30 insertions, 28 deletions
diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs
index 8bbdbf6e7dd..b59b342024f 100644
--- a/components/script/dom/element.rs
+++ b/components/script/dom/element.rs
@@ -985,32 +985,27 @@ impl<'dom> LayoutElementHelpers<'dom> for LayoutDom<'dom, Element> {
unsafe { &(*self.unsafe_get()).namespace }
}
- #[allow(unsafe_code)]
fn get_lang_for_layout(self) -> String {
- unsafe {
- let mut current_node = Some(self.upcast::<Node>());
- while let Some(node) = current_node {
- current_node = node.composed_parent_node_ref();
- match node.downcast::<Element>() {
- Some(elem) => {
- if let Some(attr) =
- elem.get_attr_val_for_layout(&ns!(xml), &local_name!("lang"))
- {
- return attr.to_owned();
- }
- if let Some(attr) =
- elem.get_attr_val_for_layout(&ns!(), &local_name!("lang"))
- {
- return attr.to_owned();
- }
- },
- None => continue,
- }
+ let mut current_node = Some(self.upcast::<Node>());
+ while let Some(node) = current_node {
+ current_node = node.composed_parent_node_ref();
+ match node.downcast::<Element>() {
+ Some(elem) => {
+ if let Some(attr) =
+ elem.get_attr_val_for_layout(&ns!(xml), &local_name!("lang"))
+ {
+ return attr.to_owned();
+ }
+ if let Some(attr) = elem.get_attr_val_for_layout(&ns!(), &local_name!("lang")) {
+ return attr.to_owned();
+ }
+ },
+ None => continue,
}
- // TODO: Check meta tags for a pragma-set default language
- // TODO: Check HTTP Content-Language header
- String::new()
}
+ // TODO: Check meta tags for a pragma-set default language
+ // TODO: Check HTTP Content-Language header
+ String::new()
}
#[inline]
diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs
index a42f2371d3d..7348431d4f3 100644
--- a/components/script/dom/node.rs
+++ b/components/script/dom/node.rs
@@ -1307,7 +1307,7 @@ pub unsafe fn from_untrusted_node_address(
pub trait LayoutNodeHelpers<'dom> {
fn type_id_for_layout(self) -> NodeTypeId;
- unsafe fn composed_parent_node_ref(self) -> Option<LayoutDom<'dom, Node>>;
+ fn composed_parent_node_ref(self) -> Option<LayoutDom<'dom, Node>>;
fn first_child_ref(self) -> Option<LayoutDom<'dom, Node>>;
fn last_child_ref(self) -> Option<LayoutDom<'dom, Node>>;
fn prev_sibling_ref(self) -> Option<LayoutDom<'dom, Node>>;
@@ -1339,6 +1339,14 @@ pub trait LayoutNodeHelpers<'dom> {
fn opaque(self) -> OpaqueNode;
}
+impl<'dom> LayoutDom<'dom, Node> {
+ #[inline]
+ #[allow(unsafe_code)]
+ fn parent_node_ref(self) -> Option<LayoutDom<'dom, Node>> {
+ unsafe { self.unsafe_get().parent_node.get_inner_as_layout() }
+ }
+}
+
impl<'dom> LayoutNodeHelpers<'dom> for LayoutDom<'dom, Node> {
#[inline]
#[allow(unsafe_code)]
@@ -1352,10 +1360,9 @@ impl<'dom> LayoutNodeHelpers<'dom> for LayoutDom<'dom, Node> {
}
#[inline]
- #[allow(unsafe_code)]
- unsafe fn composed_parent_node_ref(self) -> Option<LayoutDom<'dom, Node>> {
- let parent = (*self.unsafe_get()).parent_node.get_inner_as_layout();
- if let Some(ref parent) = parent {
+ fn composed_parent_node_ref(self) -> Option<LayoutDom<'dom, Node>> {
+ let parent = self.parent_node_ref();
+ if let Some(parent) = parent {
if let Some(shadow_root) = parent.downcast::<ShadowRoot>() {
return Some(shadow_root.get_host_for_layout().upcast());
}