8000 Bug 1545823 - Implement non-standard CSSStyleSheet.rules, CSSStyleShe… · mozilla/gecko-dev@23b7a28 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 23b7a28

Browse files
committed
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 w3c/csswg-drafts#3900. Differential Revision: https://phabricator.services.mozilla.com/D30348 --HG-- extra : moz-landing-system : lando
1 parent 70d0747 commit 23b7a28

File tree

6 files changed

+84
-50
lines changed

6 files changed

+84
-50
lines changed

dom/webidl/CSSStyleSheet.webidl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ interface CSSStyleSheet : StyleSheet {
2424
unsigned long insertRule(DOMString rule, optional unsigned long index = 0);
2525
[Throws, NeedsSubjectPrincipal]
2626
void deleteRule(unsigned long index);
27+
28+
// Non-standard WebKit things.
29+
[Throws, NeedsSubjectPrincipal, BinaryName="cssRules"]
30+
readonly attribute CSSRuleList rules;
31+
[Throws, NeedsSubjectPrincipal, BinaryName="deleteRule"]
32+
void removeRule(optional unsigned long index = 0);
33+
[Throws, NeedsSubjectPrincipal]
34+
long addRule(optional DOMString selector = "undefined", optional DOMString style = "undefined", optional unsigned long index);
2735
};

layout/style/StyleSheet.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,26 +510,44 @@ css::Rule* StyleSheet::GetDOMOwnerRule() const { return mOwnerRule; }
510510
uint32_t StyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
511511
nsIPrincipal& aSubjectPrincipal,
512512
ErrorResult& aRv) {
513-
if (IsReadOnly()) {
514-
return 0;
515-
}
516-
if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
513+
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
517514
return 0;
518515
}
519516
return InsertRuleInternal(aRule, aIndex, aRv);
520517
}
521518

522519
void StyleSheet::DeleteRule(uint32_t aIndex, nsIPrincipal& aSubjectPrincipal,
523520
ErrorResult& aRv) {
524-
if (IsReadOnly()) {
525-
return;
526-
}
527-
if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
521+
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
528522
return;
529523
}
530524
return DeleteRuleInternal(aIndex, aRv);
531525
}
532526

527+
int32_t StyleSheet::AddRule(const nsAString& aSelector, const nsAString& aBlock,
528+
const Optional<uint32_t>& aIndex,
529+
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
530+
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
531+
return -1;
532+
}
533+
534+
nsAutoString rule;
535+
rule.Append(aSelector);
536+
rule.AppendLiteral(" { ");
537+
if (!aBlock.IsEmpty()) {
538+
rule.Append(aBlock);
539+
rule.Append(' ');
540+
}
541+
rule.Append('}');
542+
543+
auto index =
544+
aIndex.WasPassed() ? aIndex.Value() : GetCssRulesInternal()->Length();
545+
546+
InsertRuleInternal(rule, index, aRv);
547+
// Always return -1.
548+
return -1;
549+
}
550+
533551
nsresult StyleSheet::DeleteRuleFromGroup(css::GroupRule* aGroup,
534552
uint32_t aIndex) {
535553
NS_ENSURE_ARG_POINTER(aGroup);

layout/style/StyleSheet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ class StyleSheet final : public nsICSSLoaderObserver, public nsWrapperCache {
346346
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
347347
void DeleteRule(uint32_t aIndex, nsIPrincipal& aSubjectPrincipal,
348348
ErrorResult& aRv);
349+
int32_t AddRule(const nsAString& aSelector, const nsAString& aBlock,
350+
const dom::Optional<uint32_t>& aIndex,
351+
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
349352

350353
// WebIDL miscellaneous bits
351354
inline dom::ParentObject GetParentObject() const;

testing/web-platform/meta/css/css-counter-styles/idlharness.html.ini

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,3 @@
22
[css-counter-styles IDL tests]
33
expected: FAIL
44

5-
[CSSCounterStyleRule interface: counter must inherit property "speakAs" with the proper type]
6-
expected: FAIL
7-
8-
[Stringification of counter]
9-
expected: FAIL
10-
11-
[CSSCounterStyleRule interface: counter must inherit property " 29C2 symbols" with the proper type]
12-
expected: FAIL
13-
14-
[CSSRule interface: counter must inherit property "COUNTER_STYLE_RULE" with the proper type]
15-
expected: FAIL
16-
17-
[CSSCounterStyleRule must be primary interface of counter]
18-
expected: FAIL
19-
20-
[CSSCounterStyleRule interface: counter must inherit property "pad" with the proper type]
21-
expected: FAIL
22-
23-
[CSSCounterStyleRule interface: counter must inherit property "prefix" with the proper type]
24-
expected: FAIL
25-
26-
[CSSCounterStyleRule interface: counter must inherit property "suffix" with the proper type]
27-
expected: FAIL
28-
29-
[CSSCounterStyleRule interface: counter must inherit property "name" with the proper type]
30-
expected: FAIL
31-
32-
[CSSCounterStyleRule interface: counter must inherit property "fallback" with the proper type]
33-
expected: FAIL
34-
35-
[CSSCounterStyleRule interface: counter must inherit property "negative" with the proper type]
36-
expected: FAIL
37-
38-
[CSSCounterStyleRule interface: counter must inherit property "additiveSymbols" with the proper type]
39-
expected: FAIL
40-
41-
[CSSCounterStyleRule interface: counter must inherit property "range" with the proper type]
42-
expected: FAIL
43-
44-
[CSSCounterStyleRule interface: counter must inherit property "system" with the proper type]
45-
expected: FAIL
46-

testing/web-platform/meta/css/css-typed-om/the-stylepropertymap/declared/declared.tentative.html.ini

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,24 @@
22
[Declared StylePropertyMap tests]
33
expected: FAIL
44

5+
[Declared StylePropertyMap contains custom property declarations]
6+
expected: FAIL
7+
8+
[Declared 29C2 StylePropertyMap contains properties with their last valid value]
9+
expected: FAIL
10+
11+
[Declared StylePropertyMap does not contain properties with invalid values]
12+
expected: FAIL
13+
14+
[Declared StylePropertyMap only contains properties in the style rule]
15+
expected: FAIL
< E1FE code>16+
17+
[Declared StylePropertyMap is live]
18+
expected: FAIL
19+
20+
[Declared StylePropertyMap does not contain inline styles]
21+
expected: FAIL
22+
23+
[Declared StylePropertyMap contains CSS property declarations in style rules]
24+
expected: FAIL
25+

testing/web-platform/tests/css/cssom/CSSStyleSheet.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@
3838
assert_equals(styleSheet.cssRules[2], undefined, "CSSStyleSheet cssRules attribute after deleteRule function");
3939
assert_equals(styleSheet.cssRules[0].randomProperty, 1, "[SameObject] cssRules attribute after deleteRule function");
4040
assert_equals(styleSheet.cssRules[1].randomProperty, 2, "[SameObject] cssRules attribute after deleteRule function");
41+
42+
styleSheet.removeRule();
43+
assert_equals(styleSheet.cssRules.length, 1, "CSSStyleSheet cssRules attribute after removeRule function");
44+
assert_equals(styleSheet.cssRules[0].cssText, "#foo { height: 100px; }", "CSSStyleSheet cssRules attribute after removeRule function");
45+
46+
assert_equals(styleSheet.addRule("@media all", "#foo { color: red }"), -1);
47+
assert_equals(styleSheet.cssRules.length, 2, "CSSStyleSheet cssRules attribute after addRule function");
48+
assert_true(styleSheet.cssRules[1] instanceof CSSMediaRule, "CSSStyleSheet addRule does some silly string concatenation");
49+
50+
styleSheet.removeRule(1);
51+
assert_equals(styleSheet.cssRules.length, 1, "CSSStyleSheet cssRules attribute after removeRule function with index");
52+
assert_equals(styleSheet.cssRules[0].cssText, "#foo { height: 100px; }", "CSSStyleSheet cssRules attribute after deleteRule function with index");
53+
54+
assert_equals(styleSheet.addRule("#foo", "color: red"), -1);
55+
assert_equals(styleSheet.cssRules.length, 2, "CSSStyleSheet cssRules attribute after addRule function with simple selector");
56+
assert_equals(styleSheet.cssRules[1].cssText, "#foo { color: red; }", "CSSStyleSheet cssRules attribute after addRule function without index appends to the end");
57+
58+
assert_equals(styleSheet.addRule("#foo", "color: blue", 0), -1);
59+
assert_equals(styleSheet.cssRules.length, 3, "CSSStyleSheet cssRules attribute after addRule function with simple selector with index");
60+
assert_equals(styleSheet.cssRules[0].cssText, "#foo { color: blue; }", "addRule function with index performs an insertion");
61+
62+
assert_equals(styleSheet.addRule(), -1);
63+
assert_equals(styleSheet.cssRules.length, 4, "CSSStyleSheet cssRules attribute after addRule function without arguments");
64+
assert_equals(styleSheet.cssRules[3].cssText, "undefined { }", "addRule arguments default to undefined");
65+
66+
assert_equals(styleSheet.cssRules, styleSheet.rules, "CSSStyleSheet.rules returns the same object as CSSStyleSheet.cssRules");
4167
});
4268
</script>
4369
</head>

0 commit comments

Comments
 (0)
0