grunt-bake
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -171,2 +171,12 @@ /* | ||
semantic_if: { | ||
options: { | ||
semanticIf: true | ||
}, | ||
files: { | ||
"tmp/semantic_if.html": "test/fixtures/semantic_if.html" | ||
} | ||
}, | ||
format_bake: { | ||
@@ -173,0 +183,0 @@ files: { |
{ | ||
"name": "grunt-bake", | ||
"description": "Bake external includes into files to create static pages with no server-side compilation time", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"homepage": "https://github.com/MathiasPaumgarten/grunt-bake", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -602,3 +602,4 @@ # grunt-bake | ||
`1.4.0` __1-30-2016__ adds full JS support for evaluating _if. | ||
`1.3.1` __1-20-2016__ adds support for parsing values in inline variables. | ||
`1.3.0` __1-13-2016__ adds support for parsing file paths in bake tag. |
@@ -70,6 +70,3 @@ /* | ||
// check if key exists and leave pattern untouched if specified in options | ||
// due to support for variables as array keys (see #41) we need to check if we could resolve | ||
// something, because mout.object.has() can not resolve keys. | ||
if( resolved === "" && !mout.object.has( content, key ) && !options.removeUndefined ) { | ||
if( resolved === undefined && !options.removeUndefined ) { | ||
return match; | ||
@@ -117,3 +114,2 @@ } | ||
// Regex to detect array syntax. | ||
@@ -127,2 +123,6 @@ | ||
// | ||
var ifRegex = /([a-z_$][0-9a-z_$@]*)|(?:"([^"]*)")|(?:'([^']*)')/gi; | ||
// Method to check wether file exists and warn if not. | ||
@@ -191,59 +191,13 @@ | ||
// Helper method to check if a value represents false | ||
function isFalse( value ) { | ||
var string = String( value ).toLowerCase(); | ||
if ( value === "" || value === undefined || value === false || string === "false" ) { | ||
return true; | ||
} | ||
if ( options.semanticIf === true ) { | ||
return mout.array.indexOf( [ "no", "off" ], string ) !== -1; | ||
} | ||
if ( mout.lang.isArray( options.semanticIf ) ) { | ||
return mout.array.indexOf( options.semanticIf, string ) !== -1; | ||
} | ||
if ( mout.lang.isFunction( options.semanticIf ) ) { | ||
return options.semanticIf( value ); | ||
} | ||
return false; | ||
} | ||
// Helper method to resolve nested placeholder names like: "home.footer.text" | ||
function resolveName( name, values ) { | ||
name = name.replace( /\[([^\]]+)\]/g, function( match, inner ) { | ||
name = String( name ).replace( /\[([^\]]+)\]/g, function( match, inner ) { | ||
return "." + resolveName( inner, values ); | ||
}); | ||
var value = mout.object.get( values, name ); | ||
return value !== undefined ? value : ""; | ||
return mout.object.get( values, name ); | ||
} | ||
// Helper that simply checks weather a value exists and is not `false` | ||
function hasValue( name, values ) { | ||
name = name.replace( / /g, "" ); | ||
var invert = false; | ||
if ( name[ 0 ] === "!" ) { | ||
name = name.substr( 1 ); | ||
invert = true; | ||
} | ||
var current = mout.object.get( values, name ); | ||
var returnValue = !isFalse( current ); | ||
return invert ? ! returnValue : returnValue; | ||
} | ||
// Helper method to apply indent | ||
@@ -259,3 +213,4 @@ | ||
.map( function( line ) { | ||
return indent + line; | ||
// do not indent empty lines | ||
return line.trim() !== "" ? ( indent + line ) : ""; | ||
} ) | ||
@@ -277,3 +232,3 @@ .join( "\n" ); | ||
var array = resolveName( string, values ); | ||
if ( array === "" ) array = []; | ||
if ( ! mout.lang.isArray( array ) ) array = []; | ||
@@ -285,3 +240,2 @@ return array; | ||
// Handle _if attributes in inline arguments | ||
@@ -296,35 +250,31 @@ | ||
var operator = getOperator( value ); | ||
var params = {}; | ||
if ( operator ) { | ||
var parts = value.split( operator ); | ||
var condition = value.replace( ifRegex, function( match, varname ) { | ||
if( ! varname ) return match; | ||
var left = mout.string.rtrim( parts[ 0 ] ); | ||
var right = mout.string.ltrim( parts[ 1 ] ).replace( /'/g, "" ); | ||
var resolved = resolveName( varname, values ); | ||
if( mout.object.has( values, left ) ) left = resolveName( left, values ); | ||
if( mout.object.has( values, right ) ) right = resolveName( right, values ); | ||
// check for semantic falsy values | ||
if ( options.semanticIf === true ) { | ||
resolved = [ "no", "off" ].indexOf( resolved ) === -1; | ||
if ( operator === "==" && left === right ) return false; | ||
else if ( operator === "!=" && left !== right ) return false; | ||
} else if ( mout.lang.isArray( options.semanticIf ) ) { | ||
resolved = options.semanticIf.indexOf( resolved ) === -1; | ||
return true; | ||
} | ||
} else if ( mout.lang.isFunction( options.semanticIf ) ) { | ||
resolved = options.semanticIf( resolved ); | ||
} | ||
if ( ! hasValue( value, values ) ) { | ||
params[varname] = resolved; | ||
return true; | ||
return "params." + varname; | ||
}); | ||
} else if ( value[ 0 ] === "!" ) { | ||
try { | ||
return ! eval( condition ); | ||
var name = value.substr( 1 ); | ||
return ! isFalse( resolveName( name, values ) ); | ||
} else if ( isFalse( resolveName( value, values ) ) ) { | ||
return true; | ||
} catch( e ) { | ||
grunt.log.error( "Invalid if condition: '" + value + "'" ); | ||
} | ||
} | ||
@@ -335,12 +285,3 @@ | ||
function getOperator( string ) { | ||
var operators = [ "==", "!=" ]; | ||
for ( var i = 0; i < operators.length; i++ ) { | ||
if ( string.indexOf( operators[ i ] ) > -1 ) return operators[ i ]; | ||
} | ||
return false; | ||
} | ||
// Handle _render attributes in inline arguments | ||
@@ -482,3 +423,3 @@ | ||
var start = 0; // character position in `content` where inner-content starts | ||
var position = 0; // current character position within _original_ content | ||
var position = 0; // current character position within _original_ content | ||
var length = 0; // length section (= spacing plus bake-tag) we currently evaluate | ||
@@ -485,0 +426,0 @@ var remain = content; // content left for further extraction |
@@ -20,2 +20,3 @@ "use strict"; | ||
"tmp/if_bake.html": "test/expected/if_bake.html", | ||
"tmp/semantic_if.html": "test/expected/semantic_if.html", | ||
"tmp/format_bake.html": "test/expected/format_bake.html", | ||
@@ -22,0 +23,0 @@ "tmp/foreach_bake.html": "test/expected/foreach_bake.html" , |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51785
79
605
831
1