style: Introduce TDocument::is_html_element and TDocument::quirks_mode.

This allows some code to read a bit nicer, and stop passing down quirks mode to
querySelector / querySelectorAll.
This commit is contained in:
Emilio Cobos Álvarez 2017-10-26 13:20:10 +02:00
parent bfa7cd7d9e
commit dd5cd29a61
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
5 changed files with 38 additions and 26 deletions

View file

@ -51,7 +51,7 @@ use script_layout_interface::{OpaqueStyleAndLayoutData, StyleData};
use script_layout_interface::wrapper_traits::{DangerousThreadSafeLayoutNode, GetLayoutData, LayoutNode};
use script_layout_interface::wrapper_traits::{PseudoElementType, ThreadSafeLayoutElement, ThreadSafeLayoutNode};
use selectors::attr::{AttrSelectorOperation, NamespaceConstraint, CaseSensitivity};
use selectors::matching::{ElementSelectorFlags, MatchingContext, RelevantLinkStatus};
use selectors::matching::{ElementSelectorFlags, MatchingContext, QuirksMode, RelevantLinkStatus};
use selectors::matching::VisitedHandlingMode;
use selectors::sink::Push;
use servo_arc::{Arc, ArcBorrow};
@ -298,6 +298,14 @@ impl<'ld> TDocument for ServoLayoutDocument<'ld> {
fn as_node(&self) -> Self::ConcreteNode {
ServoLayoutNode::from_layout_js(self.document.upcast())
}
fn quirks_mode(&self) -> QuirksMode {
unsafe { self.document.quirks_mode() }
}
fn is_html_document(&self) -> bool {
unsafe { self.document.is_html_document_for_layout() }
}
}
impl<'ld> ServoLayoutDocument<'ld> {
@ -785,9 +793,9 @@ impl<'le> ::selectors::Element for ServoLayoutElement<'le> {
if !self.element.is_html_element() {
return false;
}
self.as_node().node.owner_doc_for_layout().is_html_document_for_layout()
}
self.as_node().owner_doc().is_html_document()
}
}

View file

@ -22,7 +22,7 @@ use properties::{AnimationRules, ComputedValues, PropertyDeclarationBlock};
use rule_tree::CascadeLevel;
use selector_parser::{AttrValue, PseudoClassStringArg, PseudoElement, SelectorImpl};
use selectors::Element as SelectorsElement;
use selectors::matching::{ElementSelectorFlags, VisitedHandlingMode};
use selectors::matching::{ElementSelectorFlags, QuirksMode, VisitedHandlingMode};
use selectors::sink::Push;
use servo_arc::{Arc, ArcBorrow};
use shared_lock::Locked;
@ -140,6 +140,12 @@ pub trait TDocument : Sized + Copy + Clone {
/// Get this document as a `TNode`.
fn as_node(&self) -> Self::ConcreteNode;
/// Returns whether this document is an HTML document.
fn is_html_document(&self) -> bool;
/// Returns the quirks mode of this document.
fn quirks_mode(&self) -> QuirksMode;
}
/// The `TNode` trait. This is the main generic trait over which the style

View file

@ -6,7 +6,7 @@
//! and Gecko.
use context::QuirksMode;
use dom::{TElement, TNode};
use dom::{TDocument, TElement, TNode};
use invalidation::element::invalidator::{Invalidation, InvalidationProcessor, InvalidationVector};
use selectors::{Element, NthIndexCache, SelectorList};
use selectors::matching::{self, MatchingContext, MatchingMode};
@ -308,7 +308,6 @@ pub fn query_selector<E, Q>(
root: E::ConcreteNode,
selector_list: &SelectorList<E::Impl>,
results: &mut Q::Output,
quirks_mode: QuirksMode,
)
where
E: TElement,
@ -316,6 +315,7 @@ where
{
use invalidation::element::invalidator::TreeStyleInvalidator;
let quirks_mode = root.owner_doc().quirks_mode();
let fast_result = query_selector_fast::<E, Q>(
root,
selector_list,

View file

@ -102,6 +102,14 @@ impl<'ld> TDocument for GeckoDocument<'ld> {
fn as_node(&self) -> Self::ConcreteNode {
GeckoNode(&self.0._base)
}
fn is_html_document(&self) -> bool {
self.0.mType == structs::root::nsIDocument_Type::eHTML
}
fn quirks_mode(&self) -> QuirksMode {
self.0.mCompatMode.into()
}
}
/// A simple wrapper over a non-null Gecko node (`nsINode`) pointer.
@ -172,12 +180,6 @@ impl<'ln> GeckoNode<'ln> {
(self.0).mBoolFlags
}
/// Owner document quirks mode getter.
#[inline]
pub fn owner_document_quirks_mode(&self) -> QuirksMode {
self.owner_doc().0.mCompatMode.into()
}
#[inline]
fn get_bool_flag(&self, flag: nsINode_BooleanFlag) -> bool {
self.bool_flags() & (1u32 << flag as u32) != 0
@ -665,12 +667,6 @@ impl<'le> GeckoElement<'le> {
unsafe { Gecko_GetDocumentLWTheme(node.owner_doc().0) }
}
/// Owner document quirks mode getter.
#[inline]
pub fn owner_document_quirks_mode(&self) -> QuirksMode {
self.as_node().owner_document_quirks_mode()
}
/// Only safe to call on the main thread, with exclusive access to the element and
/// its ancestors.
/// This function is also called after display property changed for SMIL animation.
@ -1614,7 +1610,7 @@ impl<'le> TElement for GeckoElement<'le> {
if self.get_local_name().as_ptr() == atom!("th").as_ptr() {
hints.push(TH_RULE.clone());
} else if self.get_local_name().as_ptr() == atom!("table").as_ptr() &&
self.as_node().owner_doc().0.mCompatMode == structs::nsCompatibility::eCompatibility_NavQuirks {
self.as_node().owner_doc().quirks_mode() == QuirksMode::Quirks {
hints.push(TABLE_COLOR_RULE.clone());
}
}
@ -2061,7 +2057,7 @@ impl<'le> ::selectors::Element for GeckoElement<'le> {
fn is_html_element_in_html_document(&self) -> bool {
self.is_html_element() &&
self.as_node().owner_doc().0.mType == structs::root::nsIDocument_Type::eHTML
self.as_node().owner_doc().is_html_document()
}
fn ignores_nth_child_selectors(&self) -> bool {

View file

@ -19,7 +19,7 @@ use style::applicable_declarations::ApplicableDeclarationBlock;
use style::context::{CascadeInputs, QuirksMode, SharedStyleContext, StyleContext};
use style::context::ThreadLocalStyleContext;
use style::data::{ElementStyles, self};
use style::dom::{ShowSubtreeData, TElement, TNode};
use style::dom::{ShowSubtreeData, TDocument, TElement, TNode};
use style::driver;
use style::element_state::{DocumentState, ElementState};
use style::error_reporting::{NullReporter, ParseErrorReporter};
@ -1577,7 +1577,9 @@ pub extern "C" fn Servo_StyleRule_SelectorMatchesElement(rule: RawServoStyleRule
};
let element = GeckoElement(element);
let mut ctx = MatchingContext::new(matching_mode, None, None, element.owner_document_quirks_mode());
let quirks_mode = element.as_node().owner_doc().quirks_mode();
let mut ctx =
MatchingContext::new(matching_mode, None, None, quirks_mode);
matches_selector(selector, 0, None, &element, &mut ctx, &mut |_, _| {})
})
}
@ -1591,9 +1593,10 @@ pub unsafe extern "C" fn Servo_SelectorList_Closest(
use style::dom_apis;
let element = GeckoElement(element);
let quirks_mode = element.as_node().owner_doc().quirks_mode();
let selectors = ::selectors::SelectorList::from_ffi(selectors).borrow();
dom_apis::element_closest(element, &selectors, element.owner_document_quirks_mode())
dom_apis::element_closest(element, &selectors, quirks_mode)
.map_or(ptr::null(), |e| e.0)
}
@ -1606,11 +1609,12 @@ pub unsafe extern "C" fn Servo_SelectorList_Matches(
use style::dom_apis;
let element = GeckoElement(element);
let quirks_mode = element.as_node().owner_doc().quirks_mode();
let selectors = ::selectors::SelectorList::from_ffi(selectors).borrow();
dom_apis::element_matches(
&element,
&selectors,
element.owner_document_quirks_mode(),
quirks_mode,
)
}
@ -1629,7 +1633,6 @@ pub unsafe extern "C" fn Servo_SelectorList_QueryFirst(
node,
&selectors,
&mut result,
node.owner_document_quirks_mode(),
);
result.map_or(ptr::null(), |e| e.0)
@ -1653,7 +1656,6 @@ pub unsafe extern "C" fn Servo_SelectorList_QueryAll(
node,
&selectors,
&mut result,
node.owner_document_quirks_mode(),
);
if !result.is_empty() {