mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
script: Implement DocumentOrShadowDOM.adoptedStylesheet
with FrozenArray
(#38163)
Spec: https://drafts.csswg.org/cssom/#dom-documentorshadowroot-adoptedstylesheets Implement `DocumentOrShadowDOM.adoptedStylesheet`. Due to `ObservableArray` being a massive issue on its own, it will be as it was a `FrozenArray` at first. This approach is similar to how Gecko implement adopted stylesheet. See https://phabricator.services.mozilla.com/D144547#change-IXyOzxxFn8sU. All of the changes will be gated behind a preference `dom_adoptedstylesheet_enabled`. Adopted stylesheet is implemented by adding the setter and getter of it. While the getter works like a normal attribute getter, the setter need to consider the inner working of document and shadow root StylesheetSet, specifically the ordering and the invalidations. Particularly for setter, we will clear all of the adopted stylesheet within the StylesheetSet and readd them. Possible optimization exist, but the focus should be directed to implementing `ObservableArray`. More context about the implementations https://hackmd.io/vtJAn4UyS_O0Idvk5dCO_w. Testing: Existing WPT Coverage Fixes: https://github.com/servo/servo/issues/37561 --------- Signed-off-by: Jo Steven Novaryo <jo.steven.novaryo@huawei.com>
This commit is contained in:
parent
d2e5137201
commit
f523445fc3
24 changed files with 434 additions and 139 deletions
|
@ -40,7 +40,7 @@ use fnv::FnvHashMap;
|
|||
use html5ever::{LocalName, Namespace, QualName, local_name, ns};
|
||||
use hyper_serde::Serde;
|
||||
use ipc_channel::ipc;
|
||||
use js::rust::{HandleObject, HandleValue};
|
||||
use js::rust::{HandleObject, HandleValue, MutableHandleValue};
|
||||
use keyboard_types::{Code, Key, KeyState, Modifiers};
|
||||
use layout_api::{
|
||||
PendingRestyle, ReflowGoal, RestyleReason, TrustedNodeAddress, node_id_from_scroll_id,
|
||||
|
@ -58,6 +58,7 @@ use profile_traits::ipc as profile_ipc;
|
|||
use profile_traits::time::TimerMetadataFrameType;
|
||||
use regex::bytes::Regex;
|
||||
use script_bindings::interfaces::DocumentHelpers;
|
||||
use script_bindings::script_runtime::JSContext;
|
||||
use script_traits::{ConstellationInputEvent, DocumentActivity, ProgressiveWebMetricType};
|
||||
use servo_arc::Arc;
|
||||
use servo_config::pref;
|
||||
|
@ -114,6 +115,7 @@ use crate::dom::bindings::domname::{
|
|||
self, is_valid_attribute_local_name, is_valid_element_local_name, namespace_from_domstring,
|
||||
};
|
||||
use crate::dom::bindings::error::{Error, ErrorInfo, ErrorResult, Fallible};
|
||||
use crate::dom::bindings::frozenarray::CachedFrozenArray;
|
||||
use crate::dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
|
||||
use crate::dom::bindings::num::Finite;
|
||||
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
|
||||
|
@ -564,6 +566,12 @@ pub(crate) struct Document {
|
|||
active_keyboard_modifiers: Cell<Modifiers>,
|
||||
/// The node that is currently highlighted by the devtools
|
||||
highlighted_dom_node: MutNullableDom<Node>,
|
||||
/// The constructed stylesheet that is adopted by this [Document].
|
||||
/// <https://drafts.csswg.org/cssom/#dom-documentorshadowroot-adoptedstylesheets>
|
||||
adopted_stylesheets: DomRefCell<Vec<Dom<CSSStyleSheet>>>,
|
||||
/// Cached frozen array of [`Self::adopted_stylesheets`]
|
||||
#[ignore_malloc_size_of = "mozjs"]
|
||||
adopted_stylesheets_frozen_types: CachedFrozenArray,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
|
@ -4253,6 +4261,8 @@ impl Document {
|
|||
intersection_observers: Default::default(),
|
||||
active_keyboard_modifiers: Cell::new(Modifiers::empty()),
|
||||
highlighted_dom_node: Default::default(),
|
||||
adopted_stylesheets: Default::default(),
|
||||
adopted_stylesheets_frozen_types: CachedFrozenArray::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4898,26 +4908,30 @@ impl Document {
|
|||
.and_then(|s| s.owner.get_cssom_object())
|
||||
}
|
||||
|
||||
/// Add a stylesheet owned by `owner` to the list of document sheets, in the
|
||||
/// correct tree position.
|
||||
/// Add a stylesheet owned by `owner_node` to the list of document sheets, in the
|
||||
/// correct tree position. Additionally, ensure that the owned stylesheet is inserted
|
||||
/// before any constructed stylesheet.
|
||||
///
|
||||
/// <https://drafts.csswg.org/cssom/#documentorshadowroot-final-css-style-sheets>
|
||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))] // Owner needs to be rooted already necessarily.
|
||||
pub(crate) fn add_stylesheet(&self, owner: StylesheetSource, sheet: Arc<Stylesheet>) {
|
||||
pub(crate) fn add_owned_stylesheet(&self, owner_node: &Element, sheet: Arc<Stylesheet>) {
|
||||
let stylesheets = &mut *self.stylesheets.borrow_mut();
|
||||
|
||||
// TODO(stevennovayo): support constructed stylesheet for adopted stylesheet and its ordering
|
||||
let insertion_point = match &owner {
|
||||
StylesheetSource::Element(owner_elem) => stylesheets
|
||||
.iter()
|
||||
.map(|(sheet, _origin)| sheet)
|
||||
.find(|sheet_in_doc| match sheet_in_doc.owner {
|
||||
StylesheetSource::Element(ref other_elem) => {
|
||||
owner_elem.upcast::<Node>().is_before(other_elem.upcast())
|
||||
// FIXME(stevennovaryo): This is almost identical with the one in ShadowRoot::add_stylesheet.
|
||||
let insertion_point = stylesheets
|
||||
.iter()
|
||||
.map(|(sheet, _origin)| sheet)
|
||||
.find(|sheet_in_doc| {
|
||||
match &sheet_in_doc.owner {
|
||||
StylesheetSource::Element(other_node) => {
|
||||
owner_node.upcast::<Node>().is_before(other_node.upcast())
|
||||
},
|
||||
StylesheetSource::Constructed(_) => unreachable!(),
|
||||
})
|
||||
.cloned(),
|
||||
StylesheetSource::Constructed(_) => unreachable!(),
|
||||
};
|
||||
// Non-constructed stylesheet should be ordered before the
|
||||
// constructed ones.
|
||||
StylesheetSource::Constructed(_) => true,
|
||||
}
|
||||
})
|
||||
.cloned();
|
||||
|
||||
if self.has_browsing_context() {
|
||||
self.window.layout_mut().add_stylesheet(
|
||||
|
@ -4927,7 +4941,40 @@ impl Document {
|
|||
}
|
||||
|
||||
DocumentOrShadowRoot::add_stylesheet(
|
||||
owner,
|
||||
StylesheetSource::Element(Dom::from_ref(owner_node)),
|
||||
StylesheetSetRef::Document(stylesheets),
|
||||
sheet,
|
||||
insertion_point,
|
||||
self.style_shared_lock(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Append a constructed stylesheet to the back of document stylesheet set. Because
|
||||
/// it would be the last element, we therefore would not mess with the ordering.
|
||||
///
|
||||
/// <https://drafts.csswg.org/cssom/#documentorshadowroot-final-css-style-sheets>
|
||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
||||
pub(crate) fn append_constructed_stylesheet(&self, cssom_stylesheet: &CSSStyleSheet) {
|
||||
debug_assert!(cssom_stylesheet.is_constructed());
|
||||
|
||||
let stylesheets = &mut *self.stylesheets.borrow_mut();
|
||||
let sheet = cssom_stylesheet.style_stylesheet_arc().clone();
|
||||
|
||||
let insertion_point = stylesheets
|
||||
.iter()
|
||||
.last()
|
||||
.map(|(sheet, _origin)| sheet)
|
||||
.cloned();
|
||||
|
||||
if self.has_browsing_context() {
|
||||
self.window.layout_mut().add_stylesheet(
|
||||
sheet.clone(),
|
||||
insertion_point.as_ref().map(|s| s.sheet.clone()),
|
||||
);
|
||||
}
|
||||
|
||||
DocumentOrShadowRoot::add_stylesheet(
|
||||
StylesheetSource::Constructed(Dom::from_ref(cssom_stylesheet)),
|
||||
StylesheetSetRef::Document(stylesheets),
|
||||
sheet,
|
||||
insertion_point,
|
||||
|
@ -6719,6 +6766,40 @@ impl DocumentMethods<crate::DomTypeHolder> for Document {
|
|||
can_gc,
|
||||
)
|
||||
}
|
||||
|
||||
/// <https://drafts.csswg.org/cssom/#dom-documentorshadowroot-adoptedstylesheets>
|
||||
fn AdoptedStyleSheets(&self, context: JSContext, can_gc: CanGc, retval: MutableHandleValue) {
|
||||
self.adopted_stylesheets_frozen_types.get_or_init(
|
||||
|| {
|
||||
self.adopted_stylesheets
|
||||
.borrow()
|
||||
.clone()
|
||||
.iter()
|
||||
.map(|sheet| sheet.as_rooted())
|
||||
.collect()
|
||||
},
|
||||
context,
|
||||
retval,
|
||||
can_gc,
|
||||
);
|
||||
}
|
||||
|
||||
/// <https://drafts.csswg.org/cssom/#dom-documentorshadowroot-adoptedstylesheets>
|
||||
fn SetAdoptedStyleSheets(&self, context: JSContext, val: HandleValue) -> ErrorResult {
|
||||
let result = DocumentOrShadowRoot::set_adopted_stylesheet_from_jsval(
|
||||
context,
|
||||
self.adopted_stylesheets.borrow_mut().as_mut(),
|
||||
val,
|
||||
&StyleSheetListOwner::Document(Dom::from_ref(self)),
|
||||
);
|
||||
|
||||
// If update is successful, clear the FrozenArray cache.
|
||||
if result.is_ok() {
|
||||
self.adopted_stylesheets_frozen_types.clear()
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
fn update_with_current_instant(marker: &Cell<Option<CrossProcessInstant>>) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue