Skip to content

Commit

Permalink
Correctly escape slashes in regular expressions (or really anywhere)
Browse files Browse the repository at this point in the history
- Add a test to cover this scenario
  • Loading branch information
underscorebrody committed Jan 18, 2016
1 parent 8e50ad4 commit 41e10ee
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ module.exports = function(grunt) {
options: {
quotes: 'single'
}
},

regexp: {
src: 'test/fixtures/regexp.html',
dest: 'tmp/regexp.js'
}
}
});
Expand Down
1 change: 1 addition & 0 deletions tasks/lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ var Compiler = function(grunt, options, cwd, expanded) {
return source.split(/^/gm).map(function(line) {
var quote = options.quotes === 'single' ? '\'' : '"';

line = line.replace(/\\/g, '\\\\');
line = line.replace(/\n/g, '\\n');
var quoteRegExp = new RegExp(quote, 'g');
line = line.replace(quoteRegExp, '\\' + quote);
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 @@ -217,6 +217,16 @@ exports.ngtemplates = {
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();
},

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

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

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

$templateCache.put('test/fixtures/regexp.html',
"<h1>Regexp</h1>\n" +
"\n" +
"<script type=\"text/javascript\">\n" +
" var reg = new RegExp(/^(((\\+[1-9][0-9])|(00[1-9][0-9]))[0-9]{7,11})|((((01|02|03|04|05|07|08)[0-9])|(06[1-9]))[0-9]{7})$/)\n" +
" var reg2 = new RegExp(/^\\+-\\\\--\\|)$/)\n" +
"</script>\n"
);

}]);
6 changes: 6 additions & 0 deletions test/fixtures/regexp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Regexp</h1>

<script type="text/javascript">
var reg = new RegExp(/^(((\+[1-9][0-9])|(00[1-9][0-9]))[0-9]{7,11})|((((01|02|03|04|05|07|08)[0-9])|(06[1-9]))[0-9]{7})$/)
var reg2 = new RegExp(/^\+-\\--\|)$/)
</script>

0 comments on commit 41e10ee

Please sign in to comment.