Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
servo: Merge #17538 - Implement CSSStyleRule.selectorText (from jyc:s…
Browse files Browse the repository at this point in the history
…electorText); r=Manishearth

We parse when assigning using the namespaces of the stylesheet. It isn't
clear if the spec says to do that (Firefox doesn't support the setter at
all, Chrome does, Safari doesn't); the spec issue is here:
w3c/csswg-drafts#1511

Also fix ToCss implementation of AttrSelectorOperator to not pad with
spaces, to conform with CSSOM. This means we have to update some unit
tests that expect operators with spaces around them in attribute
selectors to roundtrip.

See the "attribute selector" section of "Serializing Selectors" here:
https://drafts.csswg.org/cssom/#serializing-selectors

CSSStyleRule.selectorText is specified here:
https://drafts.csswg.org/cssom/#dom-cssstylerule-selectortext

<!-- Please describe your changes on the following line: -->

---
<!-- 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 is part of a series to fix #17182

<!-- Either: -->
I am running this to identify what tests will fail; as the other PRs in the series are merged, I believe the tests that pass will change as well.

- [ ] 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. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: 4b6e79337ef975e3ec752513b76ae6a6284c1199
  • Loading branch information
jyc committed Jul 12, 2017
1 parent 6bce7c2 commit 823b3aa
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
2 changes: 2 additions & 0 deletions servo/components/script/dom/cssstyledeclaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ impl CSSStyleOwner {
f(&mut *pdb.write_with(&mut guard), &mut changed)
};
if changed {
// If this is changed, see also
// CSSStyleRule::SetSelectorText, which does the same thing.
rule.global().as_window().Document().invalidate_stylesheets();
}
result
Expand Down
37 changes: 36 additions & 1 deletion servo/components/script/dom/cssstylerule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use cssparser::{Parser as CssParser, ParserInput as CssParserInput};
use cssparser::ToCss;
use dom::bindings::codegen::Bindings::CSSStyleRuleBinding::{self, CSSStyleRuleMethods};
use dom::bindings::codegen::Bindings::WindowBinding::WindowBinding::WindowMethods;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, MutNullableJS, Root};
use dom::bindings::reflector::{DomObject, reflect_dom_object};
Expand All @@ -12,9 +15,12 @@ use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSSt
use dom::cssstylesheet::CSSStyleSheet;
use dom::window::Window;
use dom_struct::dom_struct;
use selectors::parser::SelectorList;
use std::mem;
use style::selector_parser::SelectorParser;
use style::shared_lock::{Locked, ToCssWithGuard};
use style::stylearc::Arc;
use style::stylesheets::StyleRule;
use style::stylesheets::{StyleRule, Origin};

#[dom_struct]
pub struct CSSStyleRule {
Expand Down Expand Up @@ -71,4 +77,33 @@ impl CSSStyleRuleMethods for CSSStyleRule {
)
})
}

// https://drafts.csswg.org/cssom/#dom-cssstylerule-selectortext
fn SelectorText(&self) -> DOMString {
let guard = self.cssrule.shared_lock().read();
let stylerule = self.stylerule.read_with(&guard);
return DOMString::from_string(stylerule.selectors.to_css_string());
}

// https://drafts.csswg.org/cssom/#dom-cssstylerule-selectortext
fn SetSelectorText(&self, value: DOMString) {
// It's not clear from the spec if we should use the stylesheet's namespaces.
// https://github.com/w3c/csswg-drafts/issues/1511
let namespaces = self.cssrule.parent_stylesheet().style_stylesheet().contents.namespaces.read();
let parser = SelectorParser {
stylesheet_origin: Origin::Author,
namespaces: &namespaces,
};
let mut css_parser = CssParserInput::new(&*value);
let mut css_parser = CssParser::new(&mut css_parser);
if let Ok(mut s) = SelectorList::parse(&parser, &mut css_parser) {
// This mirrors what we do in CSSStyleOwner::mutate_associated_block.
let mut guard = self.cssrule.shared_lock().write();
let mut stylerule = self.stylerule.write_with(&mut guard);
mem::swap(&mut stylerule.selectors, &mut s);
// It seems like we will want to avoid having to invalidate all
// stylesheets eventually!
self.global().as_window().Document().invalidate_stylesheets();
}
}
}
2 changes: 1 addition & 1 deletion servo/components/script/dom/webidls/CSSStyleRule.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// https://drafts.csswg.org/cssom/#the-cssstylerule-interface
[Exposed=Window]
interface CSSStyleRule : CSSRule {
// attribute DOMString selectorText;
attribute DOMString selectorText;
[SameObject, PutForwards=cssText] readonly attribute CSSStyleDeclaration style;
};
14 changes: 8 additions & 6 deletions servo/components/selectors/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ pub enum AttrSelectorOperator {

impl ToCss for AttrSelectorOperator {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
// https://drafts.csswg.org/cssom/#serializing-selectors
// See "attribute selector".
dest.write_str(match *self {
AttrSelectorOperator::Equal => " = ",
AttrSelectorOperator::Includes => " ~= ",
AttrSelectorOperator::DashMatch => " |= ",
AttrSelectorOperator::Prefix => " ^= ",
AttrSelectorOperator::Substring => " *= ",
AttrSelectorOperator::Suffix => " $= ",
AttrSelectorOperator::Equal => "=",
AttrSelectorOperator::Includes => "~=",
AttrSelectorOperator::DashMatch => "|=",
AttrSelectorOperator::Prefix => "^=",
AttrSelectorOperator::Substring => "*=",
AttrSelectorOperator::Suffix => "$=",
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions servo/components/selectors/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ pub mod tests {
].into_boxed_slice())
), specificity(0, 0, 1))
))));
assert_eq!(parse("[attr |= \"foo\"]"), Ok(SelectorList::from_vec(vec!(
assert_eq!(parse("[attr|=\"foo\"]"), Ok(SelectorList::from_vec(vec!(
Selector::from_vec(vec!(
Component::AttributeInNoNamespace {
local_name: DummyAtom::from("attr"),
Expand Down Expand Up @@ -2012,7 +2012,7 @@ pub mod tests {
parser.default_ns = None;
assert!(parse(":not(#provel.old)").is_err());
assert!(parse(":not(#provel > old)").is_err());
assert!(parse("table[rules]:not([rules = \"none\"]):not([rules = \"\"])").is_ok());
assert!(parse("table[rules]:not([rules=\"none\"]):not([rules=\"\"])").is_ok());
assert_eq!(parse(":not(#provel)"), Ok(SelectorList::from_vec(vec!(
Selector::from_vec(vec!(Component::Negation(vec!(
Component::ID(DummyAtom::from("provel")),
Expand Down
2 changes: 1 addition & 1 deletion servo/tests/unit/style/parsing/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ fn test_selectors() {
assert_roundtrip!(parse_selector, "div");
assert_roundtrip!(parse_selector, "svg|circle");
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");
}

0 comments on commit 823b3aa

Please sign in to comment.