From 33c76eec0fed9c7258cac194aaa3ee9f43a919bf Mon Sep 17 00:00:00 2001 From: roopkt Date: Fri, 13 Jul 2018 12:51:42 -0400 Subject: [PATCH 1/2] #36 configuring templatebegin for angular1 application support --- README.md | 7 ++++++ lib/Angular1Processor.js | 48 +++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index fc64423..1777435 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/Angular1Processor.js b/lib/Angular1Processor.js index 688471d..7fd6518 100644 --- a/lib/Angular1Processor.js +++ b/lib/Angular1Processor.js @@ -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) { @@ -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 } ); } @@ -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; + } }, /** @@ -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); @@ -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; @@ -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] } } }); From 8f21be9e8d961c6f974e7f1297f9ebe82576e4e8 Mon Sep 17 00:00:00 2001 From: roopkt Date: Fri, 13 Jul 2018 13:05:21 -0400 Subject: [PATCH 2/2] added tests and updated package.json and changelog.md #36 --- CHANGELOG.md | 5 ++ package.json | 2 +- test/mocha.js | 127 ++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 103 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfd5f2a..f067898 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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} diff --git a/package.json b/package.json index f6c6901..1ad77d1 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/mocha.js b/test/mocha.js index 56e889c..c850df8 100644 --- a/test/mocha.js +++ b/test/mocha.js @@ -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) { @@ -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'); @@ -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' }); @@ -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); }); @@ -163,7 +163,7 @@ describe('gulp-angular-embed-templates', function () { }); it('should embed Angular 2.0 templates with Search', 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) { @@ -171,44 +171,48 @@ describe('gulp-angular-embed-templates', function () { }); 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) { @@ -216,6 +220,69 @@ describe('gulp-angular-embed-templates', function () { }); 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=\'Hello World!\''); + 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=\'Hello World!\''); + 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=\'Hello World!\''); + 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=\'Hello World!\''); + 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=\'Hello World!\''); + 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=\'Hello World!\''); + done(); + }); + }); + }) }); \ No newline at end of file