style: Assert we never style a root element from another document.

Right now when calling getComputedStyle with an element from another document,
we return the style using the pres context of that document, not of the document
of the window getComputedStyle was called on, so this holds.

But it will stop holding if we ever change this, so assert it doesn't happen.

Bug: 1374062
Reviewed-By: Manishearth
MozReview-Commit-ID: 3g8yQWWdsen
This commit is contained in:
Emilio Cobos Álvarez 2017-06-21 13:01:44 +02:00
parent 1b2fd3fe85
commit 6fefe522a3
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
14 changed files with 52 additions and 30 deletions

View file

@ -14,6 +14,7 @@ use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
use data::ElementData;
use element_state::ElementState;
use font_metrics::FontMetricsProvider;
use media_queries::Device;
use properties::{ComputedValues, PropertyDeclarationBlock};
#[cfg(feature = "gecko")] use properties::animated_properties::AnimationValue;
#[cfg(feature = "gecko")] use properties::animated_properties::TransitionProperty;
@ -304,6 +305,13 @@ pub trait TElement : Eq + PartialEq + Debug + Hash + Sized + Copy + Clone +
/// Get this element as a node.
fn as_node(&self) -> Self::ConcreteNode;
/// A debug-only check that the device's owner doc matches the actual doc
/// we're the root of.
///
/// Otherwise we may set document-level state incorrectly, like the root
/// font-size used for rem units.
fn owner_doc_matches_for_testing(&self, _: &Device) -> bool { true }
/// Returns the depth of this element in the DOM.
fn depth(&self) -> usize {
let mut depth = 0;

View file

@ -46,7 +46,7 @@ impl PerDocumentStyleData {
pub fn new(pres_context: RawGeckoPresContextOwned) -> Self {
let device = Device::new(pres_context);
let quirks_mode = unsafe {
(*(*device.pres_context).mDocument.raw::<nsIDocument>()).mCompatMode
(*device.pres_context().mDocument.raw::<nsIDocument>()).mCompatMode
};
PerDocumentStyleData(AtomicRefCell::new(PerDocumentStyleDataImpl {

View file

@ -14,7 +14,7 @@ use gecko_bindings::bindings;
use gecko_bindings::structs::{nsCSSKeyword, nsCSSProps_KTableEntry, nsCSSValue, nsCSSUnit, nsStringBuffer};
use gecko_bindings::structs::{nsMediaExpression_Range, nsMediaFeature};
use gecko_bindings::structs::{nsMediaFeature_ValueType, nsMediaFeature_RangeType, nsMediaFeature_RequirementFlags};
use gecko_bindings::structs::RawGeckoPresContextOwned;
use gecko_bindings::structs::{nsPresContext, RawGeckoPresContextOwned};
use media_queries::MediaType;
use parser::ParserContext;
use properties::{ComputedValues, StyleBuilder};
@ -36,7 +36,7 @@ pub struct Device {
/// NB: The pres context lifetime is tied to the styleset, who owns the
/// stylist, and thus the `Device`, so having a raw pres context pointer
/// here is fine.
pub pres_context: RawGeckoPresContextOwned,
pres_context: RawGeckoPresContextOwned,
default_values: Arc<ComputedValues>,
viewport_override: Option<ViewportConstraints>,
/// The font size of the root element
@ -105,11 +105,16 @@ impl Device {
self.root_font_size.store(size.0 as isize, Ordering::Relaxed)
}
/// Gets the pres context associated with this document.
pub fn pres_context(&self) -> &nsPresContext {
unsafe { &*self.pres_context }
}
/// Recreates the default computed values.
pub fn reset_computed_values(&mut self) {
// NB: A following stylesheet flush will populate this if appropriate.
self.viewport_override = None;
self.default_values = ComputedValues::default_values(unsafe { &*self.pres_context });
self.default_values = ComputedValues::default_values(self.pres_context());
self.used_root_font_size.store(false, Ordering::Relaxed);
}
@ -134,10 +139,10 @@ impl Device {
// FIXME(emilio): Gecko allows emulating random media with
// mIsEmulatingMedia / mMediaEmulated . Refactor both sides so that
// is supported (probably just making MediaType an Atom).
if (*self.pres_context).mMedium == atom!("screen").as_ptr() {
if self.pres_context().mMedium == atom!("screen").as_ptr() {
MediaType::Screen
} else {
debug_assert!((*self.pres_context).mMedium == atom!("print").as_ptr());
debug_assert!(self.pres_context().mMedium == atom!("print").as_ptr());
MediaType::Print
}
}
@ -150,19 +155,19 @@ impl Device {
Au::from_f32_px(v.size.height))
}).unwrap_or_else(|| unsafe {
// TODO(emilio): Need to take into account scrollbars.
Size2D::new(Au((*self.pres_context).mVisibleArea.width),
Au((*self.pres_context).mVisibleArea.height))
let area = &self.pres_context().mVisibleArea;
Size2D::new(Au(area.width), Au(area.height))
})
}
/// Returns whether document colors are enabled.
pub fn use_document_colors(&self) -> bool {
unsafe { (*self.pres_context).mUseDocumentColors() != 0 }
self.pres_context().mUseDocumentColors() != 0
}
/// Returns the default background color.
pub fn default_background_color(&self) -> RGBA {
convert_nscolor_to_rgba(unsafe { (*self.pres_context).mBackgroundColor })
convert_nscolor_to_rgba(self.pres_context().mBackgroundColor)
}
}

View file

@ -401,7 +401,7 @@ impl CounterStyleOrNone {
pub fn to_gecko_value(self, gecko_value: &mut CounterStylePtr, device: &Device) {
use gecko_bindings::bindings::Gecko_SetCounterStyleToName as set_name;
use gecko_bindings::bindings::Gecko_SetCounterStyleToSymbols as set_symbols;
let pres_context = unsafe { &*device.pres_context };
let pres_context = device.pres_context();
match self {
CounterStyleOrNone::None => unsafe {
set_name(gecko_value, atom!("none").into_addrefed(), pres_context);

View file

@ -672,7 +672,7 @@ impl FontMetricsProvider for GeckoFontMetricsProvider {
in_media_query: bool, device: &Device) -> FontMetricsQueryResult {
use gecko_bindings::bindings::Gecko_GetFontMetrics;
let gecko_metrics = unsafe {
Gecko_GetFontMetrics(&*device.pres_context,
Gecko_GetFontMetrics(device.pres_context(),
wm.is_vertical() && !wm.is_sideways(),
font.gecko(),
font_size.0,
@ -771,6 +771,11 @@ impl<'le> TElement for GeckoElement<'le> {
unsafe { GeckoNode(&*(self.0 as *const _ as *const RawGeckoNode)) }
}
fn owner_doc_matches_for_testing(&self, device: &Device) -> bool {
self.as_node().owner_doc() as *const structs::nsIDocument ==
device.pres_context().mDocument.raw::<structs::nsIDocument>()
}
fn style_attribute(&self) -> Option<&Arc<Locked<PropertyDeclarationBlock>>> {
if !self.may_have_style_attribute() {
return None;

View file

@ -274,6 +274,7 @@ trait PrivateMatchMethods: TElement {
if self.is_native_anonymous() || cascade_target == CascadeTarget::EagerPseudo {
cascade_flags.insert(PROHIBIT_DISPLAY_CONTENTS);
} else if self.is_root() {
debug_assert!(self.owner_doc_matches_for_testing(shared_context.stylist.device()));
cascade_flags.insert(IS_ROOT_ELEMENT);
}

View file

@ -1551,7 +1551,7 @@ fn static_assert() {
pub fn fixup_none_generic(&mut self, device: &Device) {
unsafe {
bindings::Gecko_nsStyleFont_FixupNoneGeneric(&mut self.gecko, &*device.pres_context)
bindings::Gecko_nsStyleFont_FixupNoneGeneric(&mut self.gecko, device.pres_context())
}
}
@ -1621,7 +1621,7 @@ fn static_assert() {
}
pub fn fixup_font_min_size(&mut self, device: &Device) {
unsafe { bindings::Gecko_nsStyleFont_FixupMinFontSize(&mut self.gecko, &*device.pres_context) }
unsafe { bindings::Gecko_nsStyleFont_FixupMinFontSize(&mut self.gecko, device.pres_context()) }
}
pub fn apply_unconstrained_font_size(&mut self, v: Au) {

View file

@ -108,7 +108,7 @@
fn to_computed_value(&self, cx: &Context) -> Self::ComputedValue {
unsafe {
Gecko_GetLookAndFeelSystemColor(*self as i32,
&*cx.device.pres_context)
cx.device.pres_context())
}
}

View file

@ -2388,9 +2388,12 @@ ${helpers.single_keyword("-moz-math-variant",
let mut system: nsFont = unsafe { mem::uninitialized() };
unsafe {
bindings::Gecko_nsFont_InitSystem(&mut system, id as i32,
cx.style.get_font().gecko(),
&*cx.device.pres_context)
bindings::Gecko_nsFont_InitSystem(
&mut system,
id as i32,
cx.style.get_font().gecko(),
cx.device.pres_context()
)
}
let family = system.fontlist.mFontlist.iter().map(|font| {
use properties::longhands::font_family::computed_value::*;

View file

@ -2783,13 +2783,14 @@ pub fn apply_declarations<'a, F, I>(device: &Device,
// which Gecko just does regular cascading with. Do the same.
// This can only happen in the case where the language changed but the family did not
if generic != structs::kGenericFont_NONE {
let pres_context = context.device.pres_context;
let gecko_font = context.mutate_style().mutate_font().gecko_mut();
let gecko_font = context.style.mutate_font().gecko_mut();
gecko_font.mGenericID = generic;
unsafe {
bindings::Gecko_nsStyleFont_PrefillDefaultForGeneric(gecko_font,
&*pres_context,
generic);
bindings::Gecko_nsStyleFont_PrefillDefaultForGeneric(
gecko_font,
context.device.pres_context(),
generic
);
}
}
}

View file

@ -141,7 +141,7 @@ impl UrlMatchingFunction {
UrlMatchingFunction::RegExp(ref pat) => pat,
});
unsafe {
Gecko_DocumentRule_UseForPresentation(&*device.pres_context, &*pattern, func)
Gecko_DocumentRule_UseForPresentation(device.pres_context(), &*pattern, func)
}
}

View file

@ -254,7 +254,7 @@ impl ToComputedValue for Color {
#[cfg(feature = "gecko")]
Color::Special(special) => {
use self::gecko::SpecialColorKeyword as Keyword;
let pres_context = unsafe { &*_context.device.pres_context };
let pres_context = _context.device.pres_context();
convert_nscolor_to_computedcolor(match special {
Keyword::MozDefaultColor => pres_context.mDefaultColor,
Keyword::MozDefaultBackgroundColor => pres_context.mBackgroundColor,
@ -268,7 +268,7 @@ impl ToComputedValue for Color {
use dom::TElement;
use gecko::wrapper::GeckoElement;
use gecko_bindings::bindings::Gecko_GetBody;
let pres_context = unsafe { &*_context.device.pres_context };
let pres_context = _context.device.pres_context();
let body = unsafe {
Gecko_GetBody(pres_context)
};

View file

@ -351,8 +351,7 @@ impl PhysicalLength {
const MM_PER_INCH: f32 = 25.4;
let physical_inch = unsafe {
let pres_context = &*context.device.pres_context;
bindings::Gecko_GetAppUnitsPerPhysicalInch(&pres_context)
bindings::Gecko_GetAppUnitsPerPhysicalInch(context.device.pres_context())
};
let inch = self.0 / MM_PER_INCH;