Skip to content

Commit 68c13c8

Browse files
committed
Fix parentRule and implement parentStyleSheet
parentRule and parentStyleSheet should match the spec now.
1 parent fc20d17 commit 68c13c8

9 files changed

Lines changed: 183 additions & 81 deletions

lib/CSSKeyframeRule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CSSOM.CSSKeyframeRule = function CSSKeyframeRule() {
1414
CSSOM.CSSRule.call(this);
1515
this.keyText = '';
1616
this.style = new CSSOM.CSSStyleDeclaration;
17+
this.style.parentRule = this;
1718
};
1819

1920
CSSOM.CSSKeyframeRule.prototype = new CSSOM.CSSRule;

lib/CSSRule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var CSSOM = {};
1010
*/
1111
CSSOM.CSSRule = function CSSRule() {
1212
this.parentRule = null;
13+
this.parentStyleSheet = null;
1314
};
1415

1516
CSSOM.CSSRule.STYLE_RULE = 1;

lib/CSSStyleDeclaration.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var CSSOM = {};
99
*/
1010
CSSOM.CSSStyleDeclaration = function CSSStyleDeclaration(){
1111
this.length = 0;
12+
this.parentRule = null;
1213

1314
// NON-STANDARD
1415
this._importants = {};

lib/CSSStyleRule.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CSSOM.CSSStyleRule = function CSSStyleRule() {
1515
CSSOM.CSSRule.call(this);
1616
this.selectorText = "";
1717
this.style = new CSSOM.CSSStyleDeclaration;
18+
this.style.parentRule = this;
1819
};
1920

2021
CSSOM.CSSStyleRule.prototype = new CSSOM.CSSRule;

lib/CSSStyleSheet.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var CSSOM = {
1111
* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet
1212
*/
1313
CSSOM.CSSStyleSheet = function CSSStyleSheet() {
14+
CSSOM.StyleSheet.call(this);
1415
this.cssRules = [];
1516
};
1617

lib/StyleSheet.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ var CSSOM = {};
77
* @constructor
88
* @see http://dev.w3.org/csswg/cssom/#the-stylesheet-interface
99
*/
10-
CSSOM.StyleSheet = function StyleSheet(){};
10+
CSSOM.StyleSheet = function StyleSheet() {
11+
this.parentStyleSheet = null;
12+
};
1113

1214

1315
//.CommonJS

lib/parse.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ CSSOM.parse = function parse(token) {
4444

4545
var styleSheet = new CSSOM.CSSStyleSheet;
4646

47-
// @type CSSStyleSheet|CSSMediaRule
47+
// @type CSSStyleSheet|CSSMediaRule|CSSKeyframesRule
4848
var currentScope = styleSheet;
4949

50+
// @type CSSMediaRule|CSSKeyframesRule
51+
var parentRule;
52+
5053
var selector, name, value, priority="", styleRule, mediaRule, importRule, keyframesRule, keyframeRule;
5154

5255
for (var character; character = token.charAt(i); i++) {
@@ -152,14 +155,17 @@ CSSOM.parse = function parse(token) {
152155
state = "before-name";
153156
} else if (state === "atBlock") {
154157
mediaRule.media.mediaText = buffer.trim();
155-
mediaRule.parentRule = currentScope;
156-
currentScope = mediaRule;
158+
currentScope = parentRule = mediaRule;
159+
mediaRule.parentStyleSheet = styleSheet;
157160
buffer = "";
158161
state = "before-selector";
159162
} else if (state === "keyframesRule-begin") {
160163
keyframesRule.name = buffer.trim();
161-
keyframesRule.parentRule = currentScope;
162-
currentScope = keyframesRule;
164+
if (parentRule) {
165+
keyframesRule.parentRule = parentRule;
166+
}
167+
keyframesRule.parentStyleSheet = styleSheet;
168+
currentScope = parentRule = keyframesRule;
163169
buffer = "";
164170
state = "keyframeRule-begin";
165171
} else if (state === "keyframeRule-begin") {
@@ -217,9 +223,9 @@ CSSOM.parse = function parse(token) {
217223
break;
218224
case "importRule":
219225
importRule = new CSSOM.CSSImportRule;
220-
importRule.parentRule = currentScope;
226+
importRule.parentStyleSheet = importRule.styleSheet.parentStyleSheet = styleSheet;
221227
importRule.cssText = buffer + character;
222-
currentScope.cssRules.push(importRule);
228+
styleSheet.cssRules.push(importRule);
223229
buffer = "";
224230
state = "before-selector";
225231
break;
@@ -237,7 +243,10 @@ CSSOM.parse = function parse(token) {
237243
case "before-name":
238244
case "name":
239245
styleRule.__ends = i + 1;
240-
styleRule.parentRule = currentScope;
246+
if (parentRule) {
247+
styleRule.parentRule = parentRule;
248+
}
249+
styleRule.parentStyleSheet = styleSheet;
241250
currentScope.cssRules.push(styleRule);
242251
buffer = "";
243252
if (currentScope.constructor === CSSOM.CSSKeyframesRule) {
@@ -250,13 +259,14 @@ CSSOM.parse = function parse(token) {
250259
case "before-selector":
251260
case "selector":
252261
// End of media rule.
253-
// Nesting rules aren't supported yet
254-
if (!currentScope.parentRule) {
262+
if (!parentRule) {
255263
throw "unexpected }";
256264
}
257265
currentScope.__ends = i + 1;
258-
currentScope.parentRule.cssRules.push(currentScope);
259-
currentScope = currentScope.parentRule;
266+
// Nesting rules aren’t supported yet
267+
styleSheet.cssRules.push(currentScope);
268+
currentScope = styleSheet;
269+
parentRule = null;
260270
buffer = "";
261271
state = "before-selector";
262272
break;

test/CSSStyleDeclaration.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test("CSSStyleDeclaration", function(){
55
equalOwnProperties(d, {
66
0: "color",
77
length: 1,
8+
parentRule: null,
89
color: "purple"
910
});
1011

@@ -13,6 +14,7 @@ test("CSSStyleDeclaration", function(){
1314
0: "color",
1415
1: "width",
1516
length: 2,
17+
parentRule: null,
1618
color: "purple",
1719
width: "128px"
1820
});

0 commit comments

Comments
 (0)