Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement ToCss serialization for CSSRules #14238
Conversation
highfive
commented
Nov 15, 2016
|
Heads up! This PR modifies the following files: |
highfive
commented
Nov 15, 2016
| @@ -92,6 +93,50 @@ declare_viewport_descriptor! { | |||
| Orientation(Orientation), | |||
| } | |||
|
|
|||
| impl ToCss for ViewportDescriptor { | |||
This comment has been minimized.
This comment has been minimized.
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.
This comment has been minimized.
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.
|
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) |
|
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
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://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.
This comment has been minimized.
Manishearth
Nov 15, 2016
Member
It should be @media (queries...) {rules...}. You're missing the parentheses.
| @@ -92,6 +93,50 @@ declare_viewport_descriptor! { | |||
| Orientation(Orientation), | |||
| } | |||
|
|
|||
| impl ToCss for ViewportDescriptor { | |||
This comment has been minimized.
This comment has been minimized.
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.
|
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 |
d4fba90
to
0254452
|
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. |
|
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.
This comment has been minimized.
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.
This comment has been minimized.
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 :)
|
Rebased it over master. |
|
@bors-servo r+ thanks! |
|
|
|
@bors-servo p=22 |
|
|
|
Added as_str function to |
|
@bors-servo r+ (you can r=manishearth this yourself fwiw) |
|
|
|
oh, you can't, I delegated the other PR but not this @bors-servo delegate+ |
|
|
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 retry |
|
|
|
|
highfive
commented
Nov 18, 2016
|
|
@bors-servo retry |
|
|
|
|
|
|
| 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(\"")); |
canova commentedNov 15, 2016
•
edited by larsbergstrom
Implementation of ToCss serialization for CSSRules. It requires #14190 to merge first to uncomment
CssRule::Stylebranch in CssRule's ToCss implementation.r? @Manishearth
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThese 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