Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmarineau committed Aug 11, 2014
0 parents commit 396567c
Show file tree
Hide file tree
Showing 109 changed files with 12,377 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/engine');
49 changes: 49 additions & 0 deletions lib/engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var css = require('css');
var shorthands = require('./properties');

module.exports = function(cssString) {
var cssObject = css.parse(cssString);
var longPropertiesPositions = [];

cssObject.stylesheet.rules.forEach(function(rule) {
if (rule.type === "rule") {
var declarations = [];

rule.declarations.forEach(function(declaration) {
declarations[declaration.property] = declaration;
});

shorthands.forEach(function(shorthand) {
var shorthandValue = shorthand.getShorthandValue(shorthand, declarations);

if (shorthandValue !== "") {
var newDeclarations = [];

// Add the new shorthand property
newDeclarations.push({
type: 'declaration',
property: shorthand.shorthandProperty,
value: shorthandValue
});

// Add properties having nothing to do with the property being shorthanded
rule.declarations.forEach(function(declaration) {
if (shorthand.properties.indexOf(declaration.property) <= -1) {
newDeclarations.push(declaration);
}
else {
longPropertiesPositions.push(declaration.position);
}
});

rule.declarations = newDeclarations;
}
});
}
});

return {
string: css.stringify(cssObject),
longPropertiesPositions: longPropertiesPositions
};
};
58 changes: 58 additions & 0 deletions lib/properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This is the default function that is used to convert most properties to their shorthand version. It simply replaces
var basicShorthandReplace = function(shorthand, declarations) {
var shorthandValue = shorthand.shorthandSyntax;

shorthand.properties.forEach(function(longProperty) {
if (declarations[longProperty]) {
shorthandValue = shorthandValue.replace(longProperty, declarations[longProperty].value);
}
else {
shorthandValue = shorthandValue.replace(longProperty, '');
}
});

shorthandValue = shorthandValue.replace(' ', ' ').trim();
return shorthandValue;
};

// This array defines all the shorthand properties and their details
module.exports = [{
shorthandProperty: 'background',
properties: ['background-color', 'background-image', 'background-repeat', 'background-position', 'background-attachment'],
shorthandSyntax: 'background-color background-image background-repeat background-position background-attachment',
getShorthandValue: basicShorthandReplace
}, {
shorthandProperty: 'font',
properties: ['font-style', 'font-variant', 'font-weight', 'font-size', 'line-height', 'font-family'],
shorthandSyntax: 'font-style font-variant font-weight font-size/line-height font-family',
getShorthandValue: function(shorthand, declarations) {
var shorthandValue = basicShorthandReplace(shorthand, declarations);

// We must at least have size & family or else the shorthand will get ignored by the browser
if (!declarations['font-size'] || !declarations['font-family']) {
return "";
}

// Remove slash if line-height is not specified
if (!declarations['line-height']) {
shorthandValue = shorthandValue.replace('/', '');
}

return shorthandValue.replace(' ', ' ').trim();
}
}, {
shorthandProperty: 'border',
properties: ['border-width', 'border-style', 'border-color'],
shorthandSyntax: 'border-width border-style border-color',
getShorthandValue: basicShorthandReplace
}, {
shorthandProperty: 'outline',
properties: ['outline-width', 'outline-style', 'outline-color'],
shorthandSyntax: 'outline-width outline-style outline-color',
getShorthandValue: basicShorthandReplace
}, {
shorthandProperty: 'list-style',
properties: ['list-style-type', 'list-style-position', 'list-style-image'],
shorthandSyntax: 'list-style-type list-style-position list-style-image',
getShorthandValue: basicShorthandReplace
}];
Loading

0 comments on commit 396567c

Please sign in to comment.