Permalink
Browse files

Merge pull request #35 from fracmak/calc_support

added support for calc wherever <length> is expected
  • Loading branch information...
2 parents a2aed01 + ebf3f4a commit 8ca2d38b0cea42510b4fca7f5f7ab9d4bc10f82c @nzakas nzakas committed Nov 16, 2012
Showing with 21 additions and 8 deletions.
  1. +10 −7 src/css/Parser.js
  2. +5 −1 src/css/ValidationTypes.js
  3. +1 −0 tests/css/CSSTokensTests.htm
  4. +1 −0 tests/css/TokenStream.js
  5. +4 −0 tests/css/Validation.js
View
@@ -708,18 +708,21 @@ Parser.prototype = function(){
});
},
- _operator: function(){
+ _operator: function(inFunction){
/*
- * operator
+ * operator (outside function)
* : '/' S* | ',' S* | /( empty )/
+ * operator (inside function)
+ * : '/' S* | '+' S* | '*' S* | '-' S* /( empty )/
* ;
*/
var tokenStream = this._tokenStream,
token = null;
- if (tokenStream.match([Tokens.SLASH, Tokens.COMMA])){
+ if (tokenStream.match([Tokens.SLASH, Tokens.COMMA]) ||
+ (inFunction && tokenStream.match([Tokens.PLUS, Tokens.STAR, Tokens.MINUS]))){
token = tokenStream.token();
this._readWhitespace();
}
@@ -1506,7 +1509,7 @@ Parser.prototype = function(){
return result;
},
- _expr: function(){
+ _expr: function(inFunction){
/*
* expr
* : term [ operator term ]*
@@ -1525,8 +1528,8 @@ Parser.prototype = function(){
values.push(value);
do {
- operator = this._operator();
-
+ operator = this._operator(inFunction);
+
//if there's an operator, keep building up the value parts
if (operator){
values.push(operator);
@@ -1662,7 +1665,7 @@ Parser.prototype = function(){
if (tokenStream.match(Tokens.FUNCTION)){
functionText = tokenStream.token().value;
this._readWhitespace();
- expr = this._expr();
+ expr = this._expr(true);
functionText += expr;
//START: Horrible hack in case it's an IE filter
@@ -121,7 +121,11 @@ var ValidationTypes = {
},
"<length>": function(part){
- return part.type == "length" || part.type == "number" || part.type == "integer" || part == "0";
+ if (part.type == "function" && /^(?:\-(?:ms|moz|o|webkit)\-)?calc/i.test(part)){
+ return true;
+ }else{
+ return part.type == "length" || part.type == "number" || part.type == "integer" || part == "0";
+ }
},
"<color>": function(part){
@@ -420,6 +420,7 @@
//regular CSS functions
"rgb(255,0,1)" : [CSSTokens.FUNCTION, CSSTokens.NUMBER, CSSTokens.COMMA, CSSTokens.NUMBER, CSSTokens.COMMA, CSSTokens.NUMBER, CSSTokens.RPAREN],
"counter(par-num,upper-roman)" : [CSSTokens.FUNCTION, CSSTokens.IDENT, CSSTokens.COMMA, CSSTokens.IDENT, CSSTokens.RPAREN],
+ "calc(100% - 5px)" : [CSSTokens.FUNCTION, CSSTokens.PERCENTAGE, CSSTokens.S, CSSTokens.MINUS, CSSTokens.S, CSSTokens.LENGTH, CSSTokens.RPAREN],
//old-style IE filters - interpreted as bunch of tokens
"alpha(opacity=50)" : [CSSTokens.FUNCTION, CSSTokens.IDENT, CSSTokens.EQUALS, CSSTokens.NUMBER, CSSTokens.RPAREN],
View
@@ -405,6 +405,7 @@
//regular CSS functions
"rgb(255,0,1)" : [CSSTokens.FUNCTION, CSSTokens.NUMBER, CSSTokens.COMMA, CSSTokens.NUMBER, CSSTokens.COMMA, CSSTokens.NUMBER, CSSTokens.RPAREN],
"counter(par-num,upper-roman)" : [CSSTokens.FUNCTION, CSSTokens.IDENT, CSSTokens.COMMA, CSSTokens.IDENT, CSSTokens.RPAREN],
+ "calc(100% - 5px)" : [CSSTokens.FUNCTION, CSSTokens.PERCENTAGE, CSSTokens.S, CSSTokens.MINUS, CSSTokens.S, CSSTokens.LENGTH, CSSTokens.RPAREN],
//old-style IE filters - interpreted as bunch of tokens
"alpha(opacity=50)" : [CSSTokens.FUNCTION, CSSTokens.IDENT, CSSTokens.EQUALS, CSSTokens.NUMBER, CSSTokens.RPAREN],
View
@@ -492,6 +492,10 @@
valid: [
"1px",
"1%",
+ "calc(100% - 5px)",
+ "calc(100% + 1em)",
+ "calc(100%/6)",
+ "calc(10%*6)",
"inherit"
],

0 comments on commit 8ca2d38

Please sign in to comment.