style: Remove dependency on servo_url (#31358)

In order for stylo to be a separate crate, it needs to depend on less
things from Servo. This change makes it so that stylo no longer depends
on servo_url.
This commit is contained in:
Martin Robinson 2024-02-16 12:56:35 +01:00 committed by GitHub
parent 29e1dfe1e4
commit 9a6973d629
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
42 changed files with 236 additions and 144 deletions

View file

@ -32,6 +32,7 @@ use style::values::computed::font::FontStyle;
use style::values::specified::color::Color;
use style_traits::values::ToCss;
use style_traits::ParsingMode;
use url::Url;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::{
@ -1681,7 +1682,7 @@ impl CanvasState {
pub fn parse_color(canvas: Option<&HTMLCanvasElement>, string: &str) -> Result<RGBA, ()> {
let mut input = ParserInput::new(string);
let mut parser = Parser::new(&mut input);
let url = ServoUrl::parse("about:blank").unwrap();
let url = Url::parse("about:blank").unwrap().into();
let context = ParserContext::new(
Origin::Author,
&url,

View file

@ -7,7 +7,7 @@ use dom_struct::dom_struct;
use style::context::QuirksMode;
use style::parser::ParserContext;
use style::stylesheets::supports_rule::{parse_condition_or_declaration, Declaration};
use style::stylesheets::{CssRuleType, Origin};
use style::stylesheets::{CssRuleType, Origin, UrlExtraData};
use style_traits::ParsingMode;
use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
@ -39,10 +39,10 @@ impl CSS {
decl.push_str(": ");
decl.push_str(&value);
let decl = Declaration(decl);
let url = win.Document().url();
let url_data = UrlExtraData(win.Document().url().get_arc());
let context = ParserContext::new(
Origin::Author,
&url,
&url_data,
Some(CssRuleType::Style),
ParsingMode::DEFAULT,
QuirksMode::NoQuirks,
@ -62,10 +62,10 @@ impl CSS {
Err(..) => return false,
};
let url = win.Document().url();
let url_data = UrlExtraData(win.Document().url().get_arc());
let context = ParserContext::new(
Origin::Author,
&url,
&url_data,
Some(CssRuleType::Style),
ParsingMode::DEFAULT,
QuirksMode::NoQuirks,

View file

@ -13,7 +13,7 @@ use style::properties::{
};
use style::selector_parser::PseudoElement;
use style::shared_lock::Locked;
use style::stylesheets::{CssRuleType, Origin};
use style::stylesheets::{CssRuleType, Origin, UrlExtraData};
use style_traits::ParsingMode;
use crate::dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods;
@ -160,12 +160,15 @@ impl CSSStyleOwner {
fn base_url(&self) -> ServoUrl {
match *self {
CSSStyleOwner::Element(ref el) => window_from_node(&**el).Document().base_url(),
CSSStyleOwner::CSSRule(ref rule, _) => (*rule
.parent_stylesheet()
.style_stylesheet()
.contents
.url_data
.read())
CSSStyleOwner::CSSRule(ref rule, _) => ServoUrl::from(
rule.parent_stylesheet()
.style_stylesheet()
.contents
.url_data
.read()
.0
.clone(),
)
.clone(),
}
}
@ -307,7 +310,7 @@ impl CSSStyleDeclaration {
id,
&value,
Origin::Author,
&self.owner.base_url(),
&UrlExtraData(self.owner.base_url().get_arc()),
window.css_error_reporter(),
ParsingMode::DEFAULT,
quirks_mode,
@ -458,7 +461,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
// Step 3
*pdb = parse_style_attribute(
&value,
&self.owner.base_url(),
&UrlExtraData(self.owner.base_url().get_arc()),
window.css_error_reporter(),
quirks_mode,
CssRuleType::Style,

View file

@ -13,6 +13,7 @@ use js::jsapi::JSObject;
use js::rust::{CustomAutoRooterGuard, HandleObject};
use js::typedarray::{Float32Array, Float64Array};
use style::parser::ParserContext;
use url::Url;
use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::DOMMatrixBinding::{DOMMatrixInit, DOMMatrixMethods};
@ -830,10 +831,10 @@ pub fn transform_to_matrix(value: String) -> Fallible<(bool, Transform3D<f64>)>
let mut input = ParserInput::new(&value);
let mut parser = Parser::new(&mut input);
let url = ::servo_url::ServoUrl::parse("about:blank").unwrap();
let url_data = Url::parse("about:blank").unwrap().into();
let context = ParserContext::new(
::style::stylesheets::Origin::Author,
&url,
&url_data,
Some(::style::stylesheets::CssRuleType::Style),
::style_traits::ParsingMode::DEFAULT,
::style::context::QuirksMode::NoQuirks,

View file

@ -53,7 +53,7 @@ use style::selector_parser::{
};
use style::shared_lock::{Locked, SharedRwLock};
use style::stylesheets::layer_rule::LayerOrder;
use style::stylesheets::CssRuleType;
use style::stylesheets::{CssRuleType, UrlExtraData};
use style::values::generics::NonNegative;
use style::values::{computed, specified, AtomIdent, AtomString, CSSFloat};
use style::{dom_apis, thread_state, CaseSensitivityExt};
@ -724,7 +724,7 @@ impl<'dom> LayoutElementHelpers<'dom> for LayoutDom<'dom, Element> {
hints.push(from_declaration(
shared_lock,
PropertyDeclaration::BackgroundImage(background_image::SpecifiedValue(
vec![specified::Image::for_cascade(url.into())].into(),
vec![specified::Image::for_cascade(url.get_arc())].into(),
)),
));
}
@ -2752,7 +2752,10 @@ impl ElementMethods for Element {
fn Matches(&self, selectors: DOMString) -> Fallible<bool> {
let doc = document_from_node(self);
let url = doc.url();
let selectors = match SelectorParser::parse_author_origin_no_namespace(&selectors, &url) {
let selectors = match SelectorParser::parse_author_origin_no_namespace(
&selectors,
&UrlExtraData(url.get_arc()),
) {
Err(_) => return Err(Error::Syntax),
Ok(selectors) => selectors,
};
@ -2772,7 +2775,10 @@ impl ElementMethods for Element {
fn Closest(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
let doc = document_from_node(self);
let url = doc.url();
let selectors = match SelectorParser::parse_author_origin_no_namespace(&selectors, &url) {
let selectors = match SelectorParser::parse_author_origin_no_namespace(
&selectors,
&UrlExtraData(url.get_arc()),
) {
Err(_) => return Err(Error::Syntax),
Ok(selectors) => selectors,
};
@ -2926,7 +2932,7 @@ impl VirtualMethods for Element {
let win = window_from_node(self);
Arc::new(doc.style_shared_lock().wrap(parse_style_attribute(
&attr.value(),
&doc.base_url(),
&UrlExtraData(doc.base_url().get_arc()),
win.css_error_reporter(),
doc.quirks_mode(),
CssRuleType::Style,

View file

@ -87,8 +87,10 @@ impl HTMLBodyElementMethods for HTMLBodyElement {
// https://html.spec.whatwg.org/multipage/#dom-body-background
fn SetBackground(&self, input: DOMString) {
let value =
AttrValue::from_resolved_url(&document_from_node(self).base_url(), input.into());
let value = AttrValue::from_resolved_url(
&document_from_node(self).base_url().get_arc(),
input.into(),
);
self.upcast::<Element>()
.set_attribute(&local_name!("background"), value);
}
@ -123,6 +125,7 @@ impl HTMLBodyElementLayoutHelpers for LayoutDom<'_, HTMLBodyElement> {
.get_attr_for_layout(&ns!(), &local_name!("background"))
.and_then(AttrValue::as_resolved_url)
.cloned()
.map(Into::into)
}
}
@ -164,9 +167,10 @@ impl VirtualMethods for HTMLBodyElement {
local_name!("bgcolor") | local_name!("text") => {
AttrValue::from_legacy_color(value.into())
},
local_name!("background") => {
AttrValue::from_resolved_url(&document_from_node(self).base_url(), value.into())
},
local_name!("background") => AttrValue::from_resolved_url(
&document_from_node(self).base_url().get_arc(),
value.into(),
),
_ => self
.super_type()
.unwrap()

View file

@ -40,11 +40,12 @@ use style::context::QuirksMode;
use style::media_queries::MediaList;
use style::parser::ParserContext;
use style::str::is_ascii_digit;
use style::stylesheets::{CssRuleType, Origin};
use style::stylesheets::{CssRuleType, Origin, UrlExtraData};
use style::values::specified::length::{Length, NoCalcLength};
use style::values::specified::source_size_list::SourceSizeList;
use style::values::specified::AbsoluteLength;
use style_traits::ParsingMode;
use url::Url;
use crate::document_loader::{LoadBlocker, LoadType};
use crate::dom::activation::Activatable;
@ -678,7 +679,7 @@ impl HTMLImageElement {
fn matches_environment(&self, media_query: String) -> bool {
let document = document_from_node(self);
let quirks_mode = document.quirks_mode();
let document_url = &document.url();
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...
@ -686,7 +687,7 @@ impl HTMLImageElement {
// Also, ParsingMode::all() is wrong, and should be DEFAULT.
let context = ParserContext::new(
Origin::Author,
document_url,
&document_url_data,
Some(CssRuleType::Style),
ParsingMode::all(),
quirks_mode,
@ -1434,10 +1435,10 @@ impl LayoutHTMLImageElementHelpers for LayoutDom<'_, HTMLImageElement> {
pub fn parse_a_sizes_attribute(value: DOMString) -> SourceSizeList {
let mut input = ParserInput::new(&value);
let mut parser = Parser::new(&mut input);
let url = ServoUrl::parse("about:blank").unwrap();
let url_data = Url::parse("about:blank").unwrap().into();
let context = ParserContext::new(
Origin::Author,
&url,
&url_data,
Some(CssRuleType::Style),
// FIXME(emilio): why ::empty() instead of ::DEFAULT? Also, what do
// browsers do regarding quirks-mode in a media list?

View file

@ -18,7 +18,7 @@ use style::attr::AttrValue;
use style::media_queries::MediaList;
use style::parser::ParserContext as CssParserContext;
use style::str::HTML_SPACE_CHARACTERS;
use style::stylesheets::{CssRuleType, Origin, Stylesheet};
use style::stylesheets::{CssRuleType, Origin, Stylesheet, UrlExtraData};
use style_traits::ParsingMode;
use crate::dom::attr::Attr;
@ -310,14 +310,14 @@ impl HTMLLinkElement {
let mut input = ParserInput::new(&mq_str);
let mut css_parser = CssParser::new(&mut input);
let doc_url = document.url();
let document_url_data = &UrlExtraData(document.url().get_arc());
let window = document.window();
// FIXME(emilio): This looks somewhat fishy, since we use the context
// only to parse the media query list, CssRuleType::Media doesn't make
// much sense.
let context = CssParserContext::new(
Origin::Author,
&doc_url,
&document_url_data,
Some(CssRuleType::Media),
ParsingMode::DEFAULT,
document.quirks_mode(),

View file

@ -1117,7 +1117,7 @@ impl HTMLScriptElement {
if let Some(record) = record {
rooted!(in(*GlobalScope::get_cx()) let mut rval = UndefinedValue());
let evaluated =
module_tree.execute_module(global, record, rval.handle_mut().into());
module_tree.execute_module(&global, record, rval.handle_mut().into());
if let Err(exception) = evaluated {
module_tree.set_rethrow_error(exception);

View file

@ -12,7 +12,7 @@ use net_traits::ReferrerPolicy;
use servo_arc::Arc;
use style::media_queries::MediaList;
use style::parser::ParserContext as CssParserContext;
use style::stylesheets::{AllowImportRules, CssRuleType, Origin, Stylesheet};
use style::stylesheets::{AllowImportRules, CssRuleType, Origin, Stylesheet, UrlExtraData};
use style_traits::ParsingMode;
use crate::dom::bindings::cell::DomRefCell;
@ -100,11 +100,11 @@ impl HTMLStyleElement {
let data = node
.GetTextContent()
.expect("Element.textContent must be a string");
let url = window.get_url();
let url_data = UrlExtraData(window.get_url().get_arc());
let css_error_reporter = window.css_error_reporter();
let context = CssParserContext::new(
Origin::Author,
&url,
&url_data,
Some(CssRuleType::Media),
ParsingMode::DEFAULT,
doc.quirks_mode(),
@ -119,7 +119,7 @@ impl HTMLStyleElement {
let loader = StylesheetLoader::for_element(self.upcast());
let sheet = Stylesheet::from_str(
&data,
window.get_url(),
UrlExtraData(window.get_url().get_arc()),
Origin::Author,
mq,
shared_lock,

View file

@ -8,7 +8,7 @@ use servo_arc::Arc;
use style::media_queries::{MediaList as StyleMediaList, MediaQuery};
use style::parser::ParserContext;
use style::shared_lock::{Locked, SharedRwLock};
use style::stylesheets::{CssRuleType, Origin};
use style::stylesheets::{CssRuleType, Origin, UrlExtraData};
use style_traits::{ParsingMode, ToCss};
use crate::dom::bindings::codegen::Bindings::MediaListBinding::MediaListMethods;
@ -80,11 +80,11 @@ impl MediaListMethods for MediaList {
let mut parser = Parser::new(&mut input);
let global = self.global();
let window = global.as_window();
let url = window.get_url();
let url_data = UrlExtraData(window.get_url().get_arc());
let quirks_mode = window.Document().quirks_mode();
let context = ParserContext::new(
Origin::Author,
&url,
&url_data,
Some(CssRuleType::Media),
ParsingMode::DEFAULT,
quirks_mode,
@ -123,11 +123,11 @@ impl MediaListMethods for MediaList {
let mut parser = Parser::new(&mut input);
let global = self.global();
let win = global.as_window();
let url = win.get_url();
let url_data = UrlExtraData(win.get_url().get_arc());
let quirks_mode = win.Document().quirks_mode();
let context = ParserContext::new(
Origin::Author,
&url,
&url_data,
Some(CssRuleType::Media),
ParsingMode::DEFAULT,
quirks_mode,
@ -162,11 +162,11 @@ impl MediaListMethods for MediaList {
let mut parser = Parser::new(&mut input);
let global = self.global();
let win = global.as_window();
let url = win.get_url();
let url_data = UrlExtraData(win.get_url().get_arc());
let quirks_mode = win.Document().quirks_mode();
let context = ParserContext::new(
Origin::Author,
&url,
&url_data,
Some(CssRuleType::Media),
ParsingMode::DEFAULT,
quirks_mode,

View file

@ -41,7 +41,7 @@ use style::context::QuirksMode;
use style::dom::OpaqueNode;
use style::properties::ComputedValues;
use style::selector_parser::{SelectorImpl, SelectorParser};
use style::stylesheets::Stylesheet;
use style::stylesheets::{Stylesheet, UrlExtraData};
use uuid::Uuid;
use crate::document_loader::DocumentLoader;
@ -961,7 +961,10 @@ impl Node {
pub fn query_selector(&self, selectors: DOMString) -> Fallible<Option<DomRoot<Element>>> {
// Step 1.
let doc = self.owner_doc();
match SelectorParser::parse_author_origin_no_namespace(&selectors, &doc.url()) {
match SelectorParser::parse_author_origin_no_namespace(
&selectors,
&UrlExtraData(doc.url().get_arc()),
) {
// Step 2.
Err(_) => Err(Error::Syntax),
// Step 3.
@ -989,7 +992,10 @@ impl Node {
pub fn query_selector_iter(&self, selectors: DOMString) -> Fallible<QuerySelectorIterator> {
// Step 1.
let url = self.owner_doc().url();
match SelectorParser::parse_author_origin_no_namespace(&selectors, &url) {
match SelectorParser::parse_author_origin_no_namespace(
&selectors,
&UrlExtraData(url.get_arc()),
) {
// Step 2.
Err(_) => Err(Error::Syntax),
// Step 3.

View file

@ -73,7 +73,7 @@ use style::properties::style_structs::Font;
use style::properties::{PropertyId, ShorthandId};
use style::selector_parser::PseudoElement;
use style::str::HTML_SPACE_CHARACTERS;
use style::stylesheets::{CssRuleType, Origin};
use style::stylesheets::{CssRuleType, Origin, UrlExtraData};
use style_traits::{CSSPixel, DevicePixel, ParsingMode};
use url::Position;
use webrender_api::units::{DeviceIntPoint, DeviceIntSize, LayoutPixel};
@ -1328,11 +1328,11 @@ impl WindowMethods for Window {
fn MatchMedia(&self, query: DOMString) -> DomRoot<MediaQueryList> {
let mut input = ParserInput::new(&query);
let mut parser = Parser::new(&mut input);
let url = self.get_url();
let url_data = UrlExtraData(self.get_url().get_arc());
let quirks_mode = self.Document().quirks_mode();
let context = CssParserContext::new(
Origin::Author,
&url,
&url_data,
Some(CssRuleType::Media),
ParsingMode::DEFAULT,
quirks_mode,
@ -2826,11 +2826,16 @@ pub struct CSSErrorReporter {
unsafe_no_jsmanaged_fields!(CSSErrorReporter);
impl ParseErrorReporter for CSSErrorReporter {
fn report_error(&self, url: &ServoUrl, location: SourceLocation, error: ContextualParseError) {
fn report_error(
&self,
url: &UrlExtraData,
location: SourceLocation,
error: ContextualParseError,
) {
if log_enabled!(log::Level::Info) {
info!(
"Url:\t{}\n{}:{} {}",
url.as_str(),
url.0.as_str(),
location.line,
location.column,
error
@ -2844,7 +2849,7 @@ impl ParseErrorReporter for CSSErrorReporter {
.unwrap()
.send(ConstellationControlMsg::ReportCSSError(
self.pipelineid,
url.to_string(),
url.0.to_string(),
location.line,
location.column,
error.to_string(),

View file

@ -25,7 +25,7 @@ use style::shared_lock::{Locked, SharedRwLock};
use style::stylesheets::import_rule::{ImportLayer, ImportSheet, ImportSupportsCondition};
use style::stylesheets::{
CssRules, ImportRule, Origin, Stylesheet, StylesheetContents,
StylesheetLoader as StyleStylesheetLoader,
StylesheetLoader as StyleStylesheetLoader, UrlExtraData,
};
use style::values::CssUrl;
@ -159,7 +159,7 @@ impl FetchResponseListener for StylesheetContext {
let shared_lock = document.style_shared_lock().clone();
let sheet = Arc::new(Stylesheet::from_bytes(
&data,
final_url,
UrlExtraData(final_url.get_arc()),
protocol_encoding_label,
Some(environment_encoding),
Origin::Author,
@ -183,7 +183,7 @@ impl FetchResponseListener for StylesheetContext {
&data,
protocol_encoding_label,
Some(environment_encoding),
final_url,
UrlExtraData(final_url.get_arc()),
Some(&loader),
win.css_error_reporter(),
);
@ -404,7 +404,7 @@ impl<'a> StyleStylesheetLoader for StylesheetLoader<'a> {
// TODO (mrnayak) : Whether we should use the original loader's CORS
// setting? Fix this when spec has more details.
let source = StylesheetContextSource::Import(sheet.clone());
self.load(source, url, None, "".to_owned());
self.load(source, url.into(), None, "".to_owned());
Arc::new(lock.wrap(import))
}