Auto merge of #18344 - bradwerth:selectorSerialize, r=SimonSapin

Change selector::to_css function to handle combinators in between universal selectors.

https://bugzilla.mozilla.org/show_bug.cgi?id=1391169
https://reviewboard.mozilla.org/r/172368/

---
<!-- 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
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- 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/18344)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-09-01 23:41:10 -05:00 committed by GitHub
commit fd95696d31
2 changed files with 18 additions and 12 deletions

View file

@ -787,8 +787,8 @@ impl<Impl: SelectorImpl> ToCss for Selector<Impl> {
// something like `... > ::before`, because we store `>` and `::` // something like `... > ::before`, because we store `>` and `::`
// both as combinators internally. // both as combinators internally.
// //
// If we are in this case, we continue to the next iteration of the // If we are in this case, after we have serialized the universal
// `for compound in compound_selectors` loop. // selector, we skip Step 2 and continue with the algorithm.
let (can_elide_namespace, first_non_namespace) = match &compound[0] { let (can_elide_namespace, first_non_namespace) = match &compound[0] {
&Component::ExplicitAnyNamespace | &Component::ExplicitAnyNamespace |
&Component::ExplicitNoNamespace | &Component::ExplicitNoNamespace |
@ -796,6 +796,7 @@ impl<Impl: SelectorImpl> ToCss for Selector<Impl> {
&Component::DefaultNamespace(_) => (true, 1), &Component::DefaultNamespace(_) => (true, 1),
_ => (true, 0), _ => (true, 0),
}; };
let mut perform_step_2 = true;
if first_non_namespace == compound.len() - 1 { if first_non_namespace == compound.len() - 1 {
match (combinators.peek(), &compound[first_non_namespace]) { match (combinators.peek(), &compound[first_non_namespace]) {
// We have to be careful here, because if there is a pseudo // We have to be careful here, because if there is a pseudo
@ -811,7 +812,8 @@ impl<Impl: SelectorImpl> ToCss for Selector<Impl> {
for simple in compound.iter() { for simple in compound.iter() {
simple.to_css(dest)?; simple.to_css(dest)?;
} }
continue // Skip step 2, which is an "otherwise".
perform_step_2 = false;
} }
(_, _) => (), (_, _) => (),
} }
@ -826,6 +828,7 @@ impl<Impl: SelectorImpl> ToCss for Selector<Impl> {
// proposing to change this to match up with the behavior asserted // proposing to change this to match up with the behavior asserted
// in cssom/serialize-namespaced-type-selectors.html, which the // in cssom/serialize-namespaced-type-selectors.html, which the
// following code tries to match. // following code tries to match.
if perform_step_2 {
for simple in compound.iter() { for simple in compound.iter() {
if let Component::ExplicitUniversalType = *simple { if let Component::ExplicitUniversalType = *simple {
// Can't have a namespace followed by a pseudo-element // Can't have a namespace followed by a pseudo-element
@ -839,6 +842,7 @@ impl<Impl: SelectorImpl> ToCss for Selector<Impl> {
simple.to_css(dest)?; simple.to_css(dest)?;
} }
} }
}
// 3. If this is not the last part of the chain of the selector // 3. If this is not the last part of the chain of the selector
// append a single SPACE (U+0020), followed by the combinator // append a single SPACE (U+0020), followed by the combinator

View file

@ -25,4 +25,6 @@ fn test_selectors() {
assert_roundtrip!(parse_selector, "svg|circle"); assert_roundtrip!(parse_selector, "svg|circle");
assert_roundtrip!(parse_selector, "p:before", "p::before"); assert_roundtrip!(parse_selector, "p:before", "p::before");
assert_roundtrip!(parse_selector, "[border=\"0\"]:-servo-nonzero-border ~ ::-servo-details-summary"); assert_roundtrip!(parse_selector, "[border=\"0\"]:-servo-nonzero-border ~ ::-servo-details-summary");
assert_roundtrip!(parse_selector, "* > *");
assert_roundtrip!(parse_selector, "*|* + *", "* + *");
} }