Skip to content

Commit 56d0145

Browse files
MunterNV
authored andcommitted
Implemented support for @host blocks. (#72)
Implemented support for @host blocks.
1 parent bd5fccc commit 56d0145

4 files changed

Lines changed: 79 additions & 1 deletion

File tree

lib/CSSHostRule.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//.CommonJS
2+
var CSSOM = {
3+
CSSRule: require("./CSSRule").CSSRule
4+
};
5+
///CommonJS
6+
7+
8+
/**
9+
* @constructor
10+
* @see http://www.w3.org/TR/shadow-dom/#host-at-rule
11+
*/
12+
CSSOM.CSSHostRule = function CSSHostRule() {
13+
CSSOM.CSSRule.call(this);
14+
this.cssRules = [];
15+
};
16+
17+
CSSOM.CSSHostRule.prototype = new CSSOM.CSSRule();
18+
CSSOM.CSSHostRule.prototype.constructor = CSSOM.CSSHostRule;
19+
CSSOM.CSSHostRule.prototype.type = 1001;
20+
//FIXME
21+
//CSSOM.CSSHostRule.prototype.insertRule = CSSStyleSheet.prototype.insertRule;
22+
//CSSOM.CSSHostRule.prototype.deleteRule = CSSStyleSheet.prototype.deleteRule;
23+
24+
Object.defineProperty(CSSOM.CSSHostRule.prototype, "cssText", {
25+
get: function() {
26+
var cssTexts = [];
27+
for (var i=0, length=this.cssRules.length; i < length; i++) {
28+
cssTexts.push(this.cssRules[i].cssText);
29+
}
30+
return "@host {" + cssTexts.join("") + "}";
31+
}
32+
});
33+
34+
35+
//.CommonJS
36+
exports.CSSHostRule = CSSOM.CSSHostRule;
37+
///CommonJS

lib/parse.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ CSSOM.parse = function parse(token) {
4343
// @type CSSMediaRule|CSSKeyframesRule|CSSDocumentRule
4444
var parentRule;
4545

46-
var name, priority="", styleRule, mediaRule, importRule, fontFaceRule, keyframesRule, documentRule;
46+
var name, priority="", styleRule, mediaRule, importRule, fontFaceRule, keyframesRule, documentRule, hostRule;
4747

4848
var atKeyframesRegExp = /@(-(?:\w+-)+)?keyframes/g;
4949

@@ -149,6 +149,13 @@ CSSOM.parse = function parse(token) {
149149
i += "media".length;
150150
buffer = "";
151151
break;
152+
} else if (token.indexOf("@host", i) === i) {
153+
state = "hostRule-begin";
154+
i += "host".length;
155+
hostRule = new CSSOM.CSSHostRule();
156+
hostRule.__starts = i;
157+
buffer = "";
158+
break;
152159
} else if (token.indexOf("@import", i) === i) {
153160
state = "importRule-begin";
154161
i += "import".length;
@@ -191,6 +198,11 @@ CSSOM.parse = function parse(token) {
191198
mediaRule.parentStyleSheet = styleSheet;
192199
buffer = "";
193200
state = "before-selector";
201+
} else if (state === "hostRule-begin") {
202+
currentScope = parentRule = hostRule;
203+
hostRule.parentStyleSheet = styleSheet;
204+
buffer = "";
205+
state = "before-selector";
194206
} else if (state === "fontFaceRule-begin") {
195207
if (parentRule) {
196208
fontFaceRule.parentRule = parentRule;
@@ -373,6 +385,7 @@ CSSOM.CSSStyleRule = require("./CSSStyleRule").CSSStyleRule;
373385
CSSOM.CSSImportRule = require("./CSSImportRule").CSSImportRule;
374386
CSSOM.CSSMediaRule = require("./CSSMediaRule").CSSMediaRule;
375387
CSSOM.CSSFontFaceRule = require("./CSSFontFaceRule").CSSFontFaceRule;
388+
CSSOM.CSSHostRule = require("./CSSHostRule").CSSHostRule;
376389
CSSOM.CSSStyleDeclaration = require('./CSSStyleDeclaration').CSSStyleDeclaration;
377390
CSSOM.CSSKeyframeRule = require('./CSSKeyframeRule').CSSKeyframeRule;
378391
CSSOM.CSSKeyframesRule = require('./CSSKeyframesRule').CSSKeyframesRule;

spec/parse.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,33 @@ var TESTS = [
754754
return result;
755755
})()
756756
},
757+
{
758+
input: "@host { body { background: red; } }",
759+
result: (function() {
760+
var result = {
761+
cssRules: [
762+
{
763+
cssRules: {
764+
0: {
765+
selectorText: "body",
766+
style: {
767+
0: "background",
768+
length: 1,
769+
parentRule: "..",
770+
background: "red"
771+
}
772+
}
773+
},
774+
parentRule: null
775+
}
776+
],
777+
parentStyleSheet: null
778+
};
779+
result.cssRules[0].parentStyleSheet = result.cssRules[0].cssRules[0].parentStyleSheet = result;
780+
result.cssRules[0].cssRules[0].parentRule = result.cssRules[0];
781+
return result;
782+
})()
783+
},
757784
{
758785
// Non-vendor prefixed @keyframes rule, from Twitter Bootstrap (progress-bars):
759786
input: '@keyframes progress-bar-stripes {\n from { background-position: 0 0; }\n to { background-position: 40px 0; }\n}',

src/files.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ exports.files = [
66
"CSSMediaRule",
77
"CSSImportRule",
88
"CSSFontFaceRule",
9+
"CSSHostRule",
910
"StyleSheet",
1011
"CSSStyleSheet",
1112
"CSSKeyframesRule",

0 commit comments

Comments
 (0)