Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add option to include only overrides, closes #4

  • Loading branch information...
commit 5d3cc61319a0284d9682020be71c4965bb29a495 1 parent ea2c1ff
Robin Pokorný authored
View
136 Gruntfile.js
@@ -1,64 +1,72 @@
-/*
- * grunt-legacssy
- * https://github.com/robinpokorny/grunt-legacssy
- *
- * Copyright (c) 2013 Robin Pokorný
- * Licensed under the MIT license.
- */
-
-'use strict';
-
-module.exports = function(grunt) {
-
- // Project configuration.
- grunt.initConfig({
- jshint: {
- all: [
- 'Gruntfile.js',
- 'tasks/*.js',
- '<%= nodeunit.tests %>',
- ],
- options: {
- jshintrc: '.jshintrc',
- },
- },
-
- // Before generating any new files, remove any previously-created files.
- clean: {
- tests: ['tmp'],
- },
-
- // Configuration to be run (and then tested).
- legacssy: {
- default_options: {
- options: {
- },
- files: {
- 'tmp/default_options': 'test/fixtures/test.css'
- },
- },
- },
-
- // Unit tests.
- nodeunit: {
- tests: ['test/*_test.js'],
- },
-
- });
-
- // Actually load this plugin's task(s).
- grunt.loadTasks('tasks');
-
- // These plugins provide necessary tasks.
- grunt.loadNpmTasks('grunt-contrib-jshint');
- grunt.loadNpmTasks('grunt-contrib-clean');
- grunt.loadNpmTasks('grunt-contrib-nodeunit');
-
- // Whenever the "test" task is run, first clean the "tmp" dir, then run this
- // plugin's task(s), then test the result.
- grunt.registerTask('test', ['clean', 'legacssy', 'nodeunit']);
-
- // By default, lint and run all tests.
- grunt.registerTask('default', ['jshint', 'test']);
-
-};
+/*
+ * grunt-legacssy
+ * https://github.com/robinpokorny/grunt-legacssy
+ *
+ * Copyright (c) 2013 Robin Pokorný
+ * Licensed under the MIT license.
+ */
+
+'use strict';
+
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ jshint: {
+ all: [
+ 'Gruntfile.js',
+ 'tasks/*.js',
+ '<%= nodeunit.tests %>',
+ ],
+ options: {
+ jshintrc: '.jshintrc',
+ },
+ },
+
+ // Before generating any new files, remove any previously-created files.
+ clean: {
+ tests: ['tmp'],
+ },
+
+ // Configuration to be run (and then tested).
+ legacssy: {
+ default_options: {
+ options: {
+ },
+ files: {
+ 'tmp/default_options': 'test/fixtures/test.css'
+ },
+ },
+ overrides_only: {
+ options: {
+ overridesOnly: true
+ },
+ files: {
+ 'tmp/overrides_only': 'test/fixtures/test.css'
+ },
+ },
+ },
+
+ // Unit tests.
+ nodeunit: {
+ tests: ['test/*_test.js'],
+ },
+
+ });
+
+ // Actually load this plugin's task(s).
+ grunt.loadTasks('tasks');
+
+ // These plugins provide necessary tasks.
+ grunt.loadNpmTasks('grunt-contrib-jshint');
+ grunt.loadNpmTasks('grunt-contrib-clean');
+ grunt.loadNpmTasks('grunt-contrib-nodeunit');
+
+ // Whenever the "test" task is run, first clean the "tmp" dir, then run this
+ // plugin's task(s), then test the result.
+ grunt.registerTask('test', ['clean', 'legacssy', 'nodeunit']);
+
+ // By default, lint and run all tests.
+ grunt.registerTask('default', ['jshint', 'test']);
+
+};
View
162 README.md
@@ -1,80 +1,84 @@
-# grunt-legacssy
-
-> Fix your CSS for legacy browsers
-
-## Getting Started
-This plugin requires Grunt `~0.4.0`
-
-If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
-
-```shell
-npm install grunt-legacssy --save-dev
-```
-
-Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
-
-```js
-grunt.loadNpmTasks('grunt-legacssy');
-```
-
-## The "legacssy" task
-
-### Overview
-_Run this task with the `grunt legacssy` command._
-
-The task concats provided styles and when it finds unsupported (i.e. CSS3) @media query it tests the min-width and max-width and if it matches the rules inside the @media are included in the resulting style.
-
-Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide.
-
-### Options
-
-#### legacyWidth
-type: `Number`
-Default: 1024
-
-#### matchingOnly
-Type: `Boolean`
-Default: True
-
-Unsupported media queries are flattened only if their min-width and max-width match with *legacyWidth*. Comparision is number only, that means it is compatible with both px's and em's as long as there is only one of them used.
-
-### Usage Examples
-
-#### Default Options
-
-```js
-grunt.initConfig({
- legacssy: {
- files: {
- 'css/style-legacy.css': 'css/style.css',
- },
- },
-})
-```
-
-#### Default Options
-
-```js
-grunt.initConfig({
- legacssy: {
- options: {
- // Include only styles for a screen 800px wide
- legacyWidth: 800
- },
- files: {
- 'css/style-legacy.css': 'css/style.css',
- },
- },
-})
-```
-
-## Contributing
-In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
-
-## Release History
-* 2013-08-19    v0.2.0   Media matching added
-* 2013-08-13   v0.1.0   Initial task with basic functionality.
-
-
-[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/robinpokorny/grunt-legacssy/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
+# grunt-legacssy [![Dependency Status](https://gemnasium.com/robinpokorny/grunt-legacssy.png)](https://gemnasium.com/robinpokorny/grunt-legacssy)
+> Fix your CSS for legacy browsers
+
+## Getting Started
+This plugin requires Grunt `~0.4.0`
+
+If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
+
+```shell
+npm install grunt-legacssy --save-dev
+```
+
+Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
+
+```js
+grunt.loadNpmTasks('grunt-legacssy');
+```
+
+## The "legacssy" task
+
+### Overview
+_Run this task with the `grunt legacssy` command._
+
+The task concats provided styles and when it finds unsupported (i.e. CSS3) @media query it tests the min-width and max-width and if it matches the rules inside the @media are included in the resulting style.
+
+Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide.
+
+### Options
+
+#### legacyWidth
+type: `Number`
+Default: 1024
+
+#### matchingOnly
+Type: `Boolean`
+Default: True
+
+Unsupported media queries are flattened only if their min-width and max-width match with *legacyWidth*. Comparision is number only, that means it is compatible with both px's and em's as long as there is only one of them used.
+
+#### overridesOnly
+Type: `Boolean`
+Default: False
+
+### Usage Examples
+
+#### Default Options
+
+```js
+grunt.initConfig({
+ legacssy: {
+ files: {
+ 'css/style-legacy.css': 'css/style.css',
+ },
+ },
+})
+```
+
+#### Default Options
+
+```js
+grunt.initConfig({
+ legacssy: {
+ options: {
+ // Include only styles for a screen 800px wide
+ legacyWidth: 800
+ },
+ files: {
+ 'css/style-legacy.css': 'css/style.css',
+ },
+ },
+})
+```
+
+## Contributing
+In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
+
+## Release History
+* 2013-12-24 v0.3.0 Output only overrides
+* 2013-08-19  v0.2.0   Media matching added
+* 2013-08-13   v0.1.0   Initial task with basic functionality.
+
+
+[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/robinpokorny/grunt-legacssy/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
View
8 package.json
@@ -1,7 +1,7 @@
{
"name": "grunt-legacssy",
"description": "Fix your CSS for legacy browsers",
- "version": "0.2.0",
+ "version": "0.3.0",
"homepage": "https://github.com/robinpokorny/grunt-legacssy",
"author": {
"name": "Robin Pokorný",
@@ -29,11 +29,11 @@
"test": "grunt test"
},
"dependencies" : {
- "css": "~1.4.1"
+ "css": "~1.6.0"
},
"devDependencies": {
- "grunt-contrib-jshint": "~0.6.0",
- "grunt-contrib-clean": "~0.4.0",
+ "grunt-contrib-jshint": "~0.7.0",
+ "grunt-contrib-clean": "~0.5.0",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt": "~0.4.1"
},
View
15 tasks/legacssy.js
@@ -17,7 +17,8 @@ module.exports = function(grunt) {
// Merge task-specific and/or target-specific options with these defaults.
options = this.options({
legacyWidth: 1024,
- matchingOnly: true
+ matchingOnly: true,
+ overridesOnly: false
});
if (this.files.length < 1) {
@@ -42,7 +43,7 @@ module.exports = function(grunt) {
var style = css.parse(src);
// Do the magic!
- style.stylesheet.rules = stripMediaQueries(style.stylesheet.rules);
+ style.stylesheet.rules = stripMediaQueries(style.stylesheet.rules, options.overridesOnly);
// Write the destination file.
grunt.file.write(f.dest, css.stringify(style));
@@ -69,7 +70,7 @@ module.exports = function(grunt) {
for (var i = 0; i < queries.length; i++) {
// RegExps are based on ones from scottjehl/Respond
var minw = queries[i].match( /\(\s*min\-width\s*:\s*(\s*[0-9\.]+)[^\d\)]*\s*\)/ ) && parseFloat( RegExp.$1 ),
- maxw = queries[i].match( /\(\s*max\-width\s*:\s*(\s*[0-9\.]+)[^\d\)]*\s*\)/ ) && parseFloat( RegExp.$1 );
+ maxw = queries[i].match( /\(\s*max\-width\s*:\s*(\s*[0-9\.]+)[^\d\)]*\s*\)/ ) && parseFloat( RegExp.$1 );
// If this does not match, move to the next
if ((minw && minw > options.legacyWidth) ||
@@ -84,15 +85,17 @@ module.exports = function(grunt) {
return false;
};
- var stripMediaQueries = function (rules) {
+ var stripMediaQueries = function (rules, innerOnly) {
var tmp = [];
for (var i = 0; i < rules.length; i++) {
if (rules[i].type === "media" && isUnsupported(rules[i].media)) {
if (!options.matchingOnly || isMatching(rules[i].media)) {
- tmp = tmp.concat(stripMediaQueries(rules[i].rules));
+ tmp = tmp.concat(stripMediaQueries(rules[i].rules, false));
}
} else {
- tmp.push(rules[i]);
+ if (!innerOnly) {
+ tmp.push(rules[i]);
+ }
}
}
return tmp;
View
3  test/expected/overrides_only
@@ -0,0 +1,3 @@
+h1 {
+ font-weight: bold;
+}
View
89 test/legacssy_test.js
@@ -1,40 +1,49 @@
-'use strict';
-
-var grunt = require('grunt');
-
-/*
- ======== A Handy Little Nodeunit Reference ========
- https://github.com/caolan/nodeunit
-
- Test methods:
- test.expect(numAssertions)
- test.done()
- Test assertions:
- test.ok(value, [message])
- test.equal(actual, expected, [message])
- test.notEqual(actual, expected, [message])
- test.deepEqual(actual, expected, [message])
- test.notDeepEqual(actual, expected, [message])
- test.strictEqual(actual, expected, [message])
- test.notStrictEqual(actual, expected, [message])
- test.throws(block, [error], [message])
- test.doesNotThrow(block, [error], [message])
- test.ifError(value)
-*/
-
-exports.legacssy = {
- setUp: function(done) {
- // setup here if necessary
- done();
- },
- default_options: function(test) {
- test.expect(1);
-
- var actual = grunt.file.read('tmp/default_options');
- var expected = grunt.file.read('test/expected/default_options');
- test.equal(actual, expected, 'should describe what the default behavior is.');
-
- test.done();
- },
-
-};
+'use strict';
+
+var grunt = require('grunt');
+
+/*
+ ======== A Handy Little Nodeunit Reference ========
+ https://github.com/caolan/nodeunit
+
+ Test methods:
+ test.expect(numAssertions)
+ test.done()
+ Test assertions:
+ test.ok(value, [message])
+ test.equal(actual, expected, [message])
+ test.notEqual(actual, expected, [message])
+ test.deepEqual(actual, expected, [message])
+ test.notDeepEqual(actual, expected, [message])
+ test.strictEqual(actual, expected, [message])
+ test.notStrictEqual(actual, expected, [message])
+ test.throws(block, [error], [message])
+ test.doesNotThrow(block, [error], [message])
+ test.ifError(value)
+*/
+
+exports.legacssy = {
+ setUp: function(done) {
+ // setup here if necessary
+ done();
+ },
+ default_options: function(test) {
+ test.expect(1);
+
+ var actual = grunt.file.read('tmp/default_options');
+ var expected = grunt.file.read('test/expected/default_options');
+ test.equal(actual, expected, 'should describe what the default behavior is.');
+
+ test.done();
+ },
+ overrides_only: function(test) {
+ test.expect(1);
+
+ var actual = grunt.file.read('tmp/overrides_only');
+ var expected = grunt.file.read('test/expected/overrides_only');
+ test.equal(actual, expected, 'should output only overrides.');
+
+ test.done();
+ },
+
+};
Please sign in to comment.
Something went wrong with that request. Please try again.