Skip to content

Commit

Permalink
Fix @font-face being consolidated. Fixes #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Sep 4, 2012
1 parent 667ea75 commit 7a522b5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
32 changes: 24 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ function compress(str, options) {
context(tree.stylesheet);
}

// ### isStyleRule
// Helper to check if a rule is a normal style rule.

function isStyleRule(rule) {
return ((typeof rule.declarations !== 'undefined') &&
(typeof rule.selectors !== 'undefined') &&
(rule.selectors[0] !== '@font-face'));
}

function isMediaRule(rule) {
return (typeof rule.media !== 'undefined');
}

function isKeyframesRule(rule) {
return (typeof rule.keyframes !== 'undefined');
}

// ### context
// Transforms a given tree `context`. A `context` can usually be a media query
// definition, or a stylesheet itself.
Expand All @@ -94,15 +111,14 @@ function compress(str, options) {
// __Pass #1__

tree.rules.forEach(function(rule, i) {

//- Consolidate media queries.
if (typeof rule.media !== 'undefined') {
if (isMediaRule(rule)) {
consolidateMediaQueries(rule, tree.rules, i, mediaCache);
}

//- Compress selectors and declarations.
//- Consolidate rules with same definitions.
if (typeof rule.declarations !== 'undefined') {
if (isStyleRule(rule)) {
styleRule(rule, tree.rules, i);
consolidateViaDeclarations(rule, tree.rules, i, valueCache);
}
Expand All @@ -114,7 +130,7 @@ function compress(str, options) {
tree.rules.forEach(function(rule, i) {

//- Consolidate rules with same selectors.
if (typeof rule.declarations !== 'undefined') {
if (isStyleRule(rule)) {
consolidateViaSelectors(rule, tree.rules, i, selectorCache);
}
});
Expand All @@ -125,18 +141,18 @@ function compress(str, options) {
tree.rules.forEach(function(rule, i) {
//- Consolidate rules with same definitions. Again, to account for the
// updated rules in Pass #2.
if (typeof rule.declarations !== 'undefined') {
if (isStyleRule(rule)) {
consolidateViaDeclarations(rule, tree.rules, i, valueCache);
rule.selectors = undupeSelectors(rule.selectors);
}

//- Recurse through media queries.
if (typeof rule.media !== 'undefined') {
if (isMediaRule(rule)) {
rule = context(rule);
}

//- Recurse through at keyframes.
if (typeof rule.keyframes !== 'undefined') {
if (isKeyframesRule(rule)) {
rule.keyframes.forEach(function(keyframe, i) {
styleRule(keyframe, rule.keyframes, i);
});
Expand Down Expand Up @@ -287,7 +303,7 @@ function compress(str, options) {
}

//- Compress its selectors.
if (typeof rule.selectors !== 'undefined') {
if (isStyleRule(rule)) {
rule.selectors = sortSelectors(rule.selectors.map(compressSelector));
}

Expand Down
8 changes: 8 additions & 0 deletions test/cases/multiple_font_face.in.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@font-face {
font-family: avenir;
src: url(x);
}
@font-face {
font-family: lato;
src: url(y);
}
1 change: 1 addition & 0 deletions test/cases/multiple_font_face.out.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@font-face{font-family:avenir;src:url(x)}@font-face{font-family:lato;src:url(y)}

0 comments on commit 7a522b5

Please sign in to comment.