better prechars handling including ng-template script

This commit is contained in:
Gilad Peleg
2014-04-14 17:23:56 +03:00
parent 2ede385fb3
commit 28b756a753
4 changed files with 47 additions and 3 deletions

View File

@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>fixture</title>
<script type="text/ng-template" id="/tpl.html"> </script>
</head>
<body>
</body>
</html>

View File

@@ -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. '<ng-'
//3. '</ng-'
var allowedPreChars = '(\\s|<|<\/){1}';
//build find/replace regex
//$1 -> 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';

View File

@@ -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": [

24
test.js
View File

@@ -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();
});