@acemir/cssom 0.9.0

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.
@@ -0,0 +1,148 @@
1
+ //.CommonJS
2
+ var CSSOM = {};
3
+ ///CommonJS
4
+
5
+
6
+ /**
7
+ * @constructor
8
+ * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration
9
+ */
10
+ CSSOM.CSSStyleDeclaration = function CSSStyleDeclaration(){
11
+ this.length = 0;
12
+ this.parentRule = null;
13
+
14
+ // NON-STANDARD
15
+ this._importants = {};
16
+ };
17
+
18
+
19
+ CSSOM.CSSStyleDeclaration.prototype = {
20
+
21
+ constructor: CSSOM.CSSStyleDeclaration,
22
+
23
+ /**
24
+ *
25
+ * @param {string} name
26
+ * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-getPropertyValue
27
+ * @return {string} the value of the property if it has been explicitly set for this declaration block.
28
+ * Returns the empty string if the property has not been set.
29
+ */
30
+ getPropertyValue: function(name) {
31
+ return this[name] || "";
32
+ },
33
+
34
+ /**
35
+ *
36
+ * @param {string} name
37
+ * @param {string} value
38
+ * @param {string} [priority=null] "important" or null
39
+ * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-setProperty
40
+ */
41
+ setProperty: function(name, value, priority) {
42
+ if (this[name]) {
43
+ // Property already exist. Overwrite it.
44
+ var index = Array.prototype.indexOf.call(this, name);
45
+ if (index < 0) {
46
+ this[this.length] = name;
47
+ this.length++;
48
+ }
49
+ } else {
50
+ // New property.
51
+ this[this.length] = name;
52
+ this.length++;
53
+ }
54
+ this[name] = value + "";
55
+ this._importants[name] = priority;
56
+ },
57
+
58
+ /**
59
+ *
60
+ * @param {string} name
61
+ * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-removeProperty
62
+ * @return {string} the value of the property if it has been explicitly set for this declaration block.
63
+ * Returns the empty string if the property has not been set or the property name does not correspond to a known CSS property.
64
+ */
65
+ removeProperty: function(name) {
66
+ if (!(name in this)) {
67
+ return "";
68
+ }
69
+ var index = Array.prototype.indexOf.call(this, name);
70
+ if (index < 0) {
71
+ return "";
72
+ }
73
+ var prevValue = this[name];
74
+ this[name] = "";
75
+
76
+ // That's what WebKit and Opera do
77
+ Array.prototype.splice.call(this, index, 1);
78
+
79
+ // That's what Firefox does
80
+ //this[index] = ""
81
+
82
+ return prevValue;
83
+ },
84
+
85
+ getPropertyCSSValue: function() {
86
+ //FIXME
87
+ },
88
+
89
+ /**
90
+ *
91
+ * @param {String} name
92
+ */
93
+ getPropertyPriority: function(name) {
94
+ return this._importants[name] || "";
95
+ },
96
+
97
+
98
+ /**
99
+ * element.style.overflow = "auto"
100
+ * element.style.getPropertyShorthand("overflow-x")
101
+ * -> "overflow"
102
+ */
103
+ getPropertyShorthand: function() {
104
+ //FIXME
105
+ },
106
+
107
+ isPropertyImplicit: function() {
108
+ //FIXME
109
+ },
110
+
111
+ // Doesn't work in IE < 9
112
+ get cssText(){
113
+ var properties = [];
114
+ for (var i=0, length=this.length; i < length; ++i) {
115
+ var name = this[i];
116
+ var value = this.getPropertyValue(name);
117
+ var priority = this.getPropertyPriority(name);
118
+ if (priority) {
119
+ priority = " !" + priority;
120
+ }
121
+ properties[i] = name + ": " + value + priority + ";";
122
+ }
123
+ return properties.join(" ");
124
+ },
125
+
126
+ set cssText(text){
127
+ var i, name;
128
+ for (i = this.length; i--;) {
129
+ name = this[i];
130
+ this[name] = "";
131
+ }
132
+ Array.prototype.splice.call(this, 0, this.length);
133
+ this._importants = {};
134
+
135
+ var dummyRule = CSSOM.parse('#bogus{' + text + '}').cssRules[0].style;
136
+ var length = dummyRule.length;
137
+ for (i = 0; i < length; ++i) {
138
+ name = dummyRule[i];
139
+ this.setProperty(dummyRule[i], dummyRule.getPropertyValue(name), dummyRule.getPropertyPriority(name));
140
+ }
141
+ }
142
+ };
143
+
144
+
145
+ //.CommonJS
146
+ exports.CSSStyleDeclaration = CSSOM.CSSStyleDeclaration;
147
+ CSSOM.parse = require('./parse').parse; // Cannot be included sooner due to the mutual dependency between parse.js and CSSStyleDeclaration.js
148
+ ///CommonJS
@@ -0,0 +1,200 @@
1
+ //.CommonJS
2
+ var CSSOM = {
3
+ CSSStyleDeclaration: require("./CSSStyleDeclaration").CSSStyleDeclaration,
4
+ CSSGroupingRule: require("./CSSGroupingRule").CSSGroupingRule,
5
+ CSSRule: require("./CSSRule").CSSRule
6
+ };
7
+ ///CommonJS
8
+
9
+
10
+ /**
11
+ * @constructor
12
+ * @see http://dev.w3.org/csswg/cssom/#cssstylerule
13
+ * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleRule
14
+ */
15
+ CSSOM.CSSStyleRule = function CSSStyleRule() {
16
+ CSSOM.CSSGroupingRule.call(this);
17
+ this.selectorText = "";
18
+ this.style = new CSSOM.CSSStyleDeclaration();
19
+ this.style.parentRule = this;
20
+ };
21
+
22
+ CSSOM.CSSStyleRule.prototype = new CSSOM.CSSGroupingRule();
23
+ CSSOM.CSSStyleRule.prototype.constructor = CSSOM.CSSStyleRule;
24
+ CSSOM.CSSStyleRule.prototype.type = 1;
25
+
26
+ Object.defineProperty(CSSOM.CSSStyleRule.prototype, "cssText", {
27
+ get: function() {
28
+ var text;
29
+ if (this.selectorText) {
30
+ var values = ""
31
+ if (this.cssRules.length) {
32
+ var valuesArr = [" {"];
33
+ this.style.cssText && valuesArr.push(this.style.cssText);
34
+ valuesArr.push(this.cssRules.map(function(rule){ return rule.cssText }).join("\n "));
35
+ values = valuesArr.join("\n ") + "\n}"
36
+ } else {
37
+ values = " {" + this.style.cssText + "}";
38
+ }
39
+ text = this.selectorText + values;
40
+ } else {
41
+ text = "";
42
+ }
43
+ return text;
44
+ },
45
+ set: function(cssText) {
46
+ var rule = CSSOM.CSSStyleRule.parse(cssText);
47
+ this.style = rule.style;
48
+ this.selectorText = rule.selectorText;
49
+ }
50
+ });
51
+
52
+
53
+ /**
54
+ * NON-STANDARD
55
+ * lightweight version of parse.js.
56
+ * @param {string} ruleText
57
+ * @return CSSStyleRule
58
+ */
59
+ CSSOM.CSSStyleRule.parse = function(ruleText) {
60
+ var i = 0;
61
+ var state = "selector";
62
+ var index;
63
+ var j = i;
64
+ var buffer = "";
65
+
66
+ var SIGNIFICANT_WHITESPACE = {
67
+ "selector": true,
68
+ "value": true
69
+ };
70
+
71
+ var styleRule = new CSSOM.CSSStyleRule();
72
+ var name, priority="";
73
+
74
+ for (var character; (character = ruleText.charAt(i)); i++) {
75
+
76
+ switch (character) {
77
+
78
+ case " ":
79
+ case "\t":
80
+ case "\r":
81
+ case "\n":
82
+ case "\f":
83
+ if (SIGNIFICANT_WHITESPACE[state]) {
84
+ // Squash 2 or more white-spaces in the row into 1
85
+ switch (ruleText.charAt(i - 1)) {
86
+ case " ":
87
+ case "\t":
88
+ case "\r":
89
+ case "\n":
90
+ case "\f":
91
+ break;
92
+ default:
93
+ buffer += " ";
94
+ break;
95
+ }
96
+ }
97
+ break;
98
+
99
+ // String
100
+ case '"':
101
+ j = i + 1;
102
+ index = ruleText.indexOf('"', j) + 1;
103
+ if (!index) {
104
+ throw '" is missing';
105
+ }
106
+ buffer += ruleText.slice(i, index);
107
+ i = index - 1;
108
+ break;
109
+
110
+ case "'":
111
+ j = i + 1;
112
+ index = ruleText.indexOf("'", j) + 1;
113
+ if (!index) {
114
+ throw "' is missing";
115
+ }
116
+ buffer += ruleText.slice(i, index);
117
+ i = index - 1;
118
+ break;
119
+
120
+ // Comment
121
+ case "/":
122
+ if (ruleText.charAt(i + 1) === "*") {
123
+ i += 2;
124
+ index = ruleText.indexOf("*/", i);
125
+ if (index === -1) {
126
+ throw new SyntaxError("Missing */");
127
+ } else {
128
+ i = index + 1;
129
+ }
130
+ } else {
131
+ buffer += character;
132
+ }
133
+ break;
134
+
135
+ case "{":
136
+ if (state === "selector") {
137
+ styleRule.selectorText = buffer.trim();
138
+ buffer = "";
139
+ state = "name";
140
+ }
141
+ break;
142
+
143
+ case ":":
144
+ if (state === "name") {
145
+ name = buffer.trim();
146
+ buffer = "";
147
+ state = "value";
148
+ } else {
149
+ buffer += character;
150
+ }
151
+ break;
152
+
153
+ case "!":
154
+ if (state === "value" && ruleText.indexOf("!important", i) === i) {
155
+ priority = "important";
156
+ i += "important".length;
157
+ } else {
158
+ buffer += character;
159
+ }
160
+ break;
161
+
162
+ case ";":
163
+ if (state === "value") {
164
+ styleRule.style.setProperty(name, buffer.trim(), priority);
165
+ priority = "";
166
+ buffer = "";
167
+ state = "name";
168
+ } else {
169
+ buffer += character;
170
+ }
171
+ break;
172
+
173
+ case "}":
174
+ if (state === "value") {
175
+ styleRule.style.setProperty(name, buffer.trim(), priority);
176
+ priority = "";
177
+ buffer = "";
178
+ } else if (state === "name") {
179
+ break;
180
+ } else {
181
+ buffer += character;
182
+ }
183
+ state = "selector";
184
+ break;
185
+
186
+ default:
187
+ buffer += character;
188
+ break;
189
+
190
+ }
191
+ }
192
+
193
+ return styleRule;
194
+
195
+ };
196
+
197
+
198
+ //.CommonJS
199
+ exports.CSSStyleRule = CSSOM.CSSStyleRule;
200
+ ///CommonJS
@@ -0,0 +1,88 @@
1
+ //.CommonJS
2
+ var CSSOM = {
3
+ StyleSheet: require("./StyleSheet").StyleSheet,
4
+ CSSStyleRule: require("./CSSStyleRule").CSSStyleRule
5
+ };
6
+ ///CommonJS
7
+
8
+
9
+ /**
10
+ * @constructor
11
+ * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet
12
+ */
13
+ CSSOM.CSSStyleSheet = function CSSStyleSheet() {
14
+ CSSOM.StyleSheet.call(this);
15
+ this.cssRules = [];
16
+ };
17
+
18
+
19
+ CSSOM.CSSStyleSheet.prototype = new CSSOM.StyleSheet();
20
+ CSSOM.CSSStyleSheet.prototype.constructor = CSSOM.CSSStyleSheet;
21
+
22
+
23
+ /**
24
+ * Used to insert a new rule into the style sheet. The new rule now becomes part of the cascade.
25
+ *
26
+ * sheet = new Sheet("body {margin: 0}")
27
+ * sheet.toString()
28
+ * -> "body{margin:0;}"
29
+ * sheet.insertRule("img {border: none}", 0)
30
+ * -> 0
31
+ * sheet.toString()
32
+ * -> "img{border:none;}body{margin:0;}"
33
+ *
34
+ * @param {string} rule
35
+ * @param {number} index
36
+ * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet-insertRule
37
+ * @return {number} The index within the style sheet's rule collection of the newly inserted rule.
38
+ */
39
+ CSSOM.CSSStyleSheet.prototype.insertRule = function(rule, index) {
40
+ if (index < 0 || index > this.cssRules.length) {
41
+ throw new RangeError("INDEX_SIZE_ERR");
42
+ }
43
+ var cssRule = CSSOM.parse(rule).cssRules[0];
44
+ cssRule.parentStyleSheet = this;
45
+ this.cssRules.splice(index, 0, cssRule);
46
+ return index;
47
+ };
48
+
49
+
50
+ /**
51
+ * Used to delete a rule from the style sheet.
52
+ *
53
+ * sheet = new Sheet("img{border:none} body{margin:0}")
54
+ * sheet.toString()
55
+ * -> "img{border:none;}body{margin:0;}"
56
+ * sheet.deleteRule(0)
57
+ * sheet.toString()
58
+ * -> "body{margin:0;}"
59
+ *
60
+ * @param {number} index within the style sheet's rule list of the rule to remove.
61
+ * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet-deleteRule
62
+ */
63
+ CSSOM.CSSStyleSheet.prototype.deleteRule = function(index) {
64
+ if (index < 0 || index >= this.cssRules.length) {
65
+ throw new RangeError("INDEX_SIZE_ERR");
66
+ }
67
+ this.cssRules.splice(index, 1);
68
+ };
69
+
70
+
71
+ /**
72
+ * NON-STANDARD
73
+ * @return {string} serialize stylesheet
74
+ */
75
+ CSSOM.CSSStyleSheet.prototype.toString = function() {
76
+ var result = "";
77
+ var rules = this.cssRules;
78
+ for (var i=0; i<rules.length; i++) {
79
+ result += rules[i].cssText + "\n";
80
+ }
81
+ return result;
82
+ };
83
+
84
+
85
+ //.CommonJS
86
+ exports.CSSStyleSheet = CSSOM.CSSStyleSheet;
87
+ CSSOM.parse = require('./parse').parse; // Cannot be included sooner due to the mutual dependency between parse.js and CSSStyleSheet.js
88
+ ///CommonJS
@@ -0,0 +1,36 @@
1
+ //.CommonJS
2
+ var CSSOM = {
3
+ CSSRule: require("./CSSRule").CSSRule,
4
+ CSSGroupingRule: require("./CSSGroupingRule").CSSGroupingRule,
5
+ CSSConditionRule: require("./CSSConditionRule").CSSConditionRule
6
+ };
7
+ ///CommonJS
8
+
9
+
10
+ /**
11
+ * @constructor
12
+ * @see https://drafts.csswg.org/css-conditional-3/#the-csssupportsrule-interface
13
+ */
14
+ CSSOM.CSSSupportsRule = function CSSSupportsRule() {
15
+ CSSOM.CSSConditionRule.call(this);
16
+ };
17
+
18
+ CSSOM.CSSSupportsRule.prototype = new CSSOM.CSSConditionRule();
19
+ CSSOM.CSSSupportsRule.prototype.constructor = CSSOM.CSSSupportsRule;
20
+ CSSOM.CSSSupportsRule.prototype.type = 12;
21
+
22
+ Object.defineProperty(CSSOM.CSSSupportsRule.prototype, "cssText", {
23
+ get: function() {
24
+ var cssTexts = [];
25
+
26
+ for (var i = 0, length = this.cssRules.length; i < length; i++) {
27
+ cssTexts.push(this.cssRules[i].cssText);
28
+ }
29
+
30
+ return "@supports " + this.conditionText + " {" + cssTexts.join("") + "}";
31
+ }
32
+ });
33
+
34
+ //.CommonJS
35
+ exports.CSSSupportsRule = CSSOM.CSSSupportsRule;
36
+ ///CommonJS
@@ -0,0 +1,43 @@
1
+ //.CommonJS
2
+ var CSSOM = {};
3
+ ///CommonJS
4
+
5
+
6
+ /**
7
+ * @constructor
8
+ * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSValue
9
+ *
10
+ * TODO: add if needed
11
+ */
12
+ CSSOM.CSSValue = function CSSValue() {
13
+ };
14
+
15
+ CSSOM.CSSValue.prototype = {
16
+ constructor: CSSOM.CSSValue,
17
+
18
+ // @see: http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSValue
19
+ set cssText(text) {
20
+ var name = this._getConstructorName();
21
+
22
+ throw new Error('DOMException: property "cssText" of "' + name + '" is readonly and can not be replaced with "' + text + '"!');
23
+ },
24
+
25
+ get cssText() {
26
+ var name = this._getConstructorName();
27
+
28
+ throw new Error('getter "cssText" of "' + name + '" is not implemented!');
29
+ },
30
+
31
+ _getConstructorName: function() {
32
+ var s = this.constructor.toString(),
33
+ c = s.match(/function\s([^\(]+)/),
34
+ name = c[1];
35
+
36
+ return name;
37
+ }
38
+ };
39
+
40
+
41
+ //.CommonJS
42
+ exports.CSSValue = CSSOM.CSSValue;
43
+ ///CommonJS