[svn r30] font shorthand trunk
authordglazman
Mon, 15 Mar 2010 07:02:45 -0500
branchtrunk
changeset 26 c647ac6276c1
parent 25 ed15689c9962
child 27 f4a1067b7074
[svn r30] font shorthand
cssParser.js
--- a/cssParser.js	Mon Mar 15 06:16:44 2010 -0500
+++ b/cssParser.js	Mon Mar 15 07:02:45 2010 -0500
@@ -1414,6 +1414,147 @@
     return bgColor + " " + bgImage + " " + bgRepeat + " " + bgAttachment + " " + bgPosition;
   },
 
+  parseFontShorthand: function(token, aDecl, aAcceptPriority)
+  {
+    const kStyle = {"italic": true, "oblique": true };
+    const kVariant = {"small-caps": true };
+    const kWeight = { "bold": true, "bolder": true, "lighter": true,
+                      "100": true, "200": true, "300": true, "400": true,
+                      "500": true, "600": true, "700": true, "800": true,
+                      "900": true };
+    const kSize = { "xx-small": true, "x-small": true, "small": true, "medium": true,
+                    "large": true, "x-large": true, "xx-large": true,
+                    "larger": true, "smaller": true };
+    const kValues = { "caption": true, "icon": true, "menu": true, "message-box": true, "small-caption": true, "status-bar": true };
+    const kFamily = { "serif": true, "sans-serif": true, "cursive": true, "fantasy": true, "monospace": true };
+
+    var fStyle = null;
+    var fVariant = null;
+    var fWeight = null;
+    var fSize = null;
+    var fLineHeight = null;
+    var fFamily = null;
+    var fSystem = null;
+
+    var normalCount = 0;
+    while (true) {
+
+      if (!token.isNotNull())
+        break;
+
+      if (token.isSymbol(";")
+          || (aAcceptPriority && token.isSymbol("!"))
+          || token.isSymbol("}")) {
+        if (token.isSymbol("}"))
+          this.ungetToken();
+        break;
+      }
+
+      else {
+        if (!fSystem && (token.isIdent() && token.value in kValues)) {
+          fSystem = token.value;
+          break;
+        }
+
+        else {
+	        if (!fStyle
+	                 && token.isIdent()
+	                 && (token.value in kStyle)) {
+	          fStyle = token.value;
+	        }
+	
+	        else if (!fVariant
+	                 && token.isIdent()
+	                 && (token.value in kVariant)) {
+	          fVariant = token.value;
+	        }
+	
+	        else if (!fWeight
+	                 && (token.isIdent() || token.isNumber())
+	                 && (token.value in kWeight)) {
+	          fWeight = token.value;
+	        }
+	
+	        else if (!fSize
+	                 && ((token.isIdent() && (token.value in kSize))
+	                     || token.isDimension())) {
+	          fSize = token.value;
+	          var token = this.getToken(false, false);
+	          if (nextToken.isSymbol("/")) {
+	            token = this.getToken(false, false);
+	            if (!fLineHeight &&
+	                (token.isDimension() || token.isNumber() || token.isPercentage())) {
+	              fLineheight = token.value;
+	            }
+	            else
+	              return "";
+	          }
+	          else
+	            this.ungetToken();
+	        }
+
+          else if (token.isIdent("normal")) {
+            normalCount++;
+            if (normalCount > 3)
+              return "";
+          }
+
+          else if (!fFamily && // *MUST* be last to be tested here
+                   (token.isString()
+                    || token.isIdent())) {
+            var lastWasComma = false;
+            while (true) {
+              if (!token.isNotNull())
+                break;
+              else if (token.isIdent() && token.value in kFamily) {
+                fFamily += token.value;
+                break;
+              }
+              else if (token.isString() || token.isIdent()) {
+                fFamily += token.value;
+                lastWasComma = false;
+              }
+              else if (!lastWasComma && token.isSymbol(",")) {
+                fFamily += ", ";
+                lastWasComma = true;
+              }
+              else
+                return "";
+            }
+          }
+
+          else {
+            return "";
+	        }
+        }
+
+      }
+
+      token = this.getToken(true, true);
+    }
+
+    // create the declarations
+    this.mScanner.forgetState();
+    if (fSystem) {
+      aDecl.push(this._createJscsspDeclaration("font", fSystem));
+      return fSystem;
+    }
+    fStyle = fStyle ? fStyle : "normal";
+    fVariant = fVariant ? fVariant : "normal";
+    fWeight = fWeight ? fWeight : "normal";
+    fSize = fSize ? fSize : "medium";
+    fLineHeight = fLineHeight ? fLineHeight : "normal";
+    fFamily = fFamily ? fFamily : "-moz-initial";
+
+    aDecl.push(this._createJscsspDeclaration("font-style", fStyle));
+    aDecl.push(this._createJscsspDeclaration("font-variant", fVariant));
+    aDecl.push(this._createJscsspDeclaration("font-weight", fWeight));
+    aDecl.push(this._createJscsspDeclaration("font-size", fSize));
+    aDecl.push(this._createJscsspDeclaration("line-height", fLineHeight));
+    aDecl.push(this._createJscsspDeclaration("font-family", fFamily));
+    return fStyle + " " + fVariant + " " + fWeight + " " + fSize + "/" + fLineHeight + " " + fFamily;
+  },
+
   _createJscsspDeclaration: function(property, value)
   {
     var decl = new jscsspDeclaration();
@@ -1606,9 +1747,11 @@
             value = this.parseCueShorthand(token, declarations, aAcceptPriority);
             break;
           case "pause":
-          case "cue":
             value = this.parsePauseShorthand(token, declarations, aAcceptPriority);
             break;
+          case "font":
+            value = this.parseFontShorthand(token, declarations, aAcceptPriority);
+            break;
           default:
             value = this.parseDefaultPropertyValue(token, declarations, aAcceptPriority, descriptor);
             break;