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
style: :dir() pseudo class now represented by enum #19195
Conversation
Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @mbrubeck (or someone else) soon. |
Heads up! This PR modifies the following files:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good start!
I have a bunch of comments, let me know if any of those is unclear and I'm happy to clarify or help out.
Thanks for working on this!
pub enum Direction { | ||
Rtl, | ||
Ltr, | ||
Other(String), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should still be Box<[u16]>
, or Box<str>
at least. That may avoid Component
growing.
@@ -14,8 +14,8 @@ use visitor::SelectorVisitor; | |||
size_of_test!(size_of_selector, Selector<Impl>, 8); | |||
size_of_test!(size_of_pseudo_element, gecko_like_types::PseudoElement, 24); | |||
|
|||
size_of_test!(size_of_component, Component<Impl>, 32); | |||
size_of_test!(size_of_pseudo_class, PseudoClass, 24); | |||
size_of_test!(size_of_component, Component<Impl>, 48); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Growing Component
is not really acceptable I think :(
/// left-to-right semantic directionality | ||
Ltr, | ||
/// Some other provided directionality value | ||
Other(String), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
Other(String), | ||
} | ||
|
||
impl <'a> From<&'a str> for Direction { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for From<&'a str>
, let's use the Parse
trait.
|
||
impl <'a> From<&'a str> for Direction { | ||
fn from(string: &'a str) -> Direction { | ||
match string.to_lowercase().as_str() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to allocate here, let's use match_ignore_ascii_case
.
@@ -360,6 +417,10 @@ impl<'a, 'i> ::selectors::Parser<'i> for SelectorParser<'a> { | |||
.chain(Some(0u16)).collect(); | |||
NonTSPseudoClass::$k_name(utf16.into_boxed_slice()) | |||
}, )* | |||
"dir" => { | |||
let name: &str = parser.expect_ident()?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add the match
here instead of the From
impl.
components/style/gecko/wrapper.rs
Outdated
@@ -2066,6 +2065,15 @@ impl<'le> ::selectors::Element for GeckoElement<'le> { | |||
) | |||
} | |||
} | |||
NonTSPseudoClass::Dir(ref dir) => { | |||
unsafe { | |||
Gecko_MatchStringArgPseudo( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of converting to an enum
is not having to go through Gecko_MatchStringArgPseudo
.
Instead of that, we can just do:
match *dir {
Direction::Ltr => self.get_state().intersects(element_state::IN_HAS_DIR_ATTR_LTR_STATE),
Direction::Rtl => self.get_state().intersects(element_state::IN_HAS_DIR_ATTR_RTL_STATE),
Direction::Other(..) => false,
}
use invalidation::element::invalidation_map::dir_selector_to_state; | ||
let selector_flag = dir_selector_to_state(s); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, we can simplify dir_selector_to_state
to take &Direction
, instead of comparing the string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should make the Box<[u16]>
impl unnecessary.
tests/unit/stylo/dir_repr.rs
Outdated
@@ -0,0 +1,31 @@ | |||
/* This Source Code Form is subject to the terms of the Mozilla Public |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this, we have tests in mozilla-central for this :)
@@ -14,8 +14,8 @@ use visitor::SelectorVisitor; | |||
size_of_test!(size_of_selector, Selector<Impl>, 8); | |||
size_of_test!(size_of_pseudo_element, gecko_like_types::PseudoElement, 24); | |||
|
|||
size_of_test!(size_of_component, Component<Impl>, 32); | |||
size_of_test!(size_of_pseudo_class, PseudoClass, 24); | |||
size_of_test!(size_of_component, Component<Impl>, 40); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even using the Other<Box[u16]>
enum variant type, the size of these structs is increased :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's pretty unfortunate, let's box the Direction
then:
Dir(Box<Direction>)
Mentioning that it allows not growing Component
, even though it's slightly unfortunate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, the other thing we can do instead, and it'll fix the serialization bug I just noted below too, I think, is using CustomIdent
, which should just be one word and allow us not to box it:
enum Direction {
Rtl,
Ltr,
Other(CustomIdent),
}
Then you don't even need to manually write the serialization code, and you can just #[derive(ToCss)]
on it.
Hey, @emilio, thanks for the review! I think I've addressed your comments, with the exception of the change in size of Do you have ideas regarding how to eliminate the size increase on that struct? |
components/style/selector_parser.rs
Outdated
Other(Box<[u16]>), | ||
} | ||
|
||
impl<'a> From<&'a Direction> for String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given we don't use the utf16 impl anyway, this could be Box<str>
, and there'd be no need to allocate a String
for serialization.
@@ -14,8 +14,8 @@ use visitor::SelectorVisitor; | |||
size_of_test!(size_of_selector, Selector<Impl>, 8); | |||
size_of_test!(size_of_pseudo_element, gecko_like_types::PseudoElement, 24); | |||
|
|||
size_of_test!(size_of_component, Component<Impl>, 32); | |||
size_of_test!(size_of_pseudo_class, PseudoClass, 24); | |||
size_of_test!(size_of_component, Component<Impl>, 40); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's pretty unfortunate, let's box the Direction
then:
Dir(Box<Direction>)
Mentioning that it allows not growing Component
, even though it's slightly unfortunate.
@@ -92,6 +94,12 @@ impl ToCss for NonTSPseudoClass { | |||
dest.write_str(&value)?; | |||
return dest.write_char(')') | |||
}, )* | |||
NonTSPseudoClass::Dir(ref dir) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's impl ToCss for Direction
, and use that here, doing just:
dest.write_str(":dir(")?;
dir.to_css(dest)?;
dest.write_char(')')
NonTSPseudoClass::Dir(ref dir) => { | ||
let value: String = dir.into(); | ||
dest.write_str(concat!(":dir("))?; | ||
dest.write_str(&value)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I believe this is not correct, and should do escaping for the identifier... Though it's an existing bug.
@@ -14,8 +14,8 @@ use visitor::SelectorVisitor; | |||
size_of_test!(size_of_selector, Selector<Impl>, 8); | |||
size_of_test!(size_of_pseudo_element, gecko_like_types::PseudoElement, 24); | |||
|
|||
size_of_test!(size_of_component, Component<Impl>, 32); | |||
size_of_test!(size_of_pseudo_class, PseudoClass, 24); | |||
size_of_test!(size_of_component, Component<Impl>, 40); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, the other thing we can do instead, and it'll fix the serialization bug I just noted below too, I think, is using CustomIdent
, which should just be one word and allow us not to box it:
enum Direction {
Rtl,
Ltr,
Other(CustomIdent),
}
Then you don't even need to manually write the serialization code, and you can just #[derive(ToCss)]
on it.
@emilio Thanks for the follow-up. Regarding your comment regarding escaping: do you have a link to an issue about existing escaping shortcomings and/or thoughts on how to proceed? Your attention and patience on this PR is much appreciated! At this point it probably would have been faster for you to implement a fix yourself than to explain all these things to me :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
r=me with these two nits and the commits squashed. Thanks a lot for working on this!
@@ -92,6 +94,11 @@ impl ToCss for NonTSPseudoClass { | |||
dest.write_str(&value)?; | |||
return dest.write_char(')') | |||
}, )* | |||
NonTSPseudoClass::Dir(ref dir) => { | |||
dest.write_str(concat!(":dir("))?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there a need for concat!
here?
let dir_str = match *self { | ||
Direction::Rtl => "rtl", | ||
Direction::Ltr => "ltr", | ||
Direction::Other(ref other) => other, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a FIXME
here saying:
FIXME: This should be escaped as an identifier.
That can be a followup if you want, since that's a bugfix, requires tests and all that.
The short version is that it should use serialize_identifier
, in order to properly handle escapes. I believe we have tests that test this where you could grab an example... But looking at what serialize_identifier
handles, something like this:
:dir(\-) { .. }
Should be a valid rule, but serializes back to:
:dir(-) { .. }
Which isn't an identifier token IIRC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #19231 which I can pick up as a followup.
d0b0a6b
to
10f3ef4
Compare
@emilio are there any other changes you'd like to see before this is mergeable? |
@@ -92,6 +94,12 @@ impl ToCss for NonTSPseudoClass { | |||
dest.write_str(&value)?; | |||
return dest.write_char(')') | |||
}, )* | |||
NonTSPseudoClass::Dir(ref dir) => { | |||
dest.write_str(":dir(")?; | |||
// FIXME: This should be escaped as an identifier; see #19231 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment should probably go in the to_css
impl, but that can be done in a followup PR.
Nope, sorry! I had missed the latest update. Feel free to ping me sooner if this happens again. @bors-servo r+ |
📌 Commit 10f3ef4 has been approved by |
Looks great, thanks a lot! |
style: :dir() pseudo class now represented by enum `:dir()` pseudo class param now represented as enum variants. --- - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #16840 - [X] There are tests for these changes <!-- 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/19195) <!-- Reviewable:end -->
☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev |
moving :dir() param serialization FIXME <!-- Please describe your changes on the following line: --> Per emilio's comment, #19195 (comment), a `FIXME` regarding `:dir()` parameter serialization has been moved. --- <!-- 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 do not require tests because they only modify comment lines <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- 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/19332) <!-- Reviewable:end -->
…ilsoniya:moving-fixme); r=emilio <!-- Please describe your changes on the following line: --> Per emilio's comment, servo/servo#19195 (comment), a `FIXME` regarding `:dir()` parameter serialization has been moved. --- <!-- 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 do not require tests because they only modify comment lines <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 3ecd0174cc7817cdd2007850c29b8b069a845b46 --HG-- extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear extra : subtree_revision : bd40b06fc49360f0a8ac206558734ec557c741cc
…ilsoniya:moving-fixme); r=emilio <!-- Please describe your changes on the following line: --> Per emilio's comment, servo/servo#19195 (comment), a `FIXME` regarding `:dir()` parameter serialization has been moved. --- <!-- 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 do not require tests because they only modify comment lines <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 3ecd0174cc7817cdd2007850c29b8b069a845b46
…ilsoniya:moving-fixme); r=emilio <!-- Please describe your changes on the following line: --> Per emilio's comment, servo/servo#19195 (comment), a `FIXME` regarding `:dir()` parameter serialization has been moved. --- <!-- 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 do not require tests because they only modify comment lines <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 3ecd0174cc7817cdd2007850c29b8b069a845b46
…ilsoniya:moving-fixme); r=emilio <!-- Please describe your changes on the following line: --> Per emilio's comment, servo/servo#19195 (comment), a `FIXME` regarding `:dir()` parameter serialization has been moved. --- <!-- 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 do not require tests because they only modify comment lines <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 3ecd0174cc7817cdd2007850c29b8b069a845b46
…ilsoniya:moving-fixme); r=emilio <!-- Please describe your changes on the following line: --> Per emilio's comment, servo/servo#19195 (comment), a `FIXME` regarding `:dir()` parameter serialization has been moved. --- <!-- 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 do not require tests because they only modify comment lines <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 3ecd0174cc7817cdd2007850c29b8b069a845b46 UltraBlame original commit: 84b90d871c2c14c24356f2840b5bbe9413a5e729
…ilsoniya:moving-fixme); r=emilio <!-- Please describe your changes on the following line: --> Per emilio's comment, servo/servo#19195 (comment), a `FIXME` regarding `:dir()` parameter serialization has been moved. --- <!-- 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 do not require tests because they only modify comment lines <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 3ecd0174cc7817cdd2007850c29b8b069a845b46 UltraBlame original commit: 84b90d871c2c14c24356f2840b5bbe9413a5e729
…ilsoniya:moving-fixme); r=emilio <!-- Please describe your changes on the following line: --> Per emilio's comment, servo/servo#19195 (comment), a `FIXME` regarding `:dir()` parameter serialization has been moved. --- <!-- 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 do not require tests because they only modify comment lines <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 3ecd0174cc7817cdd2007850c29b8b069a845b46 UltraBlame original commit: 84b90d871c2c14c24356f2840b5bbe9413a5e729
:dir()
pseudo class param now represented as enum variants../mach build -d
does not report any errors./mach test-tidy
does not report any errorsThis change is