Skip to content

Commit

Permalink
Format code, add configs for ESLint and JSCS
Browse files Browse the repository at this point in the history
  • Loading branch information
thorn0 committed Oct 14, 2015
1 parent 4a95388 commit abb8e7d
Show file tree
Hide file tree
Showing 10 changed files with 502 additions and 337 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[package.json]
indent_style = space
indent_size = 2
end_of_line = lf
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/cases/
28 changes: 28 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"rules": {
"indent": 0,
"quotes": [
2,
"double",
"avoid-escape"
],
"linebreak-style": [
2,
"unix"
],
"semi": [
2,
"always"
],
"space-in-parens": [
2,
"always"
],
"no-console": 0,
"eol-last": 2
},
"env": {
"node": true
},
"extends": "eslint:recommended"
}
51 changes: 51 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"excludeFiles": ["test/cases/**", "node_modules/**"],
"disallowKeywords": [ "with" ],
"disallowMixedSpacesAndTabs": "smart",
"disallowMultipleLineBreaks": true,
"disallowMultipleLineStrings": true,
"disallowMultipleSpaces": true,
"disallowSpaceAfterKeywords": [ "if", "for", "while", "switch", "catch" ],
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpaceBeforeBinaryOperators": [ ",", ":" ],
"disallowSpaceBeforeComma": true,
"disallowSpaceBeforePostfixUnaryOperators": true,
"disallowSpacesInAnonymousFunctionExpression": { "beforeOpeningRoundBrace": true },
"disallowSpacesInCallExpression": true,
"disallowSpacesInFunctionExpression": { "beforeOpeningRoundBrace": true },
"disallowSpacesInNamedFunctionExpression": { "beforeOpeningRoundBrace": true },
"disallowTrailingComma": true,
"disallowTrailingWhitespace": true,
"requireBlocksOnNewline": true,
"requireCamelCaseOrUpperCaseIdentifiers": true,
"requireCapitalizedComments": { "ignoreIfInTheMiddle": true },
"requireCommaBeforeLineBreak": true,
"requireCurlyBraces": true,
"requireDotNotation": true,
"requireKeywordsOnNewLine": [ "else" ],
"requireLineFeedAtFileEnd": true,
"requireNewlineBeforeBlockStatements": true,
"requireOperatorBeforeLineBreak": true,
"requirePaddingNewLinesBeforeLineComments": true,
"requireParenthesesAroundIIFE": true,
"requireSemicolons": true,
"requireSpaceAfterBinaryOperators": true,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceBeforeBlockStatements": true,
"requireSpaceBetweenArguments": true,
"requireSpacesInAnonymousFunctionExpression": { "beforeOpeningCurlyBrace": true },
"requireSpacesInConditionalExpression": true,
"requireSpacesInForStatement": true,
"requireSpacesInFunctionExpression": { "beforeOpeningCurlyBrace": true },
"requireSpacesInNamedFunctionExpression": { "beforeOpeningCurlyBrace": true },
"requireSpacesInsideBrackets": true,
"requireSpacesInsideObjectBrackets": "all",
"requireSpacesInsideParentheses": "all",
"validateIndentation": 4,
"validateLineBreaks": "LF",
"validateQuoteMarks": {
"escape": true,
"mark": "\""
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
},
"scripts": {
"test": "mocha test",
"coverage": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec"
"coverage": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd -R spec",
"lint": "eslint . --fix || true",
"format": "jscs . -x"
}
}
31 changes: 17 additions & 14 deletions src/css.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"use strict";


var datauri = require( "datauri" );
var CleanCSS = require( "clean-css" );
var xtend = require( "xtend" );
var async = require( "async" );
var path = require( "path" );
var _ = require( "lodash" );
var inline = require( "./util" );

module.exports = function( options, callback )
Expand All @@ -18,7 +17,7 @@ module.exports = function( options, callback )

if( inline.isBase64Path( args.src ) )
{
return callback( null ); // skip
return callback( null ); // Skip
}

inline.getFileReplacement( args.src, settings.relativeTo, function( err, datauriContent )
Expand All @@ -29,22 +28,22 @@ module.exports = function( options, callback )
}
if( typeof( args.limit ) === "number" && datauriContent.length > args.limit * 1000 )
{
return( callback( null ) ); // skip
return callback( null ); // Skip
}

var css = 'url("' + datauriContent + '");';
result = result.replace( new RegExp( "url\\(\\s?[\"']?(" + inline.escapeSpecialChars(args.src) + ")[\"']?\\s?\\);", "g" ),
function( ) { return css; } );
var css = "url(\"" + datauriContent + "\");";
var re = new RegExp( "url\\(\\s?[\"']?(" + inline.escapeSpecialChars( args.src ) + ")[\"']?\\s?\\);", "g" );
result = result.replace( re, _.constant( css ) );

return( callback( null ) );
return callback( null );
} );
};

var rebase = function( src )
{
var css = 'url("' + path.join( settings.rebaseRelativeTo, src ).replace( /\\/g, "/" ) + '");';
result = result.replace( new RegExp( "url\\(\\s?[\"']?(" + inline.escapeSpecialChars(src) + ")[\"']?\\s?\\);", "g" ),
function( ) { return css; } );
var css = "url(\"" + path.join( settings.rebaseRelativeTo, src ).replace( /\\/g, "/" ) + "\");";
var re = new RegExp( "url\\(\\s?[\"']?(" + inline.escapeSpecialChars( src ) + ")[\"']?\\s?\\);", "g" );
result = result.replace( re, _.constant( css ) );
};

var result = settings.fileContent;
Expand All @@ -60,14 +59,18 @@ module.exports = function( options, callback )
var src = found[ 1 ];
if( !inline.isRemotePath( src ) && !inline.isBase64Path( src ) )
{
rebase( src );
rebase( src );
}
}
}

var inlineAttributeCommentRegex = new RegExp( "\\/\\*\\s?" + settings.inlineAttribute + "\\s?\\*\\/", "i" );
var inlineAttributeIgnoreCommentRegex = new RegExp( "\\/\\*\\s?" + settings.inlineAttribute + "-ignore\\s?\\*\\/", "i" );

while( ( found = urlRegex.exec( result ) ) !== null )
{
if( !found[ 0 ].match( new RegExp( "\\/\\*\\s?" + settings.inlineAttribute + "-ignore\\s?\\*\\/", "gi" ) )
&& ( settings.images || found[ 0 ].match( new RegExp( "\\/\\*\\s?" + settings.inlineAttribute + "\\s?\\*\\/", "gi" ) ) ) )
if( !inlineAttributeIgnoreCommentRegex.test( found[ 0 ] ) &&
( settings.images || inlineAttributeCommentRegex.test( found[ 0 ] ) ) )
{
tasks.push( replaceUrl.bind(
{
Expand Down
63 changes: 30 additions & 33 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ var css = require( "./css" );

module.exports = function( options, callback )
{
var settings = xtend({}, inline.defaults, options );
var settings = xtend( {}, inline.defaults, options );

function replaceInlineAttribute(string) {
function replaceInlineAttribute( string )
{
return string
.replace( new RegExp( " " + settings.inlineAttribute + "-ignore", "gi" ), "" )
.replace( new RegExp( " " + settings.inlineAttribute, "gi" ), "" );
.replace( new RegExp( " " + settings.inlineAttribute + "-ignore", "gi" ), "" )
.replace( new RegExp( " " + settings.inlineAttribute, "gi" ), "" );
}

var replaceScript = function( callback )
{
var args = this;

args.element = replaceInlineAttribute(args.element);
args.element = replaceInlineAttribute( args.element );

inline.getTextReplacement( args.src, settings.relativeTo, function( err, content )
{
Expand All @@ -35,11 +36,9 @@ module.exports = function( options, callback )
{
return callback( null );
}
var html = '<script' + ( args.attrs ? ' ' + args.attrs : '' ) + '>\n' + js + '\n</script>';

result = result.replace( new RegExp( inline.escapeSpecialChars(args.element) , "g" ),
function( ) { return html; } );

var html = "<script" + ( args.attrs ? " " + args.attrs : "" ) + ">\n" + js + "\n</script>";
var re = new RegExp( inline.escapeSpecialChars( args.element ), "g" );
result = result.replace( re, _.constant( html ) );
return callback( null );
} );
};
Expand All @@ -48,7 +47,7 @@ module.exports = function( options, callback )
{
var args = this;

args.element = replaceInlineAttribute(args.element);
args.element = replaceInlineAttribute( args.element );

inline.getTextReplacement( args.src, settings.relativeTo, function( err, content )
{
Expand All @@ -66,17 +65,15 @@ module.exports = function( options, callback )
rebaseRelativeTo: path.relative( settings.relativeTo, path.join( settings.relativeTo, args.src, ".." + path.sep ) )
} );

css( cssOptions, function ( err, content )
css( cssOptions, function( err, content )
{
if( err )
{
return callback( err );
}
var html = '<style' + ( args.attrs ? ' ' + args.attrs : '' ) + '>\n' + content + '\n</style>';

result = result.replace( new RegExp( inline.escapeSpecialChars(args.element) , "g" ),
function( ) { return html; } );

var html = "<style" + ( args.attrs ? " " + args.attrs : "" ) + ">\n" + content + "\n</style>";
var re = new RegExp( inline.escapeSpecialChars( args.element ), "g" );
result = result.replace( re, _.constant( html ) );
return callback( null );
} );
} );
Expand All @@ -86,7 +83,7 @@ module.exports = function( options, callback )
{
var args = this;

args.element = replaceInlineAttribute(args.element);
args.element = replaceInlineAttribute( args.element );

inline.getFileReplacement( args.src, settings.relativeTo, function( err, datauriContent )
{
Expand All @@ -98,9 +95,9 @@ module.exports = function( options, callback )
{
return callback( null );
}
var html = '<img' + ( args.attrs ? ' ' + args.attrs : '' ) + ' src="' + datauriContent + '" />';
result = result.replace( new RegExp( inline.escapeSpecialChars(args.element) , "g" ),
function( ) { return html; } );
var html = "<img" + ( args.attrs ? " " + args.attrs : "" ) + " src=\"" + datauriContent + "\" />";
var re = new RegExp( inline.escapeSpecialChars( args.element ), "g" );
result = result.replace( re, _.constant( html ) );
return callback( null );
} );
};
Expand All @@ -109,19 +106,19 @@ module.exports = function( options, callback )
var tasks = [];
var found;

var inlineAttributeRegex = new RegExp( settings.inlineAttribute, "gi" );
var inlineAttributeIgnoreRegex = new RegExp( settings.inlineAttribute + "-ignore", "gi" );
var inlineAttributeRegex = new RegExp( settings.inlineAttribute, "i" );
var inlineAttributeIgnoreRegex = new RegExp( settings.inlineAttribute + "-ignore", "i" );

var scriptRegex = /<script\b[\s\S]+?\bsrc\s*=\s*("|')([\s\S]+?)\1[\s\S]*?>\s*<\/script>/gi;
while( ( found = scriptRegex.exec( result ) ) !== null )
{
if( !inlineAttributeIgnoreRegex.test( found[ 0 ] )
&& ( settings.scripts || inlineAttributeRegex.test( found[ 0 ] ) ) )
if( !inlineAttributeIgnoreRegex.test( found[ 0 ] ) &&
( settings.scripts || inlineAttributeRegex.test( found[ 0 ] ) ) )
{
tasks.push( replaceScript.bind(
{
element: found[ 0 ],
src: _.unescape(found[ 2 ]).trim(),
src: _.unescape( found[ 2 ] ).trim(),
attrs: inline.getAttrs( found[ 0 ], settings ),
limit: settings.scripts
} ) );
Expand All @@ -131,13 +128,13 @@ module.exports = function( options, callback )
var linkRegex = /<link\b[\s\S]+?\bhref\s*=\s*("|')([\s\S]+?)\1[\s\S]*?>/gi;
while( ( found = linkRegex.exec( result ) ) !== null )
{
if( !inlineAttributeIgnoreRegex.test( found[ 0 ] )
&& ( settings.links || inlineAttributeRegex.test( found[ 0 ] ) ) )
if( !inlineAttributeIgnoreRegex.test( found[ 0 ] ) &&
( settings.links || inlineAttributeRegex.test( found[ 0 ] ) ) )
{
tasks.push( replaceLink.bind(
{
element: found[ 0 ],
src: _.unescape(found[ 2 ]).trim(),
src: _.unescape( found[ 2 ] ).trim(),
attrs: inline.getAttrs( found[ 0 ], settings ),
limit: settings.links
} ) );
Expand All @@ -147,20 +144,20 @@ module.exports = function( options, callback )
var imgRegex = /<img\b[\s\S]+?\bsrc\s*=\s*("|')([\s\S]+?)\1[\s\S]*?>/gi;
while( ( found = imgRegex.exec( result ) ) !== null )
{
if( !inlineAttributeIgnoreRegex.test( found[ 0 ] )
&& ( settings.images || inlineAttributeRegex.test( found[ 0 ] ) ) )
if( !inlineAttributeIgnoreRegex.test( found[ 0 ] ) &&
( settings.images || inlineAttributeRegex.test( found[ 0 ] ) ) )
{
tasks.push( replaceImg.bind(
{
element: found[ 0 ],
src: _.unescape(found[ 2 ]).trim(),
src: _.unescape( found[ 2 ] ).trim(),
attrs: inline.getAttrs( found[ 0 ], settings ),
limit: settings.images
} ) );
}
}

result = replaceInlineAttribute(result);
result = replaceInlineAttribute( result );

async.parallel( tasks, function( err )
{
Expand Down
4 changes: 2 additions & 2 deletions src/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Based on https://github.com/chyingp/grunt-inline
*/

'use strict';
"use strict";

var inline = {};

module.exports = inline;

inline.html = require( "./html" );
inline.css = require( "./css" );
inline.css = require( "./css" );
Loading

0 comments on commit abb8e7d

Please sign in to comment.