@acemir/cssom 0.9.16 → 0.9.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/CSSOM.js +706 -107
- package/lib/CSSFontFaceRule.js +22 -3
- package/lib/CSSGroupingRule.js +46 -7
- package/lib/CSSImportRule.js +5 -5
- package/lib/CSSKeyframeRule.js +22 -3
- package/lib/CSSKeyframesRule.js +199 -5
- package/lib/CSSNestedDeclarations.js +23 -2
- package/lib/CSSRule.js +39 -26
- package/lib/CSSStyleRule.js +39 -7
- package/lib/CSSStyleSheet.js +101 -12
- package/lib/StyleSheet.js +8 -1
- package/lib/clone.js +6 -0
- package/lib/cssstyleTryCatchBlock.js +5 -0
- package/lib/errorUtils.js +107 -0
- package/lib/index.js +5 -0
- package/lib/parse.js +50 -34
- package/package.json +1 -1
package/lib/CSSFontFaceRule.js
CHANGED
|
@@ -3,6 +3,12 @@ var CSSOM = {
|
|
|
3
3
|
CSSStyleDeclaration: require("./CSSStyleDeclaration").CSSStyleDeclaration,
|
|
4
4
|
CSSRule: require("./CSSRule").CSSRule
|
|
5
5
|
};
|
|
6
|
+
// Use cssstyle if available
|
|
7
|
+
try {
|
|
8
|
+
CSSOM.CSSStyleDeclaration = require("cssstyle").CSSStyleDeclaration;
|
|
9
|
+
} catch (e) {
|
|
10
|
+
// ignore
|
|
11
|
+
}
|
|
6
12
|
///CommonJS
|
|
7
13
|
|
|
8
14
|
|
|
@@ -12,8 +18,8 @@ var CSSOM = {
|
|
|
12
18
|
*/
|
|
13
19
|
CSSOM.CSSFontFaceRule = function CSSFontFaceRule() {
|
|
14
20
|
CSSOM.CSSRule.call(this);
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
21
|
+
this.__style = new CSSOM.CSSStyleDeclaration();
|
|
22
|
+
this.__style.parentRule = this;
|
|
17
23
|
};
|
|
18
24
|
|
|
19
25
|
CSSOM.CSSFontFaceRule.prototype = new CSSOM.CSSRule();
|
|
@@ -23,10 +29,23 @@ CSSOM.CSSFontFaceRule.prototype.type = 5;
|
|
|
23
29
|
//CSSOM.CSSFontFaceRule.prototype.insertRule = CSSStyleSheet.prototype.insertRule;
|
|
24
30
|
//CSSOM.CSSFontFaceRule.prototype.deleteRule = CSSStyleSheet.prototype.deleteRule;
|
|
25
31
|
|
|
32
|
+
Object.defineProperty(CSSOM.CSSFontFaceRule.prototype, "style", {
|
|
33
|
+
get: function() {
|
|
34
|
+
return this.__style;
|
|
35
|
+
},
|
|
36
|
+
set: function(value) {
|
|
37
|
+
if (typeof value === "string") {
|
|
38
|
+
this.__style.cssText = value;
|
|
39
|
+
} else {
|
|
40
|
+
this.__style = value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
26
45
|
// http://www.opensource.apple.com/source/WebCore/WebCore-955.66.1/css/WebKitCSSFontFaceRule.cpp
|
|
27
46
|
Object.defineProperty(CSSOM.CSSFontFaceRule.prototype, "cssText", {
|
|
28
47
|
get: function() {
|
|
29
|
-
return "@font-face { " + this.style.cssText + " }";
|
|
48
|
+
return "@font-face {" + (this.style.cssText ? " " + this.style.cssText : "") + " }";
|
|
30
49
|
}
|
|
31
50
|
});
|
|
32
51
|
|
package/lib/CSSGroupingRule.js
CHANGED
|
@@ -3,6 +3,7 @@ var CSSOM = {
|
|
|
3
3
|
CSSRule: require("./CSSRule").CSSRule,
|
|
4
4
|
parse: require('./parse').parse
|
|
5
5
|
};
|
|
6
|
+
var errorUtils = require("./errorUtils").errorUtils;
|
|
6
7
|
///CommonJS
|
|
7
8
|
|
|
8
9
|
|
|
@@ -36,11 +37,42 @@ CSSOM.CSSGroupingRule.prototype.constructor = CSSOM.CSSGroupingRule;
|
|
|
36
37
|
* @return {number} The index within the grouping rule's collection of the newly inserted rule.
|
|
37
38
|
*/
|
|
38
39
|
CSSOM.CSSGroupingRule.prototype.insertRule = function insertRule(rule, index) {
|
|
39
|
-
if (
|
|
40
|
-
|
|
40
|
+
if (rule === undefined && index === undefined) {
|
|
41
|
+
errorUtils.throwMissingArguments(this, 'insertRule', this.constructor.name);
|
|
41
42
|
}
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
if (index === void 0) {
|
|
44
|
+
index = 0;
|
|
45
|
+
}
|
|
46
|
+
index = Number(index);
|
|
47
|
+
if (index < 0) {
|
|
48
|
+
index = 4294967296 + index;
|
|
49
|
+
}
|
|
50
|
+
if (index > this.cssRules.length) {
|
|
51
|
+
errorUtils.throwIndexError(this, 'insertRule', this.constructor.name, index, this.cssRules.length);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
var ruleToParse = String(rule);
|
|
55
|
+
var parsedSheet = CSSOM.parse(ruleToParse);
|
|
56
|
+
if (parsedSheet.cssRules.length !== 1) {
|
|
57
|
+
errorUtils.throwParseError(this, 'insertRule', this.constructor.name, ruleToParse, 'SyntaxError');
|
|
58
|
+
}
|
|
59
|
+
var cssRule = parsedSheet.cssRules[0];
|
|
60
|
+
|
|
61
|
+
// Check for rules that cannot be inserted inside a CSSGroupingRule
|
|
62
|
+
if (cssRule.constructor.name === 'CSSImportRule' || cssRule.constructor.name === 'CSSNamespaceRule') {
|
|
63
|
+
var ruleKeyword = cssRule.constructor.name === 'CSSImportRule' ? '@import' : '@namespace';
|
|
64
|
+
errorUtils.throwError(this, 'DOMException',
|
|
65
|
+
"Failed to execute 'insertRule' on '" + this.constructor.name + "': " +
|
|
66
|
+
"'" + ruleKeyword + "' rules cannot be inserted inside a group rule.",
|
|
67
|
+
'HierarchyRequestError');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Check for CSSLayerStatementRule (@layer statement rules)
|
|
71
|
+
if (cssRule.constructor.name === 'CSSLayerStatementRule') {
|
|
72
|
+
errorUtils.throwParseError(this, 'insertRule', this.constructor.name, ruleToParse, 'SyntaxError');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
cssRule.__parentRule = this;
|
|
44
76
|
this.cssRules.splice(index, 0, cssRule);
|
|
45
77
|
return index;
|
|
46
78
|
};
|
|
@@ -58,10 +90,17 @@ CSSOM.CSSGroupingRule.prototype.constructor = CSSOM.CSSGroupingRule;
|
|
|
58
90
|
* @see https://www.w3.org/TR/cssom-1/#dom-cssgroupingrule-deleterule
|
|
59
91
|
*/
|
|
60
92
|
CSSOM.CSSGroupingRule.prototype.deleteRule = function deleteRule(index) {
|
|
61
|
-
if (index
|
|
62
|
-
|
|
93
|
+
if (index === undefined) {
|
|
94
|
+
errorUtils.throwMissingArguments(this, 'deleteRule', this.constructor.name);
|
|
95
|
+
}
|
|
96
|
+
index = Number(index);
|
|
97
|
+
if (index < 0) {
|
|
98
|
+
index = 4294967296 + index;
|
|
99
|
+
}
|
|
100
|
+
if (index >= this.cssRules.length) {
|
|
101
|
+
errorUtils.throwIndexError(this, 'deleteRule', this.constructor.name, index, this.cssRules.length);
|
|
63
102
|
}
|
|
64
|
-
this.cssRules.splice(index, 1)[0].
|
|
103
|
+
this.cssRules.splice(index, 1)[0].__parentRule = null;
|
|
65
104
|
};
|
|
66
105
|
|
|
67
106
|
//.CommonJS
|
package/lib/CSSImportRule.js
CHANGED
|
@@ -28,7 +28,7 @@ CSSOM.CSSImportRule.prototype.type = 3;
|
|
|
28
28
|
Object.defineProperty(CSSOM.CSSImportRule.prototype, "cssText", {
|
|
29
29
|
get: function() {
|
|
30
30
|
var mediaText = this.media.mediaText;
|
|
31
|
-
return "@import url(" + this.href + ")" + (this.layerName !== null ? " layer" + (this.layerName && "(" + this.layerName + ")") : "" ) + (this.supportsText ? " supports(" + this.supportsText + ")" : "" ) + (mediaText ? " " + mediaText : "") + ";";
|
|
31
|
+
return "@import url(\"" + this.href + "\")" + (this.layerName !== null ? " layer" + (this.layerName && "(" + this.layerName + ")") : "" ) + (this.supportsText ? " supports(" + this.supportsText + ")" : "" ) + (mediaText ? " " + mediaText : "") + ";";
|
|
32
32
|
},
|
|
33
33
|
set: function(cssText) {
|
|
34
34
|
var i = 0;
|
|
@@ -46,7 +46,7 @@ Object.defineProperty(CSSOM.CSSImportRule.prototype, "cssText", {
|
|
|
46
46
|
var index;
|
|
47
47
|
|
|
48
48
|
var layerRegExp = /layer\(([^)]*)\)/;
|
|
49
|
-
var layerRuleNameRegExp = /^(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)$/;
|
|
49
|
+
var layerRuleNameRegExp = /^(-?[_a-zA-Z]+(\.[_a-zA-Z]+)*[_a-zA-Z0-9-]*)$/;
|
|
50
50
|
var supportsRegExp = /supports\(([^)]+)\)/;
|
|
51
51
|
var doubleOrMoreSpacesRegExp = /\s{2,}/g;
|
|
52
52
|
|
|
@@ -129,12 +129,12 @@ Object.defineProperty(CSSOM.CSSImportRule.prototype, "cssText", {
|
|
|
129
129
|
|
|
130
130
|
if (layerMatch) {
|
|
131
131
|
var layerName = layerMatch[1].trim();
|
|
132
|
-
bufferTrimmed = bufferTrimmed.replace(layerRegExp, '')
|
|
133
|
-
.replace(doubleOrMoreSpacesRegExp, ' ') // Replace double or more spaces with single space
|
|
134
|
-
.trim();
|
|
135
132
|
|
|
136
133
|
if (layerName.match(layerRuleNameRegExp) !== null) {
|
|
137
134
|
this.layerName = layerMatch[1].trim();
|
|
135
|
+
bufferTrimmed = bufferTrimmed.replace(layerRegExp, '')
|
|
136
|
+
.replace(doubleOrMoreSpacesRegExp, ' ') // Replace double or more spaces with single space
|
|
137
|
+
.trim();
|
|
138
138
|
} else {
|
|
139
139
|
// REVIEW: In the browser, an empty layer() is not processed as a unamed layer
|
|
140
140
|
// and treats the rest of the string as mediaText, ignoring the parse of supports()
|
package/lib/CSSKeyframeRule.js
CHANGED
|
@@ -3,6 +3,12 @@ var CSSOM = {
|
|
|
3
3
|
CSSRule: require("./CSSRule").CSSRule,
|
|
4
4
|
CSSStyleDeclaration: require('./CSSStyleDeclaration').CSSStyleDeclaration
|
|
5
5
|
};
|
|
6
|
+
// Use cssstyle if available
|
|
7
|
+
try {
|
|
8
|
+
CSSOM.CSSStyleDeclaration = require("cssstyle").CSSStyleDeclaration;
|
|
9
|
+
} catch (e) {
|
|
10
|
+
// ignore
|
|
11
|
+
}
|
|
6
12
|
///CommonJS
|
|
7
13
|
|
|
8
14
|
|
|
@@ -13,8 +19,8 @@ var CSSOM = {
|
|
|
13
19
|
CSSOM.CSSKeyframeRule = function CSSKeyframeRule() {
|
|
14
20
|
CSSOM.CSSRule.call(this);
|
|
15
21
|
this.keyText = '';
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
22
|
+
this.__style = new CSSOM.CSSStyleDeclaration();
|
|
23
|
+
this.__style.parentRule = this;
|
|
18
24
|
};
|
|
19
25
|
|
|
20
26
|
CSSOM.CSSKeyframeRule.prototype = new CSSOM.CSSRule();
|
|
@@ -24,10 +30,23 @@ CSSOM.CSSKeyframeRule.prototype.type = 8;
|
|
|
24
30
|
//CSSOM.CSSKeyframeRule.prototype.insertRule = CSSStyleSheet.prototype.insertRule;
|
|
25
31
|
//CSSOM.CSSKeyframeRule.prototype.deleteRule = CSSStyleSheet.prototype.deleteRule;
|
|
26
32
|
|
|
33
|
+
Object.defineProperty(CSSOM.CSSKeyframeRule.prototype, "style", {
|
|
34
|
+
get: function() {
|
|
35
|
+
return this.__style;
|
|
36
|
+
},
|
|
37
|
+
set: function(value) {
|
|
38
|
+
if (typeof value === "string") {
|
|
39
|
+
this.__style.cssText = value;
|
|
40
|
+
} else {
|
|
41
|
+
this.__style = value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
27
46
|
// http://www.opensource.apple.com/source/WebCore/WebCore-955.66.1/css/WebKitCSSKeyframeRule.cpp
|
|
28
47
|
Object.defineProperty(CSSOM.CSSKeyframeRule.prototype, "cssText", {
|
|
29
48
|
get: function() {
|
|
30
|
-
return this.keyText + " { " + this.style.cssText + " }
|
|
49
|
+
return this.keyText + " {" + (this.style.cssText ? " " + this.style.cssText : "") + " }";
|
|
31
50
|
}
|
|
32
51
|
});
|
|
33
52
|
|
package/lib/CSSKeyframesRule.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
//.CommonJS
|
|
2
2
|
var CSSOM = {
|
|
3
|
-
CSSRule: require("./CSSRule").CSSRule
|
|
3
|
+
CSSRule: require("./CSSRule").CSSRule,
|
|
4
|
+
parse: require("./parse").parse
|
|
4
5
|
};
|
|
6
|
+
var errorUtils = require("./errorUtils").errorUtils;
|
|
5
7
|
///CommonJS
|
|
6
8
|
|
|
7
9
|
|
|
@@ -13,14 +15,42 @@ CSSOM.CSSKeyframesRule = function CSSKeyframesRule() {
|
|
|
13
15
|
CSSOM.CSSRule.call(this);
|
|
14
16
|
this.name = '';
|
|
15
17
|
this.cssRules = [];
|
|
18
|
+
|
|
19
|
+
// Set up initial indexed access
|
|
20
|
+
this._setupIndexedAccess();
|
|
21
|
+
|
|
22
|
+
// Override cssRules methods after initial setup, store references as non-enumerable properties
|
|
23
|
+
var self = this;
|
|
24
|
+
var originalPush = this.cssRules.push;
|
|
25
|
+
var originalSplice = this.cssRules.splice;
|
|
26
|
+
|
|
27
|
+
// Create non-enumerable method overrides
|
|
28
|
+
Object.defineProperty(this.cssRules, 'push', {
|
|
29
|
+
value: function() {
|
|
30
|
+
var result = originalPush.apply(this, arguments);
|
|
31
|
+
self._setupIndexedAccess();
|
|
32
|
+
return result;
|
|
33
|
+
},
|
|
34
|
+
writable: true,
|
|
35
|
+
enumerable: false,
|
|
36
|
+
configurable: true
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
Object.defineProperty(this.cssRules, 'splice', {
|
|
40
|
+
value: function() {
|
|
41
|
+
var result = originalSplice.apply(this, arguments);
|
|
42
|
+
self._setupIndexedAccess();
|
|
43
|
+
return result;
|
|
44
|
+
},
|
|
45
|
+
writable: true,
|
|
46
|
+
enumerable: false,
|
|
47
|
+
configurable: true
|
|
48
|
+
});
|
|
16
49
|
};
|
|
17
50
|
|
|
18
51
|
CSSOM.CSSKeyframesRule.prototype = new CSSOM.CSSRule();
|
|
19
52
|
CSSOM.CSSKeyframesRule.prototype.constructor = CSSOM.CSSKeyframesRule;
|
|
20
53
|
CSSOM.CSSKeyframesRule.prototype.type = 7;
|
|
21
|
-
//FIXME
|
|
22
|
-
//CSSOM.CSSKeyframesRule.prototype.insertRule = CSSStyleSheet.prototype.insertRule;
|
|
23
|
-
//CSSOM.CSSKeyframesRule.prototype.deleteRule = CSSStyleSheet.prototype.deleteRule;
|
|
24
54
|
|
|
25
55
|
// http://www.opensource.apple.com/source/WebCore/WebCore-955.66.1/css/WebKitCSSKeyframesRule.cpp
|
|
26
56
|
Object.defineProperty(CSSOM.CSSKeyframesRule.prototype, "cssText", {
|
|
@@ -29,10 +59,174 @@ Object.defineProperty(CSSOM.CSSKeyframesRule.prototype, "cssText", {
|
|
|
29
59
|
for (var i=0, length=this.cssRules.length; i < length; i++) {
|
|
30
60
|
cssTexts.push(this.cssRules[i].cssText);
|
|
31
61
|
}
|
|
32
|
-
|
|
62
|
+
var cssWideKeywords = ['initial', 'inherit', 'revert', 'revert-layer', 'unset', 'none'];
|
|
63
|
+
var processedName = cssWideKeywords.includes(this.name) ? '"' + this.name + '"' : this.name;
|
|
64
|
+
return "@" + (this._vendorPrefix || '') + "keyframes " + processedName + (processedName && " ") + "{" + (cssTexts.length ? "\n " + cssTexts.join("\n ") : "") + "\n}";
|
|
33
65
|
}
|
|
34
66
|
});
|
|
35
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Appends a new keyframe rule to the list of keyframes.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} rule - The keyframe rule string to append (e.g., "50% { opacity: 0.5; }")
|
|
72
|
+
* @see https://www.w3.org/TR/css-animations-1/#dom-csskeyframesrule-appendrule
|
|
73
|
+
*/
|
|
74
|
+
CSSOM.CSSKeyframesRule.prototype.appendRule = function appendRule(rule) {
|
|
75
|
+
if (arguments.length === 0) {
|
|
76
|
+
errorUtils.throwMissingArguments(this, 'appendRule', 'CSSKeyframesRule');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
var parsedRule;
|
|
80
|
+
try {
|
|
81
|
+
// Parse the rule string as a keyframe rule
|
|
82
|
+
var tempStyleSheet = CSSOM.parse("@keyframes temp { " + rule + " }");
|
|
83
|
+
if (tempStyleSheet.cssRules.length > 0 && tempStyleSheet.cssRules[0].cssRules.length > 0) {
|
|
84
|
+
parsedRule = tempStyleSheet.cssRules[0].cssRules[0];
|
|
85
|
+
} else {
|
|
86
|
+
throw new Error("Failed to parse keyframe rule");
|
|
87
|
+
}
|
|
88
|
+
} catch (e) {
|
|
89
|
+
errorUtils.throwParseError(this, 'appendRule', 'CSSKeyframesRule', rule);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
parsedRule.__parentRule = this;
|
|
93
|
+
this.cssRules.push(parsedRule);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Deletes a keyframe rule that matches the specified key.
|
|
98
|
+
*
|
|
99
|
+
* @param {string} select - The keyframe selector to delete (e.g., "50%", "from", "to")
|
|
100
|
+
* @see https://www.w3.org/TR/css-animations-1/#dom-csskeyframesrule-deleterule
|
|
101
|
+
*/
|
|
102
|
+
CSSOM.CSSKeyframesRule.prototype.deleteRule = function deleteRule(select) {
|
|
103
|
+
if (arguments.length === 0) {
|
|
104
|
+
errorUtils.throwMissingArguments(this, 'deleteRule', 'CSSKeyframesRule');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
var normalizedSelect = this._normalizeKeyText(select);
|
|
108
|
+
|
|
109
|
+
for (var i = 0; i < this.cssRules.length; i++) {
|
|
110
|
+
var rule = this.cssRules[i];
|
|
111
|
+
if (this._normalizeKeyText(rule.keyText) === normalizedSelect) {
|
|
112
|
+
rule.__parentRule = null;
|
|
113
|
+
this.cssRules.splice(i, 1);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Finds and returns the keyframe rule that matches the specified key.
|
|
121
|
+
* When multiple rules have the same key, returns the last one.
|
|
122
|
+
*
|
|
123
|
+
* @param {string} select - The keyframe selector to find (e.g., "50%", "from", "to")
|
|
124
|
+
* @return {CSSKeyframeRule|null} The matching keyframe rule, or null if not found
|
|
125
|
+
* @see https://www.w3.org/TR/css-animations-1/#dom-csskeyframesrule-findrule
|
|
126
|
+
*/
|
|
127
|
+
CSSOM.CSSKeyframesRule.prototype.findRule = function findRule(select) {
|
|
128
|
+
if (arguments.length === 0) {
|
|
129
|
+
errorUtils.throwMissingArguments(this, 'findRule', 'CSSKeyframesRule');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
var normalizedSelect = this._normalizeKeyText(select);
|
|
133
|
+
|
|
134
|
+
// Iterate backwards to find the last matching rule
|
|
135
|
+
for (var i = this.cssRules.length - 1; i >= 0; i--) {
|
|
136
|
+
var rule = this.cssRules[i];
|
|
137
|
+
if (this._normalizeKeyText(rule.keyText) === normalizedSelect) {
|
|
138
|
+
return rule;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return null;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Normalizes keyframe selector text for comparison.
|
|
147
|
+
* Handles "from" -> "0%" and "to" -> "100%" conversions and trims whitespace.
|
|
148
|
+
*
|
|
149
|
+
* @private
|
|
150
|
+
* @param {string} keyText - The keyframe selector text to normalize
|
|
151
|
+
* @return {string} The normalized keyframe selector text
|
|
152
|
+
*/
|
|
153
|
+
CSSOM.CSSKeyframesRule.prototype._normalizeKeyText = function _normalizeKeyText(keyText) {
|
|
154
|
+
if (!keyText) return '';
|
|
155
|
+
|
|
156
|
+
var normalized = keyText.toString().trim().toLowerCase();
|
|
157
|
+
|
|
158
|
+
// Convert keywords to percentages for comparison
|
|
159
|
+
if (normalized === 'from') {
|
|
160
|
+
return '0%';
|
|
161
|
+
} else if (normalized === 'to') {
|
|
162
|
+
return '100%';
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return normalized;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Makes CSSKeyframesRule iterable over its cssRules.
|
|
170
|
+
* Allows for...of loops and other iterable methods.
|
|
171
|
+
*/
|
|
172
|
+
if (typeof Symbol !== 'undefined' && Symbol.iterator) {
|
|
173
|
+
CSSOM.CSSKeyframesRule.prototype[Symbol.iterator] = function() {
|
|
174
|
+
var index = 0;
|
|
175
|
+
var cssRules = this.cssRules;
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
next: function() {
|
|
179
|
+
if (index < cssRules.length) {
|
|
180
|
+
return { value: cssRules[index++], done: false };
|
|
181
|
+
} else {
|
|
182
|
+
return { done: true };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Adds indexed getters for direct access to cssRules by index.
|
|
191
|
+
* This enables rule[0], rule[1], etc. access patterns.
|
|
192
|
+
* Works in environments where Proxy is not available (like jsdom).
|
|
193
|
+
*/
|
|
194
|
+
CSSOM.CSSKeyframesRule.prototype._setupIndexedAccess = function() {
|
|
195
|
+
// Remove any existing indexed properties
|
|
196
|
+
for (var i = 0; i < 1000; i++) { // reasonable upper limit
|
|
197
|
+
if (this.hasOwnProperty(i)) {
|
|
198
|
+
delete this[i];
|
|
199
|
+
} else {
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Add indexed getters for current cssRules
|
|
205
|
+
for (var i = 0; i < this.cssRules.length; i++) {
|
|
206
|
+
(function(index) {
|
|
207
|
+
Object.defineProperty(this, index, {
|
|
208
|
+
get: function() {
|
|
209
|
+
return this.cssRules[index];
|
|
210
|
+
},
|
|
211
|
+
enumerable: false,
|
|
212
|
+
configurable: true
|
|
213
|
+
});
|
|
214
|
+
}.call(this, i));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Update length property
|
|
218
|
+
Object.defineProperty(this, 'length', {
|
|
219
|
+
get: function() {
|
|
220
|
+
return this.cssRules.length;
|
|
221
|
+
},
|
|
222
|
+
enumerable: false,
|
|
223
|
+
configurable: true
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
36
230
|
|
|
37
231
|
//.CommonJS
|
|
38
232
|
exports.CSSKeyframesRule = CSSOM.CSSKeyframesRule;
|
|
@@ -1,23 +1,44 @@
|
|
|
1
1
|
//.CommonJS
|
|
2
2
|
var CSSOM = {
|
|
3
3
|
CSSRule: require("./CSSRule").CSSRule,
|
|
4
|
+
CSSStyleDeclaration: require('./CSSStyleDeclaration').CSSStyleDeclaration
|
|
4
5
|
};
|
|
6
|
+
// Use cssstyle if available
|
|
7
|
+
try {
|
|
8
|
+
CSSOM.CSSStyleDeclaration = require("cssstyle").CSSStyleDeclaration;
|
|
9
|
+
} catch (e) {
|
|
10
|
+
// ignore
|
|
11
|
+
}
|
|
5
12
|
///CommonJS
|
|
6
13
|
|
|
14
|
+
|
|
7
15
|
/**
|
|
8
16
|
* @constructor
|
|
9
17
|
* @see https://drafts.csswg.org/css-nesting-1/
|
|
10
18
|
*/
|
|
11
19
|
CSSOM.CSSNestedDeclarations = function CSSNestedDeclarations() {
|
|
12
20
|
CSSOM.CSSRule.call(this);
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
21
|
+
this.__style = new CSSOM.CSSStyleDeclaration();
|
|
22
|
+
this.__style.parentRule = this;
|
|
15
23
|
};
|
|
16
24
|
|
|
17
25
|
CSSOM.CSSNestedDeclarations.prototype = new CSSOM.CSSRule();
|
|
18
26
|
CSSOM.CSSNestedDeclarations.prototype.constructor = CSSOM.CSSNestedDeclarations;
|
|
19
27
|
CSSOM.CSSNestedDeclarations.prototype.type = 0;
|
|
20
28
|
|
|
29
|
+
Object.defineProperty(CSSOM.CSSNestedDeclarations.prototype, "style", {
|
|
30
|
+
get: function() {
|
|
31
|
+
return this.__style;
|
|
32
|
+
},
|
|
33
|
+
set: function(value) {
|
|
34
|
+
if (typeof value === "string") {
|
|
35
|
+
this.__style.cssText = value;
|
|
36
|
+
} else {
|
|
37
|
+
this.__style = value;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
21
42
|
Object.defineProperty(CSSOM.CSSNestedDeclarations.prototype, "cssText", {
|
|
22
43
|
get: function () {
|
|
23
44
|
return this.style.cssText;
|
package/lib/CSSRule.js
CHANGED
|
@@ -8,35 +8,48 @@ var CSSOM = {};
|
|
|
8
8
|
* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSRule
|
|
9
9
|
*/
|
|
10
10
|
CSSOM.CSSRule = function CSSRule() {
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
11
|
+
this.__parentRule = null;
|
|
12
|
+
this.__parentStyleSheet = null;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
CSSOM.CSSRule.
|
|
16
|
-
CSSOM.CSSRule.STYLE_RULE = 1;
|
|
17
|
-
CSSOM.CSSRule.CHARSET_RULE = 2; // obsolete
|
|
18
|
-
CSSOM.CSSRule.IMPORT_RULE = 3;
|
|
19
|
-
CSSOM.CSSRule.MEDIA_RULE = 4;
|
|
20
|
-
CSSOM.CSSRule.FONT_FACE_RULE = 5;
|
|
21
|
-
CSSOM.CSSRule.PAGE_RULE = 6;
|
|
22
|
-
CSSOM.CSSRule.KEYFRAMES_RULE = 7;
|
|
23
|
-
CSSOM.CSSRule.KEYFRAME_RULE = 8;
|
|
24
|
-
CSSOM.CSSRule.MARGIN_RULE = 9;
|
|
25
|
-
CSSOM.CSSRule.NAMESPACE_RULE = 10;
|
|
26
|
-
CSSOM.CSSRule.COUNTER_STYLE_RULE = 11;
|
|
27
|
-
CSSOM.CSSRule.SUPPORTS_RULE = 12;
|
|
28
|
-
CSSOM.CSSRule.DOCUMENT_RULE = 13;
|
|
29
|
-
CSSOM.CSSRule.FONT_FEATURE_VALUES_RULE = 14;
|
|
30
|
-
CSSOM.CSSRule.VIEWPORT_RULE = 15;
|
|
31
|
-
CSSOM.CSSRule.REGION_STYLE_RULE = 16;
|
|
32
|
-
CSSOM.CSSRule.CONTAINER_RULE = 17;
|
|
33
|
-
CSSOM.CSSRule.LAYER_BLOCK_RULE = 18;
|
|
34
|
-
CSSOM.CSSRule.STARTING_STYLE_RULE = 1002;
|
|
15
|
+
Object.defineProperties(CSSOM.CSSRule.prototype, {
|
|
35
16
|
|
|
36
|
-
CSSOM.CSSRule
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
17
|
+
constructor: { value: CSSOM.CSSRule },
|
|
18
|
+
|
|
19
|
+
parentRule: {
|
|
20
|
+
get: function() {
|
|
21
|
+
return this.__parentRule
|
|
22
|
+
}
|
|
23
|
+
},
|
|
40
24
|
|
|
25
|
+
parentStyleSheet: {
|
|
26
|
+
get: function() {
|
|
27
|
+
return this.__parentStyleSheet
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
UNKNOWN_RULE: { value: 0, enumerable: true }, // obsolet
|
|
32
|
+
STYLE_RULE: { value: 1, enumerable: true },
|
|
33
|
+
CHARSET_RULE: { value: 2, enumerable: true }, // obsolet
|
|
34
|
+
IMPORT_RULE: { value: 3, enumerable: true },
|
|
35
|
+
MEDIA_RULE: { value: 4, enumerable: true },
|
|
36
|
+
FONT_FACE_RULE: { value: 5, enumerable: true },
|
|
37
|
+
PAGE_RULE: { value: 6, enumerable: true },
|
|
38
|
+
KEYFRAMES_RULE: { value: 7, enumerable: true },
|
|
39
|
+
KEYFRAME_RULE: { value: 8, enumerable: true },
|
|
40
|
+
MARGIN_RULE: { value: 9, enumerable: true },
|
|
41
|
+
NAMESPACE_RULE: { value: 10, enumerable: true },
|
|
42
|
+
COUNTER_STYLE_RULE: { value: 11, enumerable: true },
|
|
43
|
+
SUPPORTS_RULE: { value: 12, enumerable: true },
|
|
44
|
+
DOCUMENT_RULE: { value: 13, enumerable: true },
|
|
45
|
+
FONT_FEATURE_VALUES_RULE: { value: 14, enumerable: true },
|
|
46
|
+
VIEWPORT_RULE: { value: 15, enumerable: true },
|
|
47
|
+
REGION_STYLE_RULE: { value: 16, enumerable: true },
|
|
48
|
+
CONTAINER_RULE: { value: 17, enumerable: true },
|
|
49
|
+
LAYER_BLOCK_RULE: { value: 18, enumerable: true },
|
|
50
|
+
STARTING_STYLE_RULE: { value: 1002, enumerable: true },
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
//.CommonJS
|
|
41
54
|
exports.CSSRule = CSSOM.CSSRule;
|
|
42
55
|
///CommonJS
|
package/lib/CSSStyleRule.js
CHANGED
|
@@ -4,6 +4,12 @@ var CSSOM = {
|
|
|
4
4
|
CSSGroupingRule: require("./CSSGroupingRule").CSSGroupingRule,
|
|
5
5
|
CSSRule: require("./CSSRule").CSSRule
|
|
6
6
|
};
|
|
7
|
+
// Use cssstyle if available
|
|
8
|
+
try {
|
|
9
|
+
CSSOM.CSSStyleDeclaration = require("cssstyle").CSSStyleDeclaration;
|
|
10
|
+
} catch (e) {
|
|
11
|
+
// ignore
|
|
12
|
+
}
|
|
7
13
|
///CommonJS
|
|
8
14
|
|
|
9
15
|
|
|
@@ -14,14 +20,40 @@ var CSSOM = {
|
|
|
14
20
|
*/
|
|
15
21
|
CSSOM.CSSStyleRule = function CSSStyleRule() {
|
|
16
22
|
CSSOM.CSSGroupingRule.call(this);
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
23
|
+
this.__selectorText = "";
|
|
24
|
+
this.__style = new CSSOM.CSSStyleDeclaration();
|
|
25
|
+
this.__style.parentRule = this;
|
|
20
26
|
};
|
|
21
27
|
|
|
22
28
|
CSSOM.CSSStyleRule.prototype = new CSSOM.CSSGroupingRule();
|
|
23
29
|
CSSOM.CSSStyleRule.prototype.constructor = CSSOM.CSSStyleRule;
|
|
24
|
-
|
|
30
|
+
|
|
31
|
+
Object.defineProperty(CSSOM.CSSStyleRule.prototype, "type", {
|
|
32
|
+
value: 1,
|
|
33
|
+
writable: false
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
Object.defineProperty(CSSOM.CSSStyleRule.prototype, "selectorText", {
|
|
37
|
+
get: function() {
|
|
38
|
+
return this.__selectorText;
|
|
39
|
+
},
|
|
40
|
+
set: function(value) {
|
|
41
|
+
this.__selectorText = value;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
Object.defineProperty(CSSOM.CSSStyleRule.prototype, "style", {
|
|
46
|
+
get: function() {
|
|
47
|
+
return this.__style;
|
|
48
|
+
},
|
|
49
|
+
set: function(value) {
|
|
50
|
+
if (typeof value === "string") {
|
|
51
|
+
this.__style.cssText = value;
|
|
52
|
+
} else {
|
|
53
|
+
this.__style = value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
25
57
|
|
|
26
58
|
Object.defineProperty(CSSOM.CSSStyleRule.prototype, "cssText", {
|
|
27
59
|
get: function() {
|
|
@@ -34,7 +66,7 @@ Object.defineProperty(CSSOM.CSSStyleRule.prototype, "cssText", {
|
|
|
34
66
|
valuesArr.push(this.cssRules.map(function(rule){ return rule.cssText }).join("\n "));
|
|
35
67
|
values = valuesArr.join("\n ") + "\n}"
|
|
36
68
|
} else {
|
|
37
|
-
values = " { " + this.style.cssText + " }";
|
|
69
|
+
values = " {" + (this.style.cssText ? " " + this.style.cssText : "") + " }";
|
|
38
70
|
}
|
|
39
71
|
text = this.selectorText + values;
|
|
40
72
|
} else {
|
|
@@ -44,8 +76,8 @@ Object.defineProperty(CSSOM.CSSStyleRule.prototype, "cssText", {
|
|
|
44
76
|
},
|
|
45
77
|
set: function(cssText) {
|
|
46
78
|
var rule = CSSOM.CSSStyleRule.parse(cssText);
|
|
47
|
-
this.
|
|
48
|
-
this.
|
|
79
|
+
this.__style = rule.style;
|
|
80
|
+
this.__selectorText = rule.selectorText;
|
|
49
81
|
}
|
|
50
82
|
});
|
|
51
83
|
|