mirror of
https://github.com/servo/servo.git
synced 2025-07-19 13:23:46 +01:00
css: Refactor StyleSheetInDocument
owner (#38136)
Refactor `documentotshadowroot::StyleSheetInDocument`, renaming it into `ServoStylesheetInDocument` to avoid confusion with Stylo's `StylesheetInDocument` trait. To support constructed stylesheet. The `ServoStylesheetInDocument.owner` would contains enum of: - `Dom<Element>` - for stylesheet parsed from an element. - `Dom<CSSStylesheet>` - for constructed stylesheet. Testing: No WPT regression. Fixes: #38133 --------- Signed-off-by: Jo Steven Novaryo <jo.steven.novaryo@huawei.com>
This commit is contained in:
parent
d671f58078
commit
bbed6cddcd
6 changed files with 112 additions and 57 deletions
|
@ -26,46 +26,77 @@ use crate::dom::element::Element;
|
|||
use crate::dom::htmlelement::HTMLElement;
|
||||
use crate::dom::node::{self, Node, VecPreOrderInsertionHelper};
|
||||
use crate::dom::shadowroot::ShadowRoot;
|
||||
use crate::dom::types::CSSStyleSheet;
|
||||
use crate::dom::window::Window;
|
||||
use crate::script_runtime::CanGc;
|
||||
use crate::stylesheet_set::StylesheetSetRef;
|
||||
|
||||
/// Stylesheet could be constructed by a CSSOM object CSSStylesheet or parsed
|
||||
/// from HTML element such as `<style>` or `<link>`.
|
||||
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
||||
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
|
||||
pub(crate) struct StyleSheetInDocument {
|
||||
pub(crate) enum StylesheetSource {
|
||||
Element(Dom<Element>),
|
||||
// TODO(stevennovaryo): This type of enum would not be used until we implement adopted stylesheet
|
||||
#[allow(dead_code)]
|
||||
Constructed(Dom<CSSStyleSheet>),
|
||||
}
|
||||
|
||||
impl StylesheetSource {
|
||||
pub(crate) fn get_cssom_object(&self) -> Option<DomRoot<CSSStyleSheet>> {
|
||||
match self {
|
||||
StylesheetSource::Element(el) => el.upcast::<Node>().get_cssom_stylesheet(),
|
||||
StylesheetSource::Constructed(ss) => Some(ss.as_rooted()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_a_valid_owner(&self) -> bool {
|
||||
match self {
|
||||
StylesheetSource::Element(el) => el.as_stylesheet_owner().is_some(),
|
||||
StylesheetSource::Constructed(ss) => ss.is_constructed(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
||||
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
|
||||
pub(crate) struct ServoStylesheetInDocument {
|
||||
#[ignore_malloc_size_of = "Arc"]
|
||||
#[no_trace]
|
||||
pub(crate) sheet: Arc<Stylesheet>,
|
||||
pub(crate) owner: Dom<Element>,
|
||||
/// The object that owns this stylesheet. For constructed stylesheet, it would be the
|
||||
/// CSSOM object itself, and for stylesheet generated by an element, it would be the
|
||||
/// html element. This is used to get the CSSOM Stylesheet within a DocumentOrShadowDOM.
|
||||
pub(crate) owner: StylesheetSource,
|
||||
}
|
||||
|
||||
// This is necessary because this type is contained within a Stylo type which needs
|
||||
// Stylo's version of MallocSizeOf.
|
||||
impl stylo_malloc_size_of::MallocSizeOf for StyleSheetInDocument {
|
||||
impl stylo_malloc_size_of::MallocSizeOf for ServoStylesheetInDocument {
|
||||
fn size_of(&self, ops: &mut stylo_malloc_size_of::MallocSizeOfOps) -> usize {
|
||||
<StyleSheetInDocument as malloc_size_of::MallocSizeOf>::size_of(self, ops)
|
||||
<ServoStylesheetInDocument as malloc_size_of::MallocSizeOf>::size_of(self, ops)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for StyleSheetInDocument {
|
||||
impl fmt::Debug for ServoStylesheetInDocument {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.sheet.fmt(formatter)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for StyleSheetInDocument {
|
||||
impl PartialEq for ServoStylesheetInDocument {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
Arc::ptr_eq(&self.sheet, &other.sheet)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToMediaListKey for StyleSheetInDocument {
|
||||
impl ToMediaListKey for ServoStylesheetInDocument {
|
||||
fn to_media_list_key(&self) -> MediaListKey {
|
||||
self.sheet.contents.to_media_list_key()
|
||||
}
|
||||
}
|
||||
|
||||
impl ::style::stylesheets::StylesheetInDocument for StyleSheetInDocument {
|
||||
impl ::style::stylesheets::StylesheetInDocument for ServoStylesheetInDocument {
|
||||
fn enabled(&self) -> bool {
|
||||
self.sheet.enabled()
|
||||
}
|
||||
|
@ -85,7 +116,7 @@ impl ::style::stylesheets::StylesheetInDocument for StyleSheetInDocument {
|
|||
|
||||
// https://w3c.github.io/webcomponents/spec/shadow/#extensions-to-the-documentorshadowroot-mixin
|
||||
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
|
||||
#[derive(JSTraceable, MallocSizeOf)]
|
||||
#[derive(Clone, JSTraceable, MallocSizeOf)]
|
||||
pub(crate) struct DocumentOrShadowRoot {
|
||||
window: Dom<Window>,
|
||||
}
|
||||
|
@ -225,18 +256,18 @@ impl DocumentOrShadowRoot {
|
|||
/// Remove a stylesheet owned by `owner` from the list of document sheets.
|
||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))] // Owner needs to be rooted already necessarily.
|
||||
pub(crate) fn remove_stylesheet(
|
||||
owner: &Element,
|
||||
owner: StylesheetSource,
|
||||
s: &Arc<Stylesheet>,
|
||||
mut stylesheets: StylesheetSetRef<StyleSheetInDocument>,
|
||||
mut stylesheets: StylesheetSetRef<ServoStylesheetInDocument>,
|
||||
) {
|
||||
let guard = s.shared_lock.read();
|
||||
|
||||
// FIXME(emilio): Would be nice to remove the clone, etc.
|
||||
stylesheets.remove_stylesheet(
|
||||
None,
|
||||
StyleSheetInDocument {
|
||||
ServoStylesheetInDocument {
|
||||
sheet: s.clone(),
|
||||
owner: Dom::from_ref(owner),
|
||||
owner,
|
||||
},
|
||||
&guard,
|
||||
);
|
||||
|
@ -246,18 +277,15 @@ impl DocumentOrShadowRoot {
|
|||
/// correct tree position.
|
||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))] // Owner needs to be rooted already necessarily.
|
||||
pub(crate) fn add_stylesheet(
|
||||
owner: &Element,
|
||||
mut stylesheets: StylesheetSetRef<StyleSheetInDocument>,
|
||||
owner: StylesheetSource,
|
||||
mut stylesheets: StylesheetSetRef<ServoStylesheetInDocument>,
|
||||
sheet: Arc<Stylesheet>,
|
||||
insertion_point: Option<StyleSheetInDocument>,
|
||||
insertion_point: Option<ServoStylesheetInDocument>,
|
||||
style_shared_lock: &StyleSharedRwLock,
|
||||
) {
|
||||
debug_assert!(owner.as_stylesheet_owner().is_some(), "Wat");
|
||||
debug_assert!(owner.is_a_valid_owner(), "Wat");
|
||||
|
||||
let sheet = StyleSheetInDocument {
|
||||
sheet,
|
||||
owner: Dom::from_ref(owner),
|
||||
};
|
||||
let sheet = ServoStylesheetInDocument { sheet, owner };
|
||||
|
||||
let guard = style_shared_lock.read();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue