Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ToCss serialization for CSSRules #14238

Merged
merged 1 commit into from Nov 18, 2016
Merged

Conversation

@canova
Copy link
Member

canova commented Nov 15, 2016

Implementation of ToCss serialization for CSSRules. It requires #14190 to merge first to uncomment CssRule::Style branch in CssRule's ToCss implementation.

r? @Manishearth


  • ./mach build -d does not report any errors

  • ./mach test-tidy does not report any errors

  • These changes fix #14195 (github issue number if applicable).

  • These changes do not require tests because it's serialization changes and I'm not sure there is a test for that.


This change is Reviewable

@highfive
Copy link

highfive commented Nov 15, 2016

Heads up! This PR modifies the following files:

  • @bholley: components/style/stylesheets.rs, components/style/keyframes.rs, components/style/font_face.rs, components/style/viewport.rs
  • @emilio: components/style/stylesheets.rs, components/style/keyframes.rs, components/style/font_face.rs, components/style/viewport.rs
@highfive
Copy link

highfive commented Nov 15, 2016

warning Warning warning

  • These commits modify style code, but no tests are modified. Please consider adding a test!
@@ -92,6 +93,50 @@ declare_viewport_descriptor! {
Orientation(Orientation),
}

impl ToCss for ViewportDescriptor {

This comment has been minimized.

Copy link
@canova

canova Nov 15, 2016

Author Member

ViewportDescriptor is being created with a macro but I couldn't find a way to generalize this code within macro. So I moved outside of it. Should I move it inside of macro?

This comment has been minimized.

Copy link
@Manishearth

Manishearth Nov 15, 2016

Member

Yes, please extend the macro. It will need to take in a string as well, so the invocation will be something like

 declare_viewport_descriptor! {
     "min-width": MinWidth(ViewportLength),
     "max-width": MaxWidth(ViewportLength),
...

You could also do

 declare_viewport_descriptor! {
     min-width: MinWidth(ViewportLength),
     max-width: MaxWidth(ViewportLength),
...

and use stringify!() on the identifiers. It may be easy to make the macro follow sets work that way. If you're having trouble, I can fix this up for you, so don't worry too much about it.

@Manishearth
Copy link
Member

Manishearth commented Nov 15, 2016

This needs spec links for the bits that are specced. For the bits that aren't, leave a comment saying so (and try to make a PR to add some spec text for this)

Copy link
Member

Manishearth left a comment

Overall I suggest you compare with the MDN pages and/or spec closely to ensure that the output will look the same as (any one valid way of providing) the input.

[By "any one valid way" I mean that if different CSS text can produce the same parsed rule, pick the simplest one for the ToCss.]

try!(dest.write_str("; src: "));

let mut iter = self.sources.iter();
try!(iter.next().unwrap().to_css(dest));

This comment has been minimized.

Copy link
@Manishearth

Manishearth Nov 15, 2016

Member

It's valid for font face rules to not have sources, in which case there should be no src block.

@@ -82,6 +84,18 @@ pub struct Keyframe {
pub block: Arc<RwLock<PropertyDeclarationBlock>>,
}

impl ToCss for Keyframe {

This comment has been minimized.

Copy link
@Manishearth

Manishearth Nov 15, 2016

Member

This doesn't write out the property declaration block.

https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes

A single keyframe rule looks like 68%, 72% { left: 50px; top: 50px; }. So you first print out a comma-separated list of percentages, and then serialize the block. I think you'll need to add the braces yourself.

fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
CssRule::Namespace(ref lock) => try!(lock.read().to_css(dest)),
// TODO: Uncomment here when Manishearth's CSSOM PR(#14190) lands

This comment has been minimized.

Copy link
@Manishearth

Manishearth Nov 15, 2016

Member

Just base it on top of the CSSOM PR for now, and update the css_text getters to use this (there already is commented code to use this)

try!(dest.write_str(&*prefix));
try!(dest.write_str(" "));
}
try!(dest.write_str(&*self.url));

This comment has been minimized.

Copy link
@Manishearth

Manishearth Nov 15, 2016

Member

We must wrap this in url("..."). A plain quoted string will do too, but Firefox picks the quoted URL.

https://developer.mozilla.org/en-US/docs/Web/CSS/@namespace

// https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSMediaRule
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("@media "));
try!(self.media_queries.read().to_css(dest));

This comment has been minimized.

Copy link
@Manishearth

Manishearth Nov 15, 2016

Member

It should be @media (queries...) {rules...}. You're missing the parentheses.

https://developer.mozilla.org/en-US/docs/Web/CSS/@media

@@ -92,6 +93,50 @@ declare_viewport_descriptor! {
Orientation(Orientation),
}

impl ToCss for ViewportDescriptor {

This comment has been minimized.

Copy link
@Manishearth

Manishearth Nov 15, 2016

Member

Yes, please extend the macro. It will need to take in a string as well, so the invocation will be something like

 declare_viewport_descriptor! {
     "min-width": MinWidth(ViewportLength),
     "max-width": MaxWidth(ViewportLength),
...

You could also do

 declare_viewport_descriptor! {
     min-width: MinWidth(ViewportLength),
     max-width: MaxWidth(ViewportLength),
...

and use stringify!() on the identifiers. It may be easy to make the macro follow sets work that way. If you're having trouble, I can fix this up for you, so don't worry too much about it.

@canova canova force-pushed the canova:cssom-tocss branch from 5c00e72 to 10bbf95 Nov 16, 2016
@canova
Copy link
Member Author

canova commented Nov 16, 2016

Oops sorry for the mistakes. Pushed the updated codes.

Also updated the macro rule, but I'm not so good with macros so maybe it can be improved :) I tried to make it like min-width: MinWidth(ViewportLength), but ident doesn't allow dash. And I couldn't use : in the middle because it's not allowed after expr type. I made it like this:
"min-width" => MinWidth(ViewportLength),

@canova canova changed the title [WIP] Implement ToCss serialization for CSSRules Implement ToCss serialization for CSSRules Nov 16, 2016
@canova canova force-pushed the canova:cssom-tocss branch 3 times, most recently from d4fba90 to 0254452 Nov 16, 2016
@Manishearth
Copy link
Member

Manishearth commented Nov 16, 2016

String is fine, actually even better. The reason I was suggesting idents was because the follow rules for idents are more lenient (so you could use a colon or whatever), but I forgot about the dash.

@Manishearth
Copy link
Member

Manishearth commented Nov 17, 2016

btw, my PR landed, you should rebase over master

#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct FontFaceRule {
pub family: FontFamily,
pub sources: Vec<Source>,
}

impl ToCss for FontFaceRule {
// Serialization of FontFaceRule is not specced.

This comment has been minimized.

Copy link
@Manishearth

Manishearth Nov 17, 2016

Member

I wanted to check if the whitespace matched browsers, and Chrome and Firefox don't match (this is unspecced and probably unimportant so this is ok). Chrome produces @font-face { font-family: foo; src: url("bar"); }, with the same whitespace as the (specced) CSSStyleRule serialization. Firefox produces the nice-looking

@font-face {
  font-family: "foo";
  src: url("bar");
}

with newlines.

Since the rules with specced serialization just have spaces around each brace and after each semicolon, I think we should just stick with that (And if you're interested in speccing the rest, do the same then). This also seems to be what you've done here anyway, yay 😄

This comment has been minimized.

Copy link
@canova

canova Nov 17, 2016

Author Member

Firefox's way looks better but it's not specified this way in specced rules as you said. I think it's better to continue with the existing rule style too :)

@canova canova force-pushed the canova:cssom-tocss branch from 0254452 to d4989f4 Nov 17, 2016
@canova
Copy link
Member Author

canova commented Nov 17, 2016

Rebased it over master.

@Manishearth
Copy link
Member

Manishearth commented Nov 17, 2016

@bors-servo r+

thanks!

@bors-servo
Copy link
Contributor

bors-servo commented Nov 17, 2016

📌 Commit b71e122 has been approved by Manishearth

@Manishearth
Copy link
Member

Manishearth commented Nov 17, 2016

@bors-servo
Copy link
Contributor

bors-servo commented Nov 17, 2016

The latest upstream changes (presumably #14246) made this pull request unmergeable. Please resolve the merge conflicts.

@Manishearth Manishearth force-pushed the canova:cssom-tocss branch from b71e122 to 5bb7175 Nov 17, 2016
@canova canova force-pushed the canova:cssom-tocss branch from 5bb7175 to fdbadcd Nov 18, 2016
@canova
Copy link
Member Author

canova commented Nov 18, 2016

Added as_str function to SpecifiedUrl. It should work now.

@Manishearth
Copy link
Member

Manishearth commented Nov 18, 2016

@bors-servo r+

(you can r=manishearth this yourself fwiw)

@bors-servo
Copy link
Contributor

bors-servo commented Nov 18, 2016

📌 Commit fdbadcd has been approved by Manishearth

@Manishearth
Copy link
Member

Manishearth commented Nov 18, 2016

oh, you can't, I delegated the other PR but not this

@bors-servo delegate+

@bors-servo
Copy link
Contributor

bors-servo commented Nov 18, 2016

✌️ @canaltinova can now approve this pull request

@bors-servo
Copy link
Contributor

bors-servo commented Nov 18, 2016

Testing commit fdbadcd with merge 731264f...

bors-servo added a commit that referenced this pull request Nov 18, 2016
Implement ToCss serialization for CSSRules

<!-- Please describe your changes on the following line: -->
Implementation of ToCss serialization for CSSRules. It requires #14190 to merge first to uncomment `CssRule::Style` branch in CssRule's ToCss implementation.

r? @Manishearth

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #14195 (github issue number if applicable).

- [X] These changes do not require tests because it's serialization changes and I'm not sure there is a test for that.

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14238)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Nov 18, 2016

💔 Test failed - linux-rel-wpt

@canova
Copy link
Member Author

canova commented Nov 18, 2016

@bors-servo
Copy link
Contributor

bors-servo commented Nov 18, 2016

Previous build results for arm32, arm64, linux-dev, linux-rel-css, mac-dev-unit, mac-rel-css, mac-rel-wpt2, windows-dev are reusable. Rebuilding only linux-rel-wpt, mac-rel-wpt1...

@bors-servo
Copy link
Contributor

bors-servo commented Nov 18, 2016

💔 Test failed - linux-rel-wpt

@highfive
Copy link

highfive commented Nov 18, 2016

  ▶ FAIL [expected PASS] /_mozilla/css/incremental_trailing_whitespace_a.html
  └   → /_mozilla/css/incremental_trailing_whitespace_a.html da54041086f2975f8e3c776d2283ad6609e6862a
/_mozilla/css/incremental_trailing_whitespace_ref.html f5d0147c8bddbdc772a7e5e86c7c9e433fcd486b
Testing da54041086f2975f8e3c776d2283ad6609e6862a == f5d0147c8bddbdc772a7e5e86c7c9e433fcd486b
@canova
Copy link
Member Author

canova commented Nov 18, 2016

@bors-servo
Copy link
Contributor

bors-servo commented Nov 18, 2016

Previous build results for arm32, arm64, linux-dev, linux-rel-css, mac-dev-unit, mac-rel-css, mac-rel-wpt2, windows-dev are reusable. Rebuilding only linux-rel-wpt, mac-rel-wpt1...

@bors-servo
Copy link
Contributor

bors-servo commented Nov 18, 2016

@bors-servo bors-servo merged commit fdbadcd into servo:master Nov 18, 2016
3 checks passed
3 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
@canova
Copy link
Member Author

canova commented Nov 18, 2016

🎉 🎉 🎉

@canova canova deleted the canova:cssom-tocss branch Nov 18, 2016
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
Source::Url(ref url) => {
try!(dest.write_str("local(\""));

This comment has been minimized.

Copy link
@mark-buer

mark-buer Nov 18, 2016

Too late now... but are these ("local(" and "url(") around the wrong way?

This comment has been minimized.

Copy link
@canova

canova Nov 18, 2016

Author Member

Oh god... I must have mixed it during rebase or something 😞

This comment has been minimized.

Copy link
@Manishearth

Manishearth Nov 18, 2016

Member

Fix included in #14241. Good catch!

Manishearth added a commit to Manishearth/servo that referenced this pull request Nov 18, 2016
Manishearth added a commit to Manishearth/servo that referenced this pull request Nov 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

5 participants
You can’t perform that action at this time.