Skip to content
Browse files

refactored everything into compile function & added more tests

  • Loading branch information...
1 parent e841121 commit 590f2cd567d586d6e29729034b57ce2a360f4953 @khoomeister committed Jun 5, 2011
Showing with 162 additions and 129 deletions.
  1. +2 −1 .npmignore
  2. +41 −30 lib/coffee-css.js
  3. +1 −1 package.json
  4. +31 −25 src/coffee-css.coffee
  5. +87 −19 test/test.coffee
  6. +0 −30 test/test.css
  7. +0 −23 test/test.css.coffee
View
3 .npmignore
@@ -2,4 +2,5 @@
.idea/
Cakefile
src/
-test/
+test/
+README.md
View
71 lib/coffee-css.js
@@ -1,27 +1,27 @@
(function() {
- var coffee, fs, getDefaultMixins, util, vm, _;
+ var coffee, compileObject, fs, getDefaultMixins, util, vm, _;
coffee = require('coffee-script');
fs = require('fs');
_ = require('underscore');
util = require('util');
vm = require('vm');
- exports.compileFile = function(options) {
- var fileName, settings;
- settings = {
- afterWriteFile: function(fileName, newFileName) {},
- errorWriteFile: function(err, fileName, newFileName) {},
- fileName: void 0,
- newFilename: void 0
- };
- if (typeof options === 'string') {
- fileName = options;
- } else {
- _.extend(settings, options);
- fileName = settings.fileName;
+ exports.compile = function(fileName, newFileName, callback) {
+ if (callback == null) {
+ callback = function(err, fileName, newFileName) {};
+ }
+ if (typeof fileName === 'object') {
+ return compileObject(fileName, newFileName);
+ }
+ if (typeof newFileName === 'function') {
+ callback = newFileName;
+ newFileName = void 0;
}
- return fs.readFile(fileName, function(err, data) {
- var css, js, newFileName, sandbox;
- try {
+ if (newFileName === void 0) {
+ newFileName = fileName.replace(/\.css\.coffee$/i, '.css');
+ }
+ try {
+ return fs.readFile(fileName, function(err, data) {
+ var css, js, sandbox;
js = coffee.compile(data.toString().trim());
sandbox = {
exports: {
@@ -31,46 +31,51 @@
require: require
};
vm.runInNewContext(js, sandbox);
- css = exports.compileObject(sandbox.exports.css, sandbox.exports.mixins);
- newFileName = settings.newFilename || fileName.replace(/\.css\.coffee$/i, '.css');
- return fs.writeFile(newFileName, css, settings.afterWriteFile(fileName, newFileName));
- } catch (err) {
- return settings.errorWriteFile(err, fileName, newFileName);
- }
- });
+ css = compileObject(sandbox.exports.css, sandbox.exports.mixins);
+ return fs.writeFile(newFileName, css, function(err) {
+ if (err) {
+ throw err;
+ }
+ return callback(null, fileName, newFileName);
+ });
+ });
+ } catch (err) {
+ return callback(err, fileName, newFileName);
+ }
};
- exports.compileObject = function(cssObject, mixins) {
+ exports.getDefaultMixins = getDefaultMixins;
+ compileObject = function(cssObject, mixins) {
var compileCssList, outputList;
mixins || (mixins = getDefaultMixins());
outputList = [];
compileCssList = function(cssObject) {
- var declarationText, declarations, iterateDeclarations, nestedSelectors, selector, _results;
+ var declarationText, declarations, isNested, iterateDeclarations, nestedSelectors, selector, _results;
_results = [];
for (selector in cssObject) {
declarations = cssObject[selector];
declarationText = [];
nestedSelectors = {};
+ isNested = false;
iterateDeclarations = function(d) {
var cssProperty, key, value, _results2;
_results2 = [];
for (key in d) {
value = d[key];
- _results2.push(typeof value === 'object' ? nestedSelectors["" + selector + " " + key] = value : mixins[key] ? iterateDeclarations(mixins[key](value)) : (cssProperty = key.replace(/[A-Z]/g, function(s) {
+ _results2.push(typeof value === 'object' ? (nestedSelectors["" + selector + " " + key] = value, isNested = true) : mixins[key] ? iterateDeclarations(mixins[key](value)) : (cssProperty = key.replace(/[A-Z]/g, function(s) {
return '-' + s.toLowerCase();
- }), declarationText.push(" " + cssProperty + ": " + value + ";")));
+ }), declarationText.push(" " + cssProperty + ": " + value + ";")));
}
return _results2;
};
iterateDeclarations(declarations);
outputList.push("" + selector + " {\n" + (declarationText.join('\n')) + "\n}");
- _results.push(compileCssList(nestedSelectors));
+ _results.push(isNested ? compileCssList(nestedSelectors) : void 0);
}
return _results;
};
compileCssList(cssObject);
return outputList.join('\n');
};
- exports.getDefaultMixins = getDefaultMixins;
getDefaultMixins = function() {
return {
boxShadowAll: function(value) {
@@ -79,6 +84,12 @@
MozBoxShadow: value,
boxShadow: value
};
+ },
+ opacityAll: function(value) {
+ return {
+ filter: 'alpha(opacity=' + (value * 100) + ')',
+ opacity: value
+ };
}
};
};
View
2 package.json
@@ -2,7 +2,7 @@
"name": "coffee-css",
"description": "More CSS for CoffeeScript",
"author": "khoomeister",
- "version": "0.0.3",
+ "version": "0.0.4",
"licenses": [{
"type": "MIT",
"url": "https://github.com/aeosynth/ccss/raw/master/LICENSE"
View
56 src/coffee-css.coffee
@@ -6,66 +6,72 @@ util = require 'util'
vm = require 'vm'
# public functions
-exports.compileFile = (options) ->
- settings =
- afterWriteFile: (fileName, newFileName) ->
- errorWriteFile: (err, fileName, newFileName) ->
- fileName: undefined
- newFilename: undefined
+exports.compile = (fileName, newFileName, callback = (err, fileName, newFileName) ->) ->
+ if typeof fileName is 'object'
+ # assume compile object (cssObject, mixins)
+ return compileObject fileName, newFileName
- if typeof options is 'string'
- fileName = options
- else
- _.extend settings, options
- fileName = settings.fileName
+ if typeof newFileName is 'function'
+ callback = newFileName
+ newFileName = undefined
+ if newFileName is undefined
+ newFileName = fileName.replace /\.css\.coffee$/i, '.css'
- fs.readFile fileName, (err, data) ->
- try
+ try
+ fs.readFile fileName, (err, data) ->
js = coffee.compile(data.toString().trim())
sandbox =
exports:
css: {}
mixins: getDefaultMixins()
require: require
vm.runInNewContext js, sandbox
- css = exports.compileObject sandbox.exports.css, sandbox.exports.mixins
- newFileName = settings.newFilename || fileName.replace /\.css\.coffee$/i, '.css'
- fs.writeFile newFileName, css, settings.afterWriteFile(fileName, newFileName)
- catch err
- settings.errorWriteFile(err, fileName, newFileName)
+
+ css = compileObject sandbox.exports.css, sandbox.exports.mixins
+ fs.writeFile newFileName, css, (err) ->
+ if err then throw err
+ callback null, fileName, newFileName
+ catch err
+ callback err, fileName, newFileName
-exports.compileObject = (cssObject, mixins) ->
+exports.getDefaultMixins = getDefaultMixins
+
+# privates
+compileObject = (cssObject, mixins) ->
mixins or= getDefaultMixins()
outputList = []
compileCssList = (cssObject) ->
for selector, declarations of cssObject
declarationText = []
nestedSelectors = {}
+ isNested = false
iterateDeclarations = (d) ->
for key, value of d
if typeof value is 'object'
nestedSelectors["#{selector} #{key}"] = value
+ isNested = true
else
if mixins[key]
iterateDeclarations mixins[key](value)
else
cssProperty = key.replace /[A-Z]/g, (s) -> '-' + s.toLowerCase()
- declarationText.push " #{cssProperty}: #{value};"
+ declarationText.push " #{cssProperty}: #{value};"
iterateDeclarations declarations
outputList.push "#{selector} {\n#{declarationText.join('\n')}\n}"
- compileCssList nestedSelectors
+ if isNested
+ compileCssList nestedSelectors
compileCssList cssObject
outputList.join '\n'
-exports.getDefaultMixins = getDefaultMixins
-
-# privates
getDefaultMixins = ->
boxShadowAll: (value) ->
WebkitBoxShadow: value
MozBoxShadow: value
- boxShadow: value
+ boxShadow: value
+ opacityAll: (value) ->
+ filter: 'alpha(opacity=' + (value * 100) + ')'
+ opacity: value
View
106 test/test.coffee
@@ -1,33 +1,101 @@
# requires
-require 'should'
-
coffeeCss = require '../lib/coffee-css'
fs = require 'fs'
path = require 'path'
+should = require 'should'
-# functions
-compileCssTest = ->
- coffeeCss.compileFile
- fileName: __dirname + '/test.css.coffee'
+# constants
+COFFEE_CSS_SAMPLE_FILE_DATA = """
+ exports.css =
+ 'a':
+ textDecoration: 'none'
+ 'a:hover':
+ textDecoration: 'underline'
+ """
# tests
exports.testBoxShadowAll = ->
- css = coffeeCss.compileObject
+ css = coffeeCss.compile
div:
boxShadowAll: '5px'
css.should.equal """
div {
- -webkit-box-shadow: 5px;
- -moz-box-shadow: 5px;
- box-shadow: 5px;
+ -webkit-box-shadow: 5px;
+ -moz-box-shadow: 5px;
+ box-shadow: 5px;
+ }
+ """
+
+exports.testCustomMixins = ->
+ cssObject =
+ div:
+ boxShadowAll: '5px'
+ marginAndPadding: '10px'
+ css = coffeeCss.compile cssObject,
+ marginAndPadding: (value) ->
+ margin: value
+ padding: value
+
+ css.should.equal """
+ div {
+ box-shadow-all: 5px;
+ margin: 10px;
+ padding: 10px;
+ }
+ """
+
+exports.testNestedObject = ->
+ css = coffeeCss.compile
+ form:
+ margin: '5px'
+
+ input:
+ border: '1px solid #000'
+
+ button:
+ backgroundColor: '#ddd'
+
+ css.should.equal """
+ form {
+ margin: 5px;
+ }
+ form input {
+ border: 1px solid #000;
+ }
+ form button {
+ background-color: #ddd;
+ }
+ """
+
+exports.testOpacityAll = ->
+ css = coffeeCss.compile
+ div:
+ opacityAll: 0.25
+ css.should.equal """
+ div {
+ filter: alpha(opacity=25);
+ opacity: 0.25;
}
"""
-
-exports.compileWorks = ->
- cssFile = __dirname + '/test.css'
- path.exists cssFile, (exists) ->
- if exists
- fs.unlink cssFile, (err) ->
- compileCssTest()
- else
- compileCssTest()
+
+exports.testCompileFile = ->
+ coffeeCssFile = __dirname + '/testFile.css.coffee'
+ data = COFFEE_CSS_SAMPLE_FILE_DATA
+
+ fs.writeFile coffeeCssFile, data, (err) ->
+ coffeeCss.compile coffeeCssFile, (err, fileName, newFileName) ->
+ should.not.exist err
+ fileName.should.equal coffeeCssFile
+ newFileName.should.equal __dirname + '/testFile.css'
+
+ fs.readFile newFileName, (err, data) ->
+ data.toString().should.equal """
+ a {
+ text-decoration: none;
+ }
+ a:hover {
+ text-decoration: underline;
+ }
+ """
+ fs.unlink fileName
+ fs.unlink newFileName
View
30 test/test.css
@@ -1,30 +0,0 @@
-a {
- text-decoration: none;
-}
-a:hover {
- text-decoration: underline;
-}
-form {
- border-radius: 5px;
- -webkit-box-shadow: 5px;
- -moz-box-shadow: 5px;
- box-shadow: 5px;
- opacity: 1;
- padding: 10px;
-}
-form input {
- border: 1px solid #fff;
- padding: 5px;
-}
-form button {
- padding: 5px 0 5px;
-}
-#id .className {
- opacity: 1;
-}
-img {
- opacity: 1;
-}
-img:hover {
- opacity: 0.8;
-}
View
23 test/test.css.coffee
@@ -1,23 +0,0 @@
-exports.css =
- 'a':
- textDecoration: 'none'
- 'a:hover':
- textDecoration: 'underline'
- 'form':
- borderRadius: '5px',
- boxShadowAll: '5px'
- opacity: 1
- padding: '10px'
-
- input:
- border: '1px solid #fff'
- padding: '5px'
-
- button:
- padding: '5px 0 5px'
- '#id .className':
- opacity: 1
- 'img':
- opacity: 1
- 'img:hover':
- opacity: 0.8

0 comments on commit 590f2cd

Please sign in to comment.
Something went wrong with that request. Please try again.