Bug 1545823 - Implement non-standard CSSStyleSheet.rules, CSSStyleSheet.addRule and CSSStyleSheet.removeRule. r=bzbarsky

It's not worth dying on this hill. Both Blink and WebKit pass the tests.

(Well, WebKit actually fails one of the latest ones I wrote, cssRules and rules
are not the same JS object, WebKit returns a new rule list. I'll file)

Spec PR in https://github.com/w3c/csswg-drafts/pull/3900.

Differential Revision: https://phabricator.services.mozilla.com/D30348

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Emilio Cobos Álvarez 2019-05-09 12:32:52 +00:00
Родитель 70d07476cb
Коммит 23b7a280bf
6 изменённых файлов: 84 добавлений и 50 удалений

Просмотреть файл

@ -24,4 +24,12 @@ interface CSSStyleSheet : StyleSheet {
unsigned long insertRule(DOMString rule, optional unsigned long index = 0);
[Throws, NeedsSubjectPrincipal]
void deleteRule(unsigned long index);
// Non-standard WebKit things.
[Throws, NeedsSubjectPrincipal, BinaryName="cssRules"]
readonly attribute CSSRuleList rules;
[Throws, NeedsSubjectPrincipal, BinaryName="deleteRule"]
void removeRule(optional unsigned long index = 0);
[Throws, NeedsSubjectPrincipal]
long addRule(optional DOMString selector = "undefined", optional DOMString style = "undefined", optional unsigned long index);
};

Просмотреть файл

@ -510,10 +510,7 @@ css::Rule* StyleSheet::GetDOMOwnerRule() const { return mOwnerRule; }
uint32_t StyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
nsIPrincipal& aSubjectPrincipal,
ErrorResult& aRv) {
if (IsReadOnly()) {
return 0;
}
if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
return 0;
}
return InsertRuleInternal(aRule, aIndex, aRv);
@ -521,15 +518,36 @@ uint32_t StyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
void StyleSheet::DeleteRule(uint32_t aIndex, nsIPrincipal& aSubjectPrincipal,
ErrorResult& aRv) {
if (IsReadOnly()) {
return;
}
if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
return;
}
return DeleteRuleInternal(aIndex, aRv);
}
int32_t StyleSheet::AddRule(const nsAString& aSelector, const nsAString& aBlock,
const Optional<uint32_t>& aIndex,
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
return -1;
}
nsAutoString rule;
rule.Append(aSelector);
rule.AppendLiteral(" { ");
if (!aBlock.IsEmpty()) {
rule.Append(aBlock);
rule.Append(' ');
}
rule.Append('}');
auto index =
aIndex.WasPassed() ? aIndex.Value() : GetCssRulesInternal()->Length();
InsertRuleInternal(rule, index, aRv);
// Always return -1.
return -1;
}
nsresult StyleSheet::DeleteRuleFromGroup(css::GroupRule* aGroup,
uint32_t aIndex) {
NS_ENSURE_ARG_POINTER(aGroup);

Просмотреть файл

@ -346,6 +346,9 @@ class StyleSheet final : public nsICSSLoaderObserver, public nsWrapperCache {
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
void DeleteRule(uint32_t aIndex, nsIPrincipal& aSubjectPrincipal,
ErrorResult& aRv);
int32_t AddRule(const nsAString& aSelector, const nsAString& aBlock,
const dom::Optional<uint32_t>& aIndex,
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
// WebIDL miscellaneous bits
inline dom::ParentObject GetParentObject() const;

Просмотреть файл

@ -2,45 +2,3 @@
[css-counter-styles IDL tests]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "speakAs" with the proper type]
expected: FAIL
[Stringification of counter]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "symbols" with the proper type]
expected: FAIL
[CSSRule interface: counter must inherit property "COUNTER_STYLE_RULE" with the proper type]
expected: FAIL
[CSSCounterStyleRule must be primary interface of counter]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "pad" with the proper type]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "prefix" with the proper type]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "suffix" with the proper type]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "name" with the proper type]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "fallback" with the proper type]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "negative" with the proper type]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "additiveSymbols" with the proper type]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "range" with the proper type]
expected: FAIL
[CSSCounterStyleRule interface: counter must inherit property "system" with the proper type]
expected: FAIL

Просмотреть файл

@ -2,3 +2,24 @@
[Declared StylePropertyMap tests]
expected: FAIL
[Declared StylePropertyMap contains custom property declarations]
expected: FAIL
[Declared StylePropertyMap contains properties with their last valid value]
expected: FAIL
[Declared StylePropertyMap does not contain properties with invalid values]
expected: FAIL
[Declared StylePropertyMap only contains properties in the style rule]
expected: FAIL
[Declared StylePropertyMap is live]
expected: FAIL
[Declared StylePropertyMap does not contain inline styles]
expected: FAIL
[Declared StylePropertyMap contains CSS property declarations in style rules]
expected: FAIL

Просмотреть файл

@ -38,6 +38,32 @@
assert_equals(styleSheet.cssRules[2], undefined, "CSSStyleSheet cssRules attribute after deleteRule function");
assert_equals(styleSheet.cssRules[0].randomProperty, 1, "[SameObject] cssRules attribute after deleteRule function");
assert_equals(styleSheet.cssRules[1].randomProperty, 2, "[SameObject] cssRules attribute after deleteRule function");
styleSheet.removeRule();
assert_equals(styleSheet.cssRules.length, 1, "CSSStyleSheet cssRules attribute after removeRule function");
assert_equals(styleSheet.cssRules[0].cssText, "#foo { height: 100px; }", "CSSStyleSheet cssRules attribute after removeRule function");
assert_equals(styleSheet.addRule("@media all", "#foo { color: red }"), -1);
assert_equals(styleSheet.cssRules.length, 2, "CSSStyleSheet cssRules attribute after addRule function");
assert_true(styleSheet.cssRules[1] instanceof CSSMediaRule, "CSSStyleSheet addRule does some silly string concatenation");
styleSheet.removeRule(1);
assert_equals(styleSheet.cssRules.length, 1, "CSSStyleSheet cssRules attribute after removeRule function with index");
assert_equals(styleSheet.cssRules[0].cssText, "#foo { height: 100px; }", "CSSStyleSheet cssRules attribute after deleteRule function with index");
assert_equals(styleSheet.addRule("#foo", "color: red"), -1);
assert_equals(styleSheet.cssRules.length, 2, "CSSStyleSheet cssRules attribute after addRule function with simple selector");
assert_equals(styleSheet.cssRules[1].cssText, "#foo { color: red; }", "CSSStyleSheet cssRules attribute after addRule function without index appends to the end");
assert_equals(styleSheet.addRule("#foo", "color: blue", 0), -1);
assert_equals(styleSheet.cssRules.length, 3, "CSSStyleSheet cssRules attribute after addRule function with simple selector with index");
assert_equals(styleSheet.cssRules[0].cssText, "#foo { color: blue; }", "addRule function with index performs an insertion");
assert_equals(styleSheet.addRule(), -1);
assert_equals(styleSheet.cssRules.length, 4, "CSSStyleSheet cssRules attribute after addRule function without arguments");
assert_equals(styleSheet.cssRules[3].cssText, "undefined { }", "addRule arguments default to undefined");
assert_equals(styleSheet.cssRules, styleSheet.rules, "CSSStyleSheet.rules returns the same object as CSSStyleSheet.cssRules");
});
</script>
</head>