Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Ivanov committed Feb 25, 2013
0 parents commit 3502dbc
Show file tree
Hide file tree
Showing 9 changed files with 544 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/node_modules/
15 changes: 15 additions & 0 deletions .jshintrc
@@ -0,0 +1,15 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"unused": true,
"boss": true,
"eqnull": true,
"node": true,
"es5": true
}
48 changes: 48 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,48 @@
'use strict';

module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
nodeunit: {
files: ['test/**/*_test.js'],
},
jshint: {
options: {
jshintrc: '.jshintrc'
},
gruntfile: {
src: 'Gruntfile.js'
},
lib: {
src: ['lib/**/*.js']
},
test: {
src: ['test/**/*.js']
},
},
watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
lib: {
files: '<%= jshint.lib.src %>',
tasks: ['jshint:lib', 'nodeunit']
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'nodeunit']
},
},
});

// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');

// Default task.
grunt.registerTask('default', ['jshint', 'nodeunit']);

};
22 changes: 22 additions & 0 deletions LICENSE-MIT
@@ -0,0 +1,22 @@
Copyright (c) 2013 Alexey Ivanov

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
@@ -0,0 +1,27 @@
# css-url-edit

Collection of helpers for css url manipulations.

## Getting Started
Install the module with: `npm install css-url-edit`

```javascript
var css_url_edit = require('css-url-edit');
css_url_edit.awesome(); // "awesome"
```

## Documentation
_(Coming soon)_

## Examples
_(Coming soon)_

## 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
_(Nothing yet)_

## License
Copyright (c) 2013 Alexey Ivanov
Licensed under the MIT license.
230 changes: 230 additions & 0 deletions lib/css-url-edit.js
@@ -0,0 +1,230 @@
/*
* css-url-edit
* https://github.com/iadramelk/css-url-edit
*
* Copyright (c) 2013 Alexey Ivanov
* Licensed under the MIT license.
*/

/**
* Module for css url strings manipulation.
*
* @module cssURLEdit
* @param {string} src Source of CSS file to work with.
*/
module.exports = function( src ) {

'use strict';

if ( typeof src !== 'string' ) {

throw { type: 'cssURLEdit', message: 'Please provide content of css file to parse.' };

}

var cssp = require( 'cssp' ),
path = require( 'path' ),
ast = cssp.parse( src ), // base asp object from css
urls = []; // collection of urls to work with

/**
* Generates initial document tree.
*
* @private
*/
var _collectURLs = function _collectURLs ( token ) {

var elem, isArrayElem;

if ( Array.isArray( token ) ) {

for ( var i = 0; i < token.length; i++ ) {

elem = token[ i ];
isArrayElem = Array.isArray( elem );

if ( isArrayElem && ( elem[ 0 ] === 'uri' ) ) {

urls.push( elem );

} else if ( isArrayElem ) {

_collectURLs( elem );

}

}

}

};

_collectURLs( ast );

/**
* Returns clean URL string from CSSP url object.
*
* @param {array} url Array object from urls array.
* @return {string} URL string without braces.
* @private
*/
var _getURLValue = function ( url ) {

var body = url[ 1 ];

switch(body[ 0 ]) {
case 'string':
return body[ 1 ].substring( 1, body[ 1 ].length - 1 );
case 'raw':
return body[ 1 ];
}

};


/**
* Replace current url in object with new one.
*
* @param {array} url Array object from urls array.
* @param {string} path New value for URL object.
* @private
*/
var _setURLValue = function ( url, path ) {

url[1] = ['string', '\'' + path + '\''];

};


/**
* Returns list of unique URLs in css document.
*
* @param {regexp} mask RegExp to test URLs against.
* @return {array} Array of matchet URLs.
*/
var getURLs = function ( mask ) {

if ( mask && ( mask instanceof RegExp ) !== true ) {

throw { type: 'getURLs', message: 'First argument must be RegExp' };

}

return urls.map( function ( value ) {

return _getURLValue( value );

} ).filter( function ( value, pos, self ) {

var unique = self.indexOf( value ) === pos;

if ( mask && unique ) {

return mask.test( value );

} else {

return unique;

}

} );

};


/**
* Changes relative path of every relative url() address to the new base.
*
* @param {string} from_path Old CSS folder.
* @param {string} to_path New CSS folder.
*/
var changeCSSRoot = function ( from_path, to_path ) {

if ( typeof from_path !== 'string' ) {

throw { type: 'changeCSSRoot', message: 'First argument must be String' };

}

if ( typeof to_path !== 'string' ) {

throw { type: 'changeCSSRoot', message: 'Second argument must be String' };

}

urls.filter( function ( value ) {

return !/^(http|data|\/)/.test( _getURLValue( value ) );

} ).forEach( function ( value ) {

var url_path = _getURLValue( value ),
absolute_path = path.join( from_path, url_path ),
new_path = path.relative( to_path, absolute_path );

_setURLValue( value, new_path );

} );

};


/**
* Replace content of every URL matching RegExp with new content.
*
* @param {regexp} from_value Mask to select urls with.
* @param {string} to_value Rule to apply on found items.
*/
var changeURLContent = function ( from_value, to_value ) {

if ( ! from_value instanceof RegExp ) {

throw { type: 'changeURLContent', message: 'First argument must be RegExp' };

}

if ( to_value === undefined ) {

to_value = "";

} else if( typeof to_value !== 'string' ) {

throw { type: 'changeURLContent', message: 'Second argument must be String' };

}

urls.filter( function ( value ) {

return from_value.test( _getURLValue( value ) );

} ).forEach( function ( value ) {

var new_value = _getURLValue( value ).replace( from_value, to_value );
_setURLValue( value, new_value );

} );

};


/**
* Return compiled css.
*
* @return {String} Compiled CSS
*/
var rebuildCSS = function () {

return cssp.translate( ast );

};


return {
getURLs: getURLs,
changeCSSRoot: changeCSSRoot,
changeURLContent: changeURLContent,
rebuildCSS: rebuildCSS
};

};
38 changes: 38 additions & 0 deletions package.json
@@ -0,0 +1,38 @@
{
"name": "css-url-edit",
"description": "Collection of helpers for css url() manipulations.",
"version": "0.1.0",
"homepage": "https://github.com/iadramelk/css-url-edit",
"author": {
"name": "Alexey Ivanov",
"email": "stupidlogin@gmail.com"
},
"repository": {
"type": "git",
"url": "git://github.com/iadramelk/css-url-edit.git"
},
"bugs": {
"url": "https://github.com/iadramelk/css-url-edit/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/iadramelk/css-url-edit/blob/master/LICENSE-MIT"
}
],
"main": "lib/css-url-edit",
"engines": {
"node": ">=0.8.0"
},
"scripts": {
"test": "grunt vows"
},
"devDependencies": {
"grunt": "~0.4.0",
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-watch": "~0.2.0",
"grunt-vows": "~0.0.0",
"vows": "~0.7.0"
},
"keywords": []
}

0 comments on commit 3502dbc

Please sign in to comment.