From 28b756a7535202eec11c39ceee2dd1b7ef8ee97f Mon Sep 17 00:00:00 2001 From: Gilad Peleg Date: Mon, 14 Apr 2014 17:23:56 +0300 Subject: [PATCH] better prechars handling including ng-template script --- fixtures/angular-templates.html | 15 +++++++++++++++ index.js | 9 +++++++-- package.json | 2 +- test.js | 24 ++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 fixtures/angular-templates.html diff --git a/fixtures/angular-templates.html b/fixtures/angular-templates.html new file mode 100644 index 0000000..d7678ff --- /dev/null +++ b/fixtures/angular-templates.html @@ -0,0 +1,15 @@ + + + + + + fixture + + + + + + + + + diff --git a/index.js b/index.js index 862a61a..6e918a4 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ module.exports = function (params) { var customPrefixes = params.customPrefixes || []; //find ng-something by default - var prefix = '[^\/]ng-'; //Denis Bondarenko changed to support templates, inlined with script tag like ...type="text/ng-template" + var prefix = 'ng-'; //optionally add custom prefixes if (customPrefixes && customPrefixes.length) { var additions = customPrefixes.join('|'); @@ -20,11 +20,16 @@ module.exports = function (params) { //wrap around to insert into replace str later prefix = '(' + prefix + '){1}'; + //handle the following: + //1. ' ng-' + //2. ' allowable pre-chars //$2 -> prefix match //$3 -> actual directive (partially) - var replaceRegex = new RegExp('([\\s<\/]+)' + prefix + '(\\w+)', 'ig'); + var replaceRegex = new RegExp(allowedPreChars + prefix + '(\\w+)', 'ig'); //replace with data-ng-something var replaceStr = '$1data-$2$3'; diff --git a/package.json b/package.json index 5feba8a..0a6a659 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "bugs": "https://github.com/pgilad/gulp-angular-htmlify/issues", "main": "index.js", "scripts": { - "watchTest": "mocha --watch", + "watchTest": "mocha -R spec --watch", "test": "mocha" }, "keywords": [ diff --git a/test.js b/test.js index 5a4c50f..639a3da 100644 --- a/test.js +++ b/test.js @@ -143,3 +143,27 @@ it('should work with custom prefixes', function (cb) { stream.end(); }); + +it('should not modify ng-template script', function (cb) { + var stream = htmlify(); + var filename = './fixtures/angular-templates.html'; + + stream.on('data', function (file) { + //make sure ng-app turned into data-ng-app + var contents = file.contents.toString('utf8'); + //validate that ng-templates don't change + assert(/type="text\/ng-template"/.test(contents)); + }); + + stream.on('end', cb); + + var testFile = fs.readFileSync(filename); + + stream.write(new gutil.File({ + path: filename, + cwd: '.', + contents: new Buffer(testFile.toString()) + })); + + stream.end(); +});