Move Element::matches_environment to MediaList (#39034)

It more logically matches `MediaList`, and it allows us to
call this method with a document. This is required when
parsing Link headers, as they don't have an associated
element, but they do have a document.

Part of #35035

Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
This commit is contained in:
Tim van der Lippe 2025-09-04 08:21:01 +02:00 committed by GitHub
parent aac6aa6c70
commit fc6bee8b81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 34 deletions

View file

@ -12,7 +12,7 @@ use std::rc::Rc;
use std::str::FromStr;
use std::{fmt, mem};
use cssparser::{Parser as CssParser, ParserInput as CssParserInput, match_ignore_ascii_case};
use cssparser::match_ignore_ascii_case;
use devtools_traits::AttrInfo;
use dom_struct::dom_struct;
use embedder_traits::InputMethodType;
@ -38,8 +38,6 @@ use style::attr::{AttrValue, LengthOrPercentageOrAuto};
use style::computed_values::position::T as Position;
use style::context::QuirksMode;
use style::invalidation::element::restyle_hints::RestyleHint;
use style::media_queries::MediaList;
use style::parser::ParserContext as CssParserContext;
use style::properties::longhands::{
self, background_image, border_spacing, font_family, font_size,
};
@ -54,14 +52,13 @@ use style::selector_parser::{
};
use style::shared_lock::Locked;
use style::stylesheets::layer_rule::LayerOrder;
use style::stylesheets::{CssRuleType, Origin as CssOrigin, UrlExtraData};
use style::stylesheets::{CssRuleType, UrlExtraData};
use style::values::computed::Overflow;
use style::values::generics::NonNegative;
use style::values::generics::position::PreferredRatio;
use style::values::generics::ratio::Ratio;
use style::values::{AtomIdent, AtomString, CSSFloat, computed, specified};
use style::{ArcSlice, CaseSensitivityExt, dom_apis, thread_state};
use style_traits::ParsingMode as CssParsingMode;
use stylo_atoms::Atom;
use stylo_dom::ElementState;
use webrender_api::units::LayoutVector2D;
@ -859,32 +856,6 @@ impl Element {
.retain(|reg_obs| *reg_obs.observer != *observer)
}
/// <https://html.spec.whatwg.org/multipage/#matches-the-environment>
pub(crate) fn matches_environment(&self, media_query: &str) -> bool {
let document = self.owner_document();
let quirks_mode = document.quirks_mode();
let document_url_data = UrlExtraData(document.url().get_arc());
// FIXME(emilio): This should do the same that we do for other media
// lists regarding the rule type and such, though it doesn't really
// matter right now...
//
// Also, ParsingMode::all() is wrong, and should be DEFAULT.
let context = CssParserContext::new(
CssOrigin::Author,
&document_url_data,
Some(CssRuleType::Style),
CssParsingMode::all(),
quirks_mode,
/* namespaces = */ Default::default(),
None,
None,
);
let mut parser_input = CssParserInput::new(media_query);
let mut parser = CssParser::new(&mut parser_input);
let media_list = MediaList::parse(&context, &mut parser);
media_list.evaluate(document.window().layout().device(), quirks_mode)
}
/// <https://drafts.csswg.org/cssom-view/#scroll-a-target-into-view>
fn scroll_into_view_with_options(
&self,

View file

@ -75,6 +75,7 @@ use crate::dom::html::htmlformelement::{FormControl, HTMLFormElement};
use crate::dom::html::htmlmapelement::HTMLMapElement;
use crate::dom::html::htmlpictureelement::HTMLPictureElement;
use crate::dom::html::htmlsourceelement::HTMLSourceElement;
use crate::dom::medialist::MediaList;
use crate::dom::mouseevent::MouseEvent;
use crate::dom::node::{BindContext, Node, NodeDamage, NodeTraits, ShadowIncluding, UnbindContext};
use crate::dom::performanceresourcetiming::InitiatorType;
@ -713,7 +714,7 @@ impl HTMLImageElement {
// Step 4.6
if let Some(x) = element.get_attribute(&ns!(), &local_name!("media")) {
if !elem.matches_environment(&x.value()) {
if !MediaList::matches_environment(&elem.owner_document(), &x.value()) {
continue;
}
}

View file

@ -339,7 +339,7 @@ impl VirtualMethods for HTMLLinkElement {
}
let matches_media_environment =
self.upcast::<Element>().matches_environment(&attr.value());
MediaList::matches_environment(&self.owner_document(), &attr.value());
self.previous_media_environment_matched
.set(matches_media_environment);
},
@ -573,7 +573,7 @@ impl HTMLLinkElement {
None => "",
};
if !element.matches_environment(mq_str) {
if !MediaList::matches_environment(&document, mq_str) {
return;
}

View file

@ -19,6 +19,7 @@ use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::document::Document;
use crate::dom::window::Window;
use crate::script_runtime::CanGc;
@ -117,6 +118,31 @@ impl MediaList {
pub(crate) fn update_media_list(&self, media_queries: Arc<Locked<StyleMediaList>>) {
*self.media_queries.borrow_mut() = media_queries;
}
/// <https://html.spec.whatwg.org/multipage/#matches-the-environment>
pub(crate) fn matches_environment(document: &Document, media_query: &str) -> bool {
let quirks_mode = document.quirks_mode();
let document_url_data = UrlExtraData(document.url().get_arc());
// FIXME(emilio): This should do the same that we do for other media
// lists regarding the rule type and such, though it doesn't really
// matter right now...
//
// Also, ParsingMode::all() is wrong, and should be DEFAULT.
let context = ParserContext::new(
Origin::Author,
&document_url_data,
Some(CssRuleType::Style),
ParsingMode::all(),
quirks_mode,
/* namespaces = */ Default::default(),
None,
None,
);
let mut parser_input = ParserInput::new(media_query);
let mut parser = Parser::new(&mut parser_input);
let media_list = StyleMediaList::parse(&context, &mut parser);
media_list.evaluate(document.window().layout().device(), quirks_mode)
}
}
impl MediaListMethods<crate::DomTypeHolder> for MediaList {