Skip to content

Commit

Permalink
@media rules support. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
NV committed Nov 1, 2010
1 parent de8d254 commit a4fdfdc
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
1 change: 0 additions & 1 deletion lib/CSSMediaRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var CSSOM = {
CSSOM.CSSMediaRule = function CSSMediaRule() {
this.media = new CSSOM.MediaList;
this.cssRules = [];
this.type = 4;
};

CSSOM.CSSMediaRule.prototype = new CSSOM.CSSRule;
Expand Down
8 changes: 6 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ CSSOM.parse = function parse(token, options) {
case "@":
if (token.indexOf("@media", i) == 0) {
state = "atBlock";
i += "media".length;
break;
} else if (state == "selector") {
state = "atRule";
}
Expand All @@ -114,7 +116,7 @@ CSSOM.parse = function parse(token, options) {
state = "name";
} else if (state == "atBlock") {
mediaRule = new CSSOM.CSSMediaRule;
mediaRule.selectorText = buffer.trim();
mediaRule.media.mediaText = buffer.trim();
currentScope = mediaRule;
buffer = "";
state = "selector";
Expand Down Expand Up @@ -147,7 +149,6 @@ CSSOM.parse = function parse(token, options) {
buffer = "";
state = "name";
} else if (state == "atRule") {
//tokens.push(["atRule", buffer]);
buffer = "";
state = "selector";
} else {
Expand All @@ -167,6 +168,9 @@ CSSOM.parse = function parse(token, options) {
} else if (state == "selector") {
// End of media rule.
// Nesting of media rules isn't supported
if (!mediaRule) {
throw "unexpected }";
}
styleSheet.cssRules.push(mediaRule);
currentScope = styleSheet;
buffer = "";
Expand Down
69 changes: 65 additions & 4 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ var TESTS = [
}
]
}
}, {
},
{
input: "span {display: inline-block !important; vertical-align: middle !important} .error{color:red!important;}",
result: {
cssRules: [
Expand All @@ -206,15 +207,75 @@ var TESTS = [
}
]
}
}, {
input: "@media handheld {}",
},
{
input: "@media handheld, only screen and (max-device-width: 480px) {body{max-width:480px}}",
result: {
cssRules: [
{
media: {
0: "handheld",
1: "only screen and (max-device-width: 480px)",
length: 2
},
cssRules: [
{
selectorText: "body",
style: {
0: "max-width",
"max-width": "480px",
length: 1
}
}
]
}
]
}
},
{
input: "@media screen, screen, screen {/* Match Firefox and Opera behavior here rather than WebKit. \nSane person shouldn't write like this anyway. */}",
result: {
cssRules: [
{

media: {
0: "screen",
1: "screen",
2: "screen",
length: 3
},
cssRules: []
}
]
}
},
{
input: "@media/**/print {*{background:#fff}}",
result: {
cssRules: [
{
media: {
0: "print",
length: 1
},
cssRules: [
{
selectorText: "*",
style: {
0: "background",
background: "#fff",
length: 1
}
}
]
}
]
}
},
{
input: "@mediaall {}",
result: {
cssRules: []
}
}
];

Expand Down

0 comments on commit a4fdfdc

Please sign in to comment.