Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* ability to embed ng-include
* update javadoc with comparison with gulp-angular-templatecache

2.3.1 (next)
==================
* ability to configure templateBegin for angular1.x
* updated README.md file

2.3.0 / 2016-08-08
==================
* Keep attribute quotes by default (add quotes if they missed in source code). Removing quotes caused serious of issues with bindings. You can get old behaviour by specifying config property minimize: {quotes: false}
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ Type: `Number`. Not specified by default (templates of any size allowed)

define the max size limit in bytes for the template be embedded. Ignore templates which size exceed this limit

#### options.jsTemplateBegin
Type: `String`. Default value: 'template:'

angular template property value. Example it could be either `'template:'` or `'template='`. Explained below:
- 'template:' both for Angular 1.x syntax `templateUrl: 'path'` and Angular 2.x syntax `@View({templateUrl: 'path'})`
- 'template=' Additional support for Angular 1.x syntax `templateUrl= 'path'`

## License
This module is released under the MIT license.

Expand Down
48 changes: 37 additions & 11 deletions lib/Angular1Processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function escapeSingleQuotes(string) {
}

var Angular1Processor = extend(RegexpProcessor, {
init : function(config) {
init: function (config) {
this._super.init(config);

if (!this.config.minimize) {
Expand All @@ -36,7 +36,7 @@ var Angular1Processor = extend(RegexpProcessor, {
this.minimizer = new Minimize(this.config.minimize);
if (!this.config.minimize.parser) {
this.minimizer.htmlparser = new html.Parser(
new html.DomHandler(this.minimizer.emits('read')), this.config.minimize.dom || {lowerCaseAttributeNames:false}
new html.DomHandler(this.minimizer.emits('read')), this.config.minimize.dom || { lowerCaseAttributeNames: false }
);
}

Expand All @@ -46,10 +46,36 @@ var Angular1Processor = extend(RegexpProcessor, {
},

/**
* @returns {String} pattern to search
* @returns {String} pattern to search
*/
getPattern: function () {
var defaultOperator = ':';
var operator;
var jsTemplateBegin = this.config.jsTemplateBegin

switch (jsTemplateBegin) {
case 'template:':
operator = defaultOperator;
break;
case 'template=':
operator = '=';
break;
default:
operator = defaultOperator;
break;
}
return '[\'"]?templateUrl[\'"]?[\\s]*' + operator + '[\\s]*[\'"`]([^\'"`]+)[\'"`]';
},

/**
* @returns {String} templateBegin to replace
*/
getPattern : function() {
return '[\'"]?templateUrl[\'"]?[\\s]*:[\\s]*[\'"`]([^\'"`]+)[\'"`]';
getTemplateBegin: function () {
if (this.config.jsTemplateBegin) {
return Buffer(this.config.jsTemplateBegin + '\'')
} else {
return TEMPLATE_BEGIN;
}
},

/**
Expand All @@ -63,10 +89,10 @@ var Angular1Processor = extend(RegexpProcessor, {
* @param {Function} cb to call after match replaced
* @param {Function} onErr error handler
*/
replaceMatch : function(fileContext, match, cb, onErr) {
replaceMatch: function (fileContext, match, cb, onErr) {
var relativeTemplatePath = match[1];
var templatePath = pathModule.join(fileContext.path, relativeTemplatePath);
var warnNext = function(msg) {
var warnNext = function (msg) {
this.logger.warn(msg);
cb();
}.bind(this);
Expand All @@ -84,7 +110,7 @@ var Angular1Processor = extend(RegexpProcessor, {

var embedTemplate = this.embedTemplate.bind(this);
var minimizer = this.minimizer;
fs.readFile(templatePath, {encoding: this.config.templateEncoding}, function(err, templateContent) {
fs.readFile(templatePath, { encoding: this.config.templateEncoding }, function (err, templateContent) {
if (err) {
onError('Can\'t read template file: "' + templatePath + '". Error details: ' + err);
return;
Expand All @@ -102,11 +128,11 @@ var Angular1Processor = extend(RegexpProcessor, {
});
},

embedTemplate : function(match, templateBuffer) {
embedTemplate: function (match, templateBuffer) {
return {
start : match.index,
start: match.index,
length: match[0].length,
replace: [TEMPLATE_BEGIN, templateBuffer, TEMPLATE_END]
replace: [this.getTemplateBegin(), templateBuffer, TEMPLATE_END]
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-angular-embed-templates",
"version": "2.3.0",
"version": "2.3.1",
"description": "gulp plugin to include the contents of angular templates inside directive's code",
"main": "index.js",
"scripts": {
Expand Down
127 changes: 97 additions & 30 deletions test/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ describe('gulp-angular-embed-templates', function () {
})
}

beforeEach(function() {
beforeEach(function () {
// Create a 'gulp-angular-embed-templates' plugin stream
sut = embedTemplates({debug: true});
sut = embedTemplates({ debug: true });
});

it('should dial with single quoted template paths', function (done) {
Expand Down Expand Up @@ -122,13 +122,13 @@ describe('gulp-angular-embed-templates', function () {
});

it('should skip errors if particular flag specified', function (done) {
testEmbed('skip-errors', done, {skipErrors: true});
testEmbed('skip-errors', done, { skipErrors: true });
});

it('should not skip errors if skipErrors not defined', function(done) {
it('should not skip errors if skipErrors not defined', function (done) {
testEmbed('skip-errors');

sut.once('error', function(error) {
sut.once('error', function (error) {
assert.instanceOf(error, PluginError, 'error should be gulp PluginError');
assert.equal(error.plugin, 'gulp-angular-embed-template');
expect(error.message).to.startWith('Can\'t read template file');
Expand All @@ -138,7 +138,7 @@ describe('gulp-angular-embed-templates', function () {

it('should use basePath to find the templates if specified', function (done) {
// TODO: this is the path on my local machine
sut = embedTemplates({basePath: testDir, debug: true});
sut = embedTemplates({ basePath: testDir, debug: true });
var entry = JSON.stringify({
templateUrl: '/cases/hello-world/template.html'
});
Expand All @@ -151,10 +151,10 @@ describe('gulp-angular-embed-templates', function () {
});

it('should ignore files bigger than the maxSize specified', function (done) {
testEmbed('max-size', done, {maxSize: 400})
testEmbed('max-size', done, { maxSize: 400 })
});

it('should embed template with quotes properly', function(done) {
it('should embed template with quotes properly', function (done) {
testEmbed('hard-attributes', done);
});

Expand All @@ -163,59 +163,126 @@ describe('gulp-angular-embed-templates', function () {
});

it('should embed Angular 2.0 templates with <a [router-link]="[\'/search\']">Search</a>', function (done) {
testEmbed('angular2-typescript', done, {sourceType: 'ts'});
testEmbed('angular2-typescript', done, { sourceType: 'ts' });
});

it('should not change attributes case (for Angular2.0 beta)', function (done) {
testEmbed('angular2-ngIf', done);
});

it('should embed template when find pattern "templateUrl: string = \'path\'"', function (done) {
testEmbed('angular2-inheritance', done, {sourceType: 'ts'});
testEmbed('angular2-inheritance', done, { sourceType: 'ts' });
});

it('should skip files if config.skipFiles function specified', function(done) {
testEmbed('skip-file', done, {skipFiles: function(file) {
var path = file.path;
return path.endsWith('skip-file/directive.js');
}});
it('should skip files if config.skipFiles function specified', function (done) {
testEmbed('skip-file', done, {
skipFiles: function (file) {
var path = file.path;
return path.endsWith('skip-file/directive.js');
}
});
});

it('should skip files if config.skipFiles pattern specified', function(done) {
testEmbed('skip-file', done, {skipFiles: /skip-file\/directive\.js$/});
it('should skip files if config.skipFiles pattern specified', function (done) {
testEmbed('skip-file', done, { skipFiles: /skip-file\/directive\.js$/ });
});

it('should skip certain template if config.skipTemplates regexp specified', function(done) {
testEmbed('skip-template', done, {skipTemplates: /\-large\.html$/});
it('should skip certain template if config.skipTemplates regexp specified', function (done) {
testEmbed('skip-template', done, { skipTemplates: /\-large\.html$/ });
});

it('should skip certain template if config.skipTemplates function specified', function(done) {
testEmbed('skip-template', done, {skipTemplates: function(templatePath, fileContext) {
return templatePath.endsWith('-large.html') && fileContext.path.indexOf('skip-template') !== -1;
}});
it('should skip certain template if config.skipTemplates function specified', function (done) {
testEmbed('skip-template', done, {
skipTemplates: function (templatePath, fileContext) {
return templatePath.endsWith('-large.html') && fileContext.path.indexOf('skip-template') !== -1;
}
});
});

it('should skip certain template if /*!*/ comment specified', function(done) {
it('should skip certain template if /*!*/ comment specified', function (done) {
testEmbed('skip-comment', done);
});

it('should embed only Angular1.x templates if sourceType "js" specified', function(done) {
testEmbed('angular2-ignore', done, {sourceType: 'js'});
it('should embed only Angular1.x templates if sourceType "js" specified', function (done) {
testEmbed('angular2-ignore', done, { sourceType: 'js' });
});

it('should embed templateUrl: path in Angular2.x just fine', function(done) {
it('should embed templateUrl: path in Angular2.x just fine', function (done) {
testEmbed('angular2-templateUrl', done);
});

it('should embed templateUrl: strign = \'path\' in class definition', function(done) {
testEmbed('angular2-class', done, {sourceType: 'ts'});
it('should embed templateUrl: strign = \'path\' in class definition', function (done) {
testEmbed('angular2-class', done, { sourceType: 'ts' });
});

it('should keep quotes if html attribute has space', function (done) {
testEmbed('img-attributes', done);
});

it('should allow to remove attribute quotes', function (done) {
testEmbed('attr-quotes-remove', done, {minimize:{quotes: false}});
testEmbed('attr-quotes-remove', done, { minimize: { quotes: false } });
})


describe('Support Angular1.x template=', function () {
beforeEach(function () {
// Create a 'gulp-angular-embed-templates' plugin stream
sut = embedTemplates({ debug: true, jsTemplateBegin: 'template=' });
});

it('should dial with single quoted template paths', function (done) {
var fakeFile = buildFakeFile('templateUrl= \'template.html\'');
sut.write(fakeFile);
sut.once('data', function (file) {
assert.equal(file.contents.toString('utf8'), 'template=\'<strong>Hello World!</strong>\'');
done();
});
});

it('should dial with double quoted template paths', function (done) {
var fakeFile = buildFakeFile('templateUrl= "template.html"');
sut.write(fakeFile);
sut.once('data', function (file) {
assert.equal(file.contents.toString('utf8'), 'template=\'<strong>Hello World!</strong>\'');
done();
});
});

it('should dial with new quotes ` in template paths', function (done) {
var fakeFile = buildFakeFile('templateUrl= `template.html`');
sut.write(fakeFile);
sut.once('data', function (file) {
assert.equal(file.contents.toString('utf8'), 'template=\'<strong>Hello World!</strong>\'');
done();
});
});

it('should dial with single quoted templateUrl key', function (done) {
var fakeFile = buildFakeFile('\'templateUrl\'= \'template.html\'');
sut.write(fakeFile);
sut.once('data', function (file) {
assert.equal(file.contents.toString('utf8'), 'template=\'<strong>Hello World!</strong>\'');
done();
});
});

it('should dial with double quoted templateUrl key', function (done) {
var fakeFile = buildFakeFile('"templateUrl"= \'template.html\'');
sut.write(fakeFile);
sut.once('data', function (file) {
assert.equal(file.contents.toString('utf8'), 'template=\'<strong>Hello World!</strong>\'');
done();
});
});

it('should dial with templateUrl {SPACES} : {SPACES} {url}', function (done) {
var fakeFile = buildFakeFile('"templateUrl" \t\r\n=\r\n\t \'template.html\'');
sut.write(fakeFile);
sut.once('data', function (file) {
assert.equal(file.contents.toString('utf8'), 'template=\'<strong>Hello World!</strong>\'');
done();
});
});

})
});