Commit 0b8f5779 authored by rbpotter's avatar rbpotter Committed by Commit Bot

Web UI: Update template_expressions to handle minified Polymer 3 files

Correctly detect HTML _templates in Polymer 3 files that have been
minified (so "_template: html`<div></div>`," becomes
"_template:html`<div></div>`,").

This is necessary to correctly replace i18n template expressions for
optimized Web UIs using Polymer 3.

Bug: 965770
Change-Id: I41112d5fa437964fa079ee5cfa41115324fa2579
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1877251Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708829}
parent 0874ae65
...@@ -22,7 +22,9 @@ const size_t kLeaderSize = base::size(kLeader) - 1; ...@@ -22,7 +22,9 @@ const size_t kLeaderSize = base::size(kLeader) - 1;
const char kKeyOpen = '{'; const char kKeyOpen = '{';
const char kKeyClose = '}'; const char kKeyClose = '}';
const char kHtmlTemplateStart[] = "_template: html`"; const char kHtmlTemplateStart[] = "_template: html`";
const char kHtmlTemplateStartMin[] = "_template:html`";
const size_t kHtmlTemplateStartSize = base::size(kHtmlTemplateStart) - 1; const size_t kHtmlTemplateStartSize = base::size(kHtmlTemplateStart) - 1;
const size_t kHtmlTemplateStartMinSize = base::size(kHtmlTemplateStartMin) - 1;
// Currently only legacy _template: html`...`, syntax is supported. // Currently only legacy _template: html`...`, syntax is supported.
enum HtmlTemplateType { INVALID = 0, NONE = 1, LEGACY = 2 }; enum HtmlTemplateType { INVALID = 0, NONE = 1, LEGACY = 2 };
...@@ -69,14 +71,21 @@ TemplatePosition FindHtmlTemplateEnd(const base::StringPiece& source) { ...@@ -69,14 +71,21 @@ TemplatePosition FindHtmlTemplateEnd(const base::StringPiece& source) {
HtmlTemplate FindHtmlTemplate(const base::StringPiece& source) { HtmlTemplate FindHtmlTemplate(const base::StringPiece& source) {
HtmlTemplate out; HtmlTemplate out;
base::StringPiece::size_type found = source.find(kHtmlTemplateStart); base::StringPiece::size_type found = source.find(kHtmlTemplateStart);
base::StringPiece::size_type found_min = base::StringPiece::npos;
if (found == base::StringPiece::npos) {
found_min = source.find(kHtmlTemplateStartMin);
}
// No template found, return early. // No template found, return early.
if (found == base::StringPiece::npos) { if (found == base::StringPiece::npos &&
found_min == base::StringPiece::npos) {
out.type = NONE; out.type = NONE;
return out; return out;
} }
out.start = found + kHtmlTemplateStartSize; out.start = found == base::StringPiece::npos
? found_min + kHtmlTemplateStartMinSize
: found + kHtmlTemplateStartSize;
TemplatePosition end = FindHtmlTemplateEnd(source.substr(out.start)); TemplatePosition end = FindHtmlTemplateEnd(source.substr(out.start));
// Template is not terminated. // Template is not terminated.
if (end.type == NONE) { if (end.type == NONE) {
......
...@@ -377,7 +377,12 @@ TEST(TemplateExpressionsTest, JSMultipleTemplates) { ...@@ -377,7 +377,12 @@ TEST(TemplateExpressionsTest, JSMultipleTemplates) {
"Polymer({\n" "Polymer({\n"
" _template: html`<div>number</div>`,\n" " _template: html`<div>number</div>`,\n"
" is: 'bar-element',\n" " is: 'bar-element',\n"
"});"}}; "});"},
// 2 minified templates, both with substitutions.
{"Polymer({_template:html`<div>$i18n{test}</div>`,is:'foo-element',});\n"
"Polymer({_template:html`<div>$i18n{5}</div>`,is:'bar-element',});",
"Polymer({_template:html`<div>word</div>`,is:'foo-element',});\n"
"Polymer({_template:html`<div>number</div>`,is:'bar-element',});"}};
std::string formatted; std::string formatted;
for (const TestCase test_case : kTestCases) { for (const TestCase test_case : kTestCases) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment