gulp-angular-gettext-annotate
Advanced tools
Comparing version 0.0.1 to 0.0.2
46
index.js
@@ -12,3 +12,3 @@ 'use strict'; | ||
options = _.extend({ | ||
inlineTags: ['br', 'b', 'i', 'u', 'strong'], | ||
inlineTags: ['br', 'b', 'i', 'u', 'strong', 'a'], | ||
attributesToAnnotate: ['placeholder', 'title', 'alt'] | ||
@@ -36,2 +36,13 @@ }, options); | ||
// We don't annotate anything in <head> | ||
var parents = node.parents(); | ||
if (!_.isEmpty(_.where(_.values(parents), {name: 'head'}))) { | ||
return; | ||
} | ||
// We don't annotate <style> and <script> | ||
if (node[0].type == 'style' || node[0].type == 'script') { | ||
return; | ||
} | ||
if (node[0].children.length === 1) { | ||
@@ -45,4 +56,3 @@ // If there is only one child it can be: | ||
var text = node[0].children[0].data.trim(); | ||
if (!(text.substr(0, 2) === '{{' && text.indexOf('}}') === text.length - 2) && | ||
typeof node.parent().attr('translate') === 'undefined') { | ||
if (!containsBindingsOnly(text) && typeof node.parent().attr('translate') === 'undefined') { | ||
annotate = true; | ||
@@ -65,4 +75,20 @@ } | ||
// If all text nodes are whitespaces we annotate child tag node(s). | ||
// | ||
// We're also checking if every child is text node and is either | ||
// whitespace or binding. If so, we don't annotate it. Example: | ||
// <div> | ||
// {{test1}} {{test2}} | ||
// </div> | ||
annotate = annotate && !_.every(node[0].children, function(child) { | ||
return (child.type === 'tag' || (child.type === 'text' && child.data.trim() === '')); | ||
if (child.type === 'tag') { | ||
return true; | ||
} | ||
if (child.type === 'text') { | ||
var text = child.data.trim(); | ||
// Empty string or binding | ||
return (text === '' || containsBindingsOnly(text)); | ||
} | ||
return true; | ||
}); | ||
@@ -96,2 +122,14 @@ } | ||
node.attr(attr, val); | ||
} | ||
/** | ||
* Returns true if string contains bindings ({{test}}) and whitespaces only. | ||
* @param string | ||
* @returns bool | ||
*/ | ||
function containsBindingsOnly(string) { | ||
var parts = string.split(/{{.+?}}/); | ||
return _.every(parts, function(part) { | ||
return (part.trim() === ''); | ||
}) | ||
} |
{ | ||
"name": "gulp-angular-gettext-annotate", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Gulp plugin for adding angular-gettext annotations to templates.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -14,2 +14,8 @@ 'use strict'; | ||
describe('gulp-angular-gettext-annotate', function () { | ||
afterEach(function(done){ | ||
// Remove annotated files | ||
del.sync('test/annotated'); | ||
done(); | ||
}); | ||
it('should annotate node with binding and text', function (done) { | ||
@@ -31,2 +37,22 @@ test('test/templates/testManyBindingsInTextNode.html', done); | ||
it('should not annotate anything in <head>', function (done) { | ||
test('test/templates/testHead.html', done); | ||
}); | ||
it('should not annotate anything in <script> and <style>', function (done) { | ||
test('test/templates/testScriptStyle.html', done); | ||
}); | ||
it('should not annotate nodes with bindings and whitespaces only', function (done) { | ||
test('test/templates/testBindingsAndWhitespaces.html', done); | ||
}); | ||
it('should not annotate nodes with bindings, tags and whitespaces only', function (done) { | ||
test('test/templates/testBindingsInlineTagsAndWhitespaces.html', done); | ||
}); | ||
it('should annotate lowest level nodes only', function (done) { | ||
test('test/templates/testEmptyTextNodes.html', done); | ||
}); | ||
it('should annotate stellar-client templates correctly', function (done) { | ||
@@ -54,6 +80,4 @@ test('test/templates/stellar-*.html', done); | ||
// Remove annotated files | ||
del.sync('test/annotated'); | ||
done(); | ||
}); | ||
} |
17267
25
174