Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an option to use single quotes instead of double for quote wrapping #142

Merged
merged 1 commit into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ module.exports = function(grunt) {
undefined_file: {
src: 'test/fixtures/undefined.html',
dest: 'tmp/undefined_file.js'
},

single_quotes: {
src: 'test/fixtures/one.html',
dest: 'tmp/single_quotes.js',
options: {
quotes: 'single'
}
}
}
});
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ ensures that URLs load via both AJAX and `$templateCache`.
This should be the output path of the compiled JS indicated in your HTML,
such as `path/to/output.js` shown here.

### quotes

> Use single or double quotes to wrap the template strings

Defaults to 'double', other option is 'single'

## Usage


Expand Down
7 changes: 4 additions & 3 deletions tasks/angular-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ module.exports = function(grunt) {
standalone: false,
url: function(path) { return path; },
usemin: null,
append: false
append: false,
quotes: 'double'
});

grunt.verbose.writeflags(options, 'Options');
Expand All @@ -57,10 +58,10 @@ module.exports = function(grunt) {
grunt.log.writeln('File ' + file.dest.cyan + ' updated.');
}
else{
grunt.file.write(file.dest, compiled.join('\n'));
grunt.file.write(file.dest, compiled.join('\n'));
grunt.log.writeln('File ' + file.dest.cyan + ' created.');
}


if (options.usemin) {
if (appender.save('generated', appender.concatUseminFiles(options.usemin, file))) {
Expand Down
8 changes: 7 additions & 1 deletion tasks/lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,13 @@ var Compiler = function(grunt, options, cwd, expanded) {
*/
this.stringify = function(source) {
return source.split(/^/gm).map(function(line) {
return JSON.stringify(line);
var quote = options.quotes === 'single' ? '\'' : '"';

line = line.replace(/\n/g, '\\n');
var quoteRegExp = new RegExp(quote, 'g');
line = line.replace(quoteRegExp, '\\' + quote);

return quote + line + quote;
}).join(' +\n ') || '""';
};

Expand Down
10 changes: 10 additions & 0 deletions test/angular-templates_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,14 @@ exports.ngtemplates = {
test.done();
},

single_quote: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/single_quotes.js');
var expected = grunt.file.read('test/expected/single_quotes.js');

test.equal(expected, actual);
test.done();
}

};
16 changes: 16 additions & 0 deletions test/expected/single_quotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
angular.module('single_quotes').run(['$templateCache', function($templateCache) {
'use strict';

$templateCache.put('test/fixtures/one.html',
'<h1>One</h1>\n' +
'\n' +
'<p class="">I am one.</p>\n' +
'\n' +
'<script type="text/javascript">\n' +
' // Test\n' +
' /* comments */\n' +
' var foo = \'bar\';\n' +
'</script>\n'
);

}]);