Page MenuHomePhabricator

Bug 1501261 - Part 2: Serialize `background-size: auto auto` as "auto"
ClosedPublic

Authored by heycam on Nov 1 2018, 12:16 AM.

Details

Reviewers
emilio
Group Reviewers
Restricted Project
Commits
Restricted Diffusion Commit
rMOZILLACENTRALe63ddbf2a290: Bug 1501261 - Part 2: Serialize `background-size: auto auto` as "auto"…
Bugzilla Bug ID
1501261
Summary

With this change, all of Chrome, Edge, Firefox, and Safari serialize
background-size by omitting the second "auto" if the value is "auto
auto". Other keywords are still repeated.

Depends on D10445

Diff Detail

Repository
rMOZILLACENTRAL mozilla-central
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

heycam created this revision.Nov 1 2018, 12:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 1 2018, 12:16 AM
phab-bot changed the visibility from "Custom Policy" to "Public (No Login Required)".Nov 1 2018, 12:16 AM
phab-bot changed the edit policy from "Custom Policy" to "Restricted Project (Project)".
phab-bot removed a project: Restricted Project.
heycam requested review of this revision.Nov 1 2018, 12:16 AM
emilio added a comment.Nov 1 2018, 1:30 AM

I think this one is a bit unfortunate... But why not shortening all of them? That is, something like:

impl<LengthOrPercentageOrAuto> ToCss for BackgroundSize<LengthOrPercentageOrAuto>
where
    LengthOrPercentageOrAuto: ToCss + PartialEq,
{
    pub fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        match self {
            BackgroundSize::Explicit { width, height } => {
                width.to_css(dest)?;
                if width != height {
                    dest.write_str(" ")?;
                    height.to_css(dest)
                }
            }
            BackgroundSize::Cover => dest.write_str("cover"),
            BackgroundSize::Contain => dest.write_str("contain"),
        }
    }
}
heycam added a comment.EditedNov 1 2018, 1:47 AM

But why not shortening all of them?

Just because all other browsers are consistent about shortening only "auto auto" and not for any other value.

heycam added a comment.Nov 1 2018, 1:52 AM

What's more unfortunate (IMO) is that it's impossible (or difficult enough that I didn't work out how) to add a trait bound on generic::BackgroundSize's LengthPercentageOrAuto type parameter for, say, an IsAuto trait, and then just add a #[css(skip_if = "is_auto")] on the height field or something.

emilio added a comment.Nov 1 2018, 2:14 AM

But why not shortening all of them?

Just because all other browsers are consistent about shortening only "auto auto" and not for any other value.

That's too bad :(

What's more unfortunate (IMO) is that it's impossible (or difficult enough that I didn't work out how) to add a trait bound on generic::BackgroundSize's LengthPercentageOrAuto type parameter for, say, an IsAuto trait, and then just add a #[css(skip_if = "is_auto")] on the height field or something.

That'd be wrong though, since you could have stretch auto or something like that, right? You'd need an skip_if_self or something of the sort... It shouldn't be hard to add a way to add extra bounds to the impl either, something like this should work:

https://gist.github.com/emilio/421418d12864929a7d0d9c4a98ff7ccb

Now there's the question of how useful it would generally be...

I think the extra_bounds bit should not be needed, right? enum BackgroundSize<LengthOrPercentageOrAuto: IsAuto> should work without that, right? The #[derive] code I think parses and understands that.

servo/components/style/values/generics/background.rs
43

Probably (even if it's not derived) something like:

impl<LengthOrPercentageOrAuto> ToCss for BackgroundSize<LengthOrPercentageOrAuto>
where
    LengthOrPercentageOrAuto: ToCss + IsAuto,
{

And implementing IsAuto may be a bit more preferable than the _helper function plus forcing the callers to check, wdyt?

heycam added a comment.Nov 1 2018, 2:18 AM

What's more unfortunate (IMO) is that it's impossible (or difficult enough that I didn't work out how) to add a trait bound on generic::BackgroundSize's LengthPercentageOrAuto type parameter for, say, an IsAuto trait, and then just add a #[css(skip_if = "is_auto")] on the height field or something.

That'd be wrong though, since you could have stretch auto or something like that, right? You'd need an skip_if_self or something of the sort...

Yeah, true.

It shouldn't be hard to add a way to add extra bounds to the impl either, something like this should work:

https://gist.github.com/emilio/421418d12864929a7d0d9c4a98ff7ccb

Cool.

I think the extra_bounds bit should not be needed, right? enum BackgroundSize<LengthOrPercentageOrAuto: IsAuto> should work without that, right? The #[derive] code I think parses and understands that.

I couldn't get it to work.

heycam marked an inline comment as done.Nov 1 2018, 2:20 AM
heycam added inline comments.
servo/components/style/values/generics/background.rs
43

If that's possible I would much prefer it too! Please let me know how to do this because I got various errors from other derived traits not having the right bounds.

emilio added a comment.Nov 1 2018, 2:55 PM

Seems to work, wdyt about that? It's neutral in amount of lines, but I think it's a bit nicer.

Seems to work, wdyt about that? It's neutral in amount of lines, but I think it's a bit nicer.

Huh, I thought I tried pretty much exactly the same thing and ran into troubles!

I bet the problem was that I was also trying to add the IsAuto trait bound to the BackgroundSize enum itself, and that was tripping up the derived traits.

heycam updated this revision to Diff 31622.Nov 4 2018, 10:36 PM
emilio accepted this revision.Nov 4 2018, 10:42 PM

Yeah, I can see the bound on the enum causing havoc :)

servo/components/style/values/generics/background.rs
46

I guess this comment should be removed now :)

This revision is now accepted and ready to land.Nov 4 2018, 10:42 PM
heycam updated this revision to Diff 31635.Nov 5 2018, 2:21 AM
This revision was automatically updated to reflect the committed changes.