Permalink
Browse files

Merge pull request #34 from papandreou/fontface

@font-face support.
  • Loading branch information...
2 parents 76f68a2 + 59c2eda commit 9e843b8ff5cc9c14b929d7aa1b7b7d1eb799d299 Nikita Vasilyev committed Feb 18, 2012
Showing with 78 additions and 2 deletions.
  1. +34 −0 lib/CSSFontFaceRule.js
  2. +18 −2 lib/parse.js
  3. +25 −0 spec/parse.spec.js
  4. +1 −0 src/files.js
@@ -0,0 +1,34 @@
+//.CommonJS
+var CSSOM = {
+ CSSStyleDeclaration: require("./CSSStyleDeclaration").CSSStyleDeclaration,
+ CSSRule: require("./CSSRule").CSSRule
+};
+///CommonJS
+
+
+/**
+ * @constructor
+ * @see http://dev.w3.org/csswg/cssom/#css-font-face-rule
+ */
+CSSOM.CSSFontFaceRule = function CSSFontFaceRule() {
+ CSSOM.CSSRule.call(this);
+ this.style = new CSSOM.CSSStyleDeclaration;
+ this.style.parentRule = this;
+};
+
+CSSOM.CSSFontFaceRule.prototype = new CSSOM.CSSRule;
+CSSOM.CSSFontFaceRule.prototype.constructor = CSSOM.CSSFontFaceRule;
+CSSOM.CSSFontFaceRule.prototype.type = 5;
+//FIXME
+//CSSOM.CSSFontFaceRule.prototype.insertRule = CSSStyleSheet.prototype.insertRule;
+//CSSOM.CSSFontFaceRule.prototype.deleteRule = CSSStyleSheet.prototype.deleteRule;
+
+// http://www.opensource.apple.com/source/WebCore/WebCore-955.66.1/css/WebKitCSSFontFaceRule.cpp
+CSSOM.CSSFontFaceRule.prototype.__defineGetter__("cssText", function() {
+ return "@font-face {" + this.style.cssText + "}";
+});
+
+
+//.CommonJS
+exports.CSSFontFaceRule = CSSOM.CSSFontFaceRule;
+///CommonJS
View
@@ -4,6 +4,7 @@ var CSSOM = {
CSSStyleRule: require("./CSSStyleRule").CSSStyleRule,
CSSImportRule: require("./CSSImportRule").CSSImportRule,
CSSMediaRule: require("./CSSMediaRule").CSSMediaRule,
+ CSSFontFaceRule: require("./CSSFontFaceRule").CSSFontFaceRule,
CSSStyleDeclaration: require('./CSSStyleDeclaration').CSSStyleDeclaration,
CSSKeyframeRule: require('./CSSKeyframeRule').CSSKeyframeRule,
CSSKeyframesRule: require('./CSSKeyframesRule').CSSKeyframesRule
@@ -44,13 +45,13 @@ CSSOM.parse = function parse(token) {
var styleSheet = new CSSOM.CSSStyleSheet;
- // @type CSSStyleSheet|CSSMediaRule|CSSKeyframesRule
+ // @type CSSStyleSheet|CSSMediaRule|CSSFontFaceRule|CSSKeyframesRule
var currentScope = styleSheet;
// @type CSSMediaRule|CSSKeyframesRule
var parentRule;
- var selector, name, value, priority="", styleRule, mediaRule, importRule, keyframesRule, keyframeRule;
+ var selector, name, value, priority="", styleRule, mediaRule, importRule, fontFaceRule, keyframesRule, keyframeRule;
var atKeyframesRegExp = /@(-(?:\w+-)+)?keyframes/g;
@@ -136,6 +137,13 @@ CSSOM.parse = function parse(token) {
i += "import".length;
buffer += "@import";
break;
+ } else if (token.indexOf("@font-face", i) === i) {
+ state = "fontFaceRule-begin";
+ i += "font-face".length;
+ fontFaceRule = new CSSOM.CSSFontFaceRule;
+ fontFaceRule.__starts = i;
+ buffer = "";
+ break;
} else {
atKeyframesRegExp.lastIndex = i;
var matchKeyframes = atKeyframesRegExp.exec(token);
@@ -166,6 +174,14 @@ CSSOM.parse = function parse(token) {
mediaRule.parentStyleSheet = styleSheet;
buffer = "";
state = "before-selector";
+ } else if (state === "fontFaceRule-begin") {
+ if (parentRule) {
+ fontFaceRule.parentRule = parentRule;
+ }
+ fontFaceRule.parentStyleSheet = styleSheet;
+ styleRule = fontFaceRule;
+ buffer = "";
+ state = "before-name";
} else if (state === "keyframesRule-begin") {
keyframesRule.name = buffer.trim();
if (parentRule) {
View
@@ -708,6 +708,31 @@ var TESTS = [
})()
},
{
+ input: "@font-face { font-family: Delicious; font-weight: bold; src: url('Delicious-Bold.otf'); }",
+ result: (function() {
+ var result = {
+ cssRules: [
+ {
+ parentRule: null,
+ style: {
+ 0: 'font-family',
+ 1: 'font-weight',
+ 2: 'src',
+ 'font-family': 'Delicious',
+ 'font-weight': 'bold',
+ 'src': 'url(\'Delicious-Bold.otf\')',
+ length: 3
+ }
+ }
+ ],
+ parentStyleSheet: null
+ };
+ result.cssRules[0].parentStyleSheet = result;
+ result.cssRules[0].style.parentRule = result.cssRules[0];
+ return result;
+ })()
+ },
+ {
// Non-vendor prefixed @keyframes rule, from Twitter Bootstrap (progress-bars):
input: '@keyframes progress-bar-stripes {\n from { background-position: 0 0; }\n to { background-position: 40px 0; }\n}',
result: (function () {
View
@@ -5,6 +5,7 @@ exports.files = [
"MediaList",
"CSSMediaRule",
"CSSImportRule",
+ "CSSFontFaceRule",
"StyleSheet",
"CSSStyleSheet",
"CSSKeyframesRule",

0 comments on commit 9e843b8

Please sign in to comment.