Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for ng-html-bind directive handling #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
24 changes: 19 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ var extractor
keyAsText = this.data.keyAsText || false,
adapter = this.data.adapter || 'json'

var escapedInterpolationStartRegex = new RegExp(_utils.escapeRegExpInterpolation(interpolation.startDelimiter), 'g'),
escapedInterpolationEndRegex = new RegExp(_utils.escapeRegExpInterpolation(interpolation.endDelimiter), 'g')

// Extract regex strings from content and feed results object
var _extractTranslation = (regexName, regex, content, results) => {
var r
Expand Down Expand Up @@ -104,20 +107,31 @@ var extractor

switch (regexName) {
case "commentSimpleQuote":
case "HtmlFilterSimpleQuote":
case "JavascriptServiceSimpleQuote":
case "JavascriptServiceInstantSimpleQuote":
case "JavascriptFilterSimpleQuote":
translationKey = translationKey.replace(/\\\'/g, "'");
break;
case "HtmlFilterSimpleQuote":
translationKey = translationKey.replace(/\\\'/g, "'");
translationKey = translationKey.replace(escapedInterpolationStartRegex, interpolation.startDelimiter);
translationKey = translationKey.replace(escapedInterpolationEndRegex, interpolation.endDelimiter);
break;
case "HtmlNgBindHtml":
translationKey = translationKey.replace(/\\\'/g, "'")
break
translationKey = translationKey.replace(/\\\'/g, "'");
translationKey = translationKey.replace(/"/g, '"');
break;
case "commentDoubleQuote":
case "HtmlFilterDoubleQuote":
case "JavascriptServiceDoubleQuote":
case "JavascriptServiceInstantDoubleQuote":
case "JavascriptFilterDoubleQuote":
translationKey = translationKey.replace(/\\\"/g, '"')
break
case "HtmlFilterDoubleQuote":
translationKey = translationKey.replace(/\\\"/g, '"')
translationKey = translationKey.replace(escapedInterpolationStartRegex, interpolation.startDelimiter);
translationKey = translationKey.replace(escapedInterpolationEndRegex, interpolation.endDelimiter);
break
case "JavascriptServiceArraySimpleQuote":
case "JavascriptServiceArrayDoubleQuote":
var key
Expand Down Expand Up @@ -185,7 +199,7 @@ var extractor
HtmlDirectivePluralLast: 'translate="((?:\\\\.|[^"\\\\])*)".*angular-plural-extract="((?:\\\\.|[^"\\\\])*)"',
HtmlDirectivePluralFirst: 'angular-plural-extract="((?:\\\\.|[^"\\\\])*)".*translate="((?:\\\\.|[^"\\\\])*)"',
HtmlNgBindHtml: 'ng-bind-html="\\s*\'((?:\\\\.|[^\'\\\\])*)\'\\s*\\|\\s*translate(:.*?)?\\s*"',
HtmlNgBindHtmlTernary: 'ng-bind-html="\\s*([^?]*?[^:]*:[^|}]*)\\s*\\|\\s*translate(:.*?)?\\s*"',
HtmlNgBindHtmlTernary: 'ng-bind-html="\\s*([^\\"?]*?[^\\":]*:[^\\"|}]*)\\s*\\|\\s*translate(:.*?)?\\s*"',
JavascriptServiceSimpleQuote: '\\$translate\\(\\s*\'((?:\\\\.|[^\'\\\\])*)\'[^\\)]*\\)',
JavascriptServiceDoubleQuote: '\\$translate\\(\\s*"((?:\\\\.|[^"\\\\])*)"[^\\)]*\\)',
JavascriptServiceArraySimpleQuote: '\\$translate\\((?:\\s*(\\[\\s*(?:(?:\'(?:(?:\\.|[^.*\'\\\\])*)\')\\s*,*\\s*)+\\s*\\])\\s*)\\)',
Expand Down
11 changes: 10 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ export class Utils {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")
}

/**
* Escape interpolation characters in the given str
* @param str
* @returns {void|string|XML}
*/
escapeRegExpInterpolation(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\\\\\$&")
}

/**
* Extract different part from a ternary operation
* eg: myVar ? 'myFirst result' : 'mySecond result'
Expand All @@ -83,4 +92,4 @@ export class Utils {
}
}

Utils.BASE_PATH_NAME = "basePath"
Utils.BASE_PATH_NAME = "basePath"
4 changes: 3 additions & 1 deletion tests/expected/00_fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"HtmlFilterDoubleQuote{} interp\"olation {{xx}}": "",
"HtmlFilterDoubleQuote{} on same line 2/2": "",
"HtmlFilterDoubleQuote{} {{var}}on same line 1/2": "",
"HtmlFilterSimpleQuote with {{varName}}": "",
"HtmlFilterSimpleQuoteOneTimeBinding": "",
"HtmlNgBindHtml Key ' 1/1": "",
"HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>": "",
"HtmlNgBindHtml Key ' 1/2": "",
"JavascriptFilter 1/2 with var \"{name}\"": "",
"JavascriptFilter 2/2 without var": "",
"JavascriptServiceArrayDoubleQuote 1/2 without var.": "",
Expand Down
4 changes: 3 additions & 1 deletion tests/expected/01_fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"HtmlFilterDoubleQuote{} interp\"olation {{xx}}": null,
"HtmlFilterDoubleQuote{} on same line 2/2": null,
"HtmlFilterDoubleQuote{} {{var}}on same line 1/2": null,
"HtmlFilterSimpleQuote with {{varName}}": null,
"HtmlFilterSimpleQuoteOneTimeBinding": null,
"HtmlNgBindHtml Key ' 1/1": null,
"HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>": null,
"HtmlNgBindHtml Key ' 1/2": null,
"JavascriptFilter 1/2 with var \"{name}\"": "my {name} javascript filter",
"JavascriptFilter 2/2 without var": null,
"JavascriptServiceArrayDoubleQuote 1/2 without var.": null,
Expand Down
4 changes: 3 additions & 1 deletion tests/expected/02_fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"HtmlFilterDoubleQuote{} interp\"olation {{xx}}": "",
"HtmlFilterDoubleQuote{} on same line 2/2": "",
"HtmlFilterDoubleQuote{} {{var}}on same line 1/2": "",
"HtmlFilterSimpleQuote with {{varName}}": "",
"HtmlFilterSimpleQuoteOneTimeBinding": "",
"HtmlNgBindHtml Key ' 1/1": "",
"HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>": "",
"HtmlNgBindHtml Key ' 1/2": "",
"JavascriptFilter 1/2 with var \"{name}\"": "my {name} javascript filter",
"JavascriptFilter 2/2 without var": "",
"JavascriptServiceArrayDoubleQuote 1/2 without var.": "",
Expand Down
3 changes: 2 additions & 1 deletion tests/expected/03_fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"HtmlFilterSimpleQuote[] interp'olation [[xx]]": "",
"HtmlFilterSimpleQuote[] on same line 1/2": "",
"HtmlFilterSimpleQuote[] on same line 2/2": "",
"HtmlNgBindHtml Key ' 1/1": "",
"HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>": "",
"HtmlNgBindHtml Key ' 1/2": "",
"JavascriptFilter 1/2 with var \"{name}\"": "",
"JavascriptFilter 2/2 without var": "",
"JavascriptServiceArrayDoubleQuote 1/2 without var.": "",
Expand Down
1 change: 1 addition & 0 deletions tests/expected/04_en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"HtmlFilterDoubleQuote{} on same line 2/2": "HtmlFilterDoubleQuote{} on same line 2/2",
"HtmlFilterDoubleQuote{} {{var}}on same line 1/2": "HtmlFilterDoubleQuote{} {{var}}on same line 1/2",
"HtmlFilterSimpleQuoteOneTimeBinding": "HtmlFilterSimpleQuoteOneTimeBinding",
"HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>": "HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>",
"HtmlNgBindHtml Key ' 1/1": "HtmlNgBindHtml Key ' 1/1",
"JavascriptFilter 1/2 with var \"{name}\"": "JavascriptFilter 1/2 with var \"{name}\"",
"JavascriptFilter 2/2 without var": "JavascriptFilter 2/2 without var",
Expand Down
4 changes: 3 additions & 1 deletion tests/expected/04_fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"HtmlFilterDoubleQuote{} interp\"olation {{xx}}": "",
"HtmlFilterDoubleQuote{} on same line 2/2": "",
"HtmlFilterDoubleQuote{} {{var}}on same line 1/2": "",
"HtmlFilterSimpleQuote with {{varName}}": "",
"HtmlFilterSimpleQuoteOneTimeBinding": "",
"HtmlNgBindHtml Key ' 1/1": "",
"HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>": "",
"HtmlNgBindHtml Key ' 1/2": "",
"JavascriptFilter 1/2 with var \"{name}\"": "",
"JavascriptFilter 2/2 without var": "",
"JavascriptServiceArrayDoubleQuote 1/2 without var.": "",
Expand Down
4 changes: 3 additions & 1 deletion tests/expected/05_en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"HtmlFilterDoubleQuote{} interp\"olation {{xx}}": "HtmlFilterDoubleQuote{} interp\"olation {{xx}}",
"HtmlFilterDoubleQuote{} on same line 2/2": "HtmlFilterDoubleQuote{} on same line 2/2",
"HtmlFilterDoubleQuote{} {{var}}on same line 1/2": "HtmlFilterDoubleQuote{} {{var}}on same line 1/2",
"HtmlFilterSimpleQuote with {{varName}}": "HtmlFilterSimpleQuote with {{varName}}",
"HtmlFilterSimpleQuoteOneTimeBinding": "HtmlFilterSimpleQuoteOneTimeBinding",
"HtmlNgBindHtml Key ' 1/1": "HtmlNgBindHtml Key ' 1/1",
"HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>": "HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>",
"HtmlNgBindHtml Key ' 1/2": "HtmlNgBindHtml Key ' 1/2",
"JavascriptFilter 1/2 with var \"{name}\"": "JavascriptFilter 1/2 with var \"{name}\"",
"JavascriptFilter 2/2 without var": "JavascriptFilter 2/2 without var",
"JavascriptServiceArrayDoubleQuote 1/2 without var.": "JavascriptServiceArrayDoubleQuote 1/2 without var.",
Expand Down
4 changes: 3 additions & 1 deletion tests/expected/10_1_fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"HtmlFilterDoubleQuote{} interp\"olation {{xx}}": "",
"HtmlFilterDoubleQuote{} on same line 2/2": "",
"HtmlFilterDoubleQuote{} {{var}}on same line 1/2": "",
"HtmlFilterSimpleQuote with {{varName}}": "",
"HtmlFilterSimpleQuoteOneTimeBinding": "",
"HtmlNgBindHtml Key ' 1/1": "",
"HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>": "",
"HtmlNgBindHtml Key ' 1/2": "",
"JavascriptFilter 1/2 with var \"{name}\"": "",
"JavascriptFilter 2/2 without var": "",
"JavascriptServiceArrayDoubleQuote 1/2 without var.": "",
Expand Down
4 changes: 3 additions & 1 deletion tests/expected/10_fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"HtmlFilterDoubleQuote{} interp\"olation {{xx}}": "",
"HtmlFilterDoubleQuote{} on same line 2/2": "",
"HtmlFilterDoubleQuote{} {{var}}on same line 1/2": "",
"HtmlFilterSimpleQuote with {{varName}}": "",
"HtmlFilterSimpleQuoteOneTimeBinding": "",
"HtmlNgBindHtml Key ' 1/1": "",
"HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>": "",
"HtmlNgBindHtml Key ' 1/2": "",
"JavascriptFilter 1/2 with var \"{name}\"": "",
"JavascriptFilter 2/2 without var": "",
"JavascriptServiceArrayDoubleQuote 1/2 without var.": "",
Expand Down
10 changes: 9 additions & 1 deletion tests/expected/template.pot
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,20 @@ msgctxt ""
msgid "HtmlFilterDoubleQuote{} on same line 2/2"
msgstr ""

msgctxt ""
msgid "HtmlFilterSimpleQuote with {{varName}}"
msgstr ""

msgctxt ""
msgid "HtmlFilterSimpleQuoteOneTimeBinding"
msgstr ""

msgctxt ""
msgid "HtmlNgBindHtml Key ' 1/1"
msgid "HtmlNgBindHtml 2/2 Created with <i class=\"ion-heart\"></i>"
msgstr ""

msgctxt ""
msgid "HtmlNgBindHtml Key ' 1/2"
msgstr ""

msgctxt ""
Expand Down
18 changes: 16 additions & 2 deletions tests/fixtures/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ <h2>[['HtmlFilterSimpleQuote[] on same line 1/2'| translate:'{var : 1}']] - [['H
<h1 style="color:red">{{ ::'HtmlFilterSimpleQuoteOneTimeBinding' | translate }}</h1>
</header>

<!--
TEST CASE - Filter HTML
Interpolation: {{ }}
Simple quote
Variables
-->
<header style="font-family:'Helvetica Neue'">
<h1 description="{{ 'HtmlFilterSimpleQuote with \{\{varName\}\}' | translate:{varName: 'Variables'} }}">Inspect 'description' attribute</h1>
</header>

<!--
TEST CASE - Filter HTML
Interpolation: {{ }}
Expand Down Expand Up @@ -69,7 +79,11 @@ <h1>Our title text</h1>
TEST CASE - ng-bind-html
-->
<p>
<span style="text-align:center" ng-bind-html="'HtmlNgBindHtml Key \' 1/1'|translate"></span>
<span style="text-align:center" ng-bind-html="'HtmlNgBindHtml Key \' 1/2'|translate"></span>
</p>

<p>
<span style="text-align:center" ng-bind-html="'HtmlNgBindHtml 2/2 Created with <i class=&quot;ion-heart&quot;></i>' | translate"></span>
</p>

<!--
Expand All @@ -88,4 +102,4 @@ <h1>Our title text</h1>

</body>

</html>
</html>