Split CssUrl from SpecifiedUrl for non-value URLs.

This commit is contained in:
Xidorn Quan 2018-03-08 21:31:09 +11:00
parent 0090fbb3c8
commit fa5d76c395
10 changed files with 86 additions and 47 deletions

View file

@ -15,7 +15,7 @@ use std::sync::Arc;
use style_traits::{CssWriter, ParseError, ToCss};
use values::computed::{Context, ToComputedValue};
/// A specified url() value for servo.
/// A CSS url() value for servo.
///
/// Servo eagerly resolves SpecifiedUrls, which it can then take advantage of
/// when computing values. In contrast, Gecko uses a different URL backend, so
@ -24,7 +24,7 @@ use values::computed::{Context, ToComputedValue};
/// However, this approach is still not necessarily optimal: See
/// <https://bugzilla.mozilla.org/show_bug.cgi?id=1347435#c6>
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
pub struct SpecifiedUrl {
pub struct CssUrl {
/// The original URI. This might be optional since we may insert computed
/// values of images into the cascade directly, and we don't bother to
/// convert their serialization.
@ -38,7 +38,7 @@ pub struct SpecifiedUrl {
resolved: Option<ServoUrl>,
}
impl SpecifiedUrl {
impl CssUrl {
/// Try to parse a URL from a string value that is a valid CSS token for a
/// URL. Never fails - the API is only fallible to be compatible with the
/// gecko version.
@ -47,7 +47,7 @@ impl SpecifiedUrl {
-> Result<Self, ParseError<'a>> {
let serialization = Arc::new(url);
let resolved = context.url_data.join(&serialization).ok();
Ok(SpecifiedUrl {
Ok(CssUrl {
original: Some(serialization),
resolved: resolved,
})
@ -88,7 +88,7 @@ impl SpecifiedUrl {
/// Creates an already specified url value from an already resolved URL
/// for insertion in the cascade.
pub fn for_cascade(url: ServoUrl) -> Self {
SpecifiedUrl {
CssUrl {
original: None,
resolved: Some(url),
}
@ -96,21 +96,21 @@ impl SpecifiedUrl {
/// Gets a new url from a string for unit tests.
pub fn new_for_testing(url: &str) -> Self {
SpecifiedUrl {
CssUrl {
original: Some(Arc::new(url.into())),
resolved: ServoUrl::parse(url).ok(),
}
}
}
impl Parse for SpecifiedUrl {
impl Parse for CssUrl {
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
let url = input.expect_url()?;
Self::parse_from_string(url.as_ref().to_owned(), context)
}
}
impl PartialEq for SpecifiedUrl {
impl PartialEq for CssUrl {
fn eq(&self, other: &Self) -> bool {
// TODO(emilio): maybe we care about equality of the specified values if
// present? Seems not.
@ -118,9 +118,9 @@ impl PartialEq for SpecifiedUrl {
}
}
impl Eq for SpecifiedUrl {}
impl Eq for CssUrl {}
impl ToCss for SpecifiedUrl {
impl ToCss for CssUrl {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
@ -142,6 +142,9 @@ impl ToCss for SpecifiedUrl {
}
}
/// A specified url() value for servo.
pub type SpecifiedUrl = CssUrl;
impl ToComputedValue for SpecifiedUrl {
type ComputedValue = ComputedUrl;
@ -174,7 +177,7 @@ impl ToComputedValue for SpecifiedUrl {
}
/// A specified image url() value for servo.
pub type SpecifiedImageUrl = SpecifiedUrl;
pub type SpecifiedImageUrl = CssUrl;
/// The computed value of a CSS `url()`, resolved relative to the stylesheet URL.
#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]