Commit e1ded62c authored by rbpotter's avatar rbpotter Committed by Commit Bot

Web UI: Update template_expressions.cc to use new start/end markers

Update template expressions replacement to use the new HTML template
start and end markers instead of looking for _template: html` and `,.
This allows some simplification of the logic and will enable i18n
replacements in JS files using class based Polymer 3 syntax, since
the autogeneration code adds these markers to the start and end of
HTML templates at build time regardless of syntax.

Also modifying some existing tests to use class based syntax, and
adding more tests for less common cases and for mixed class/legacy
syntax.

Bug: 1023841
Change-Id: I764ee18621bc526f259b7b970e6008e0de220711
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1935609Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#719259}
parent acc77059
...@@ -21,18 +21,11 @@ const char kLeader[] = "$i18n"; ...@@ -21,18 +21,11 @@ const char kLeader[] = "$i18n";
const size_t kLeaderSize = base::size(kLeader) - 1; 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 kHtmlTemplateEnd[] = "<!--_html_template_end_-->";
const char kHtmlTemplateStartMin[] = "_template:html`"; const char kHtmlTemplateStart[] = "<!--_html_template_start_-->";
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. enum HtmlTemplateType { INVALID = 0, NONE = 1, VALID = 2 };
enum HtmlTemplateType { INVALID = 0, NONE = 1, LEGACY = 2 };
struct TemplatePosition {
HtmlTemplateType type;
base::StringPiece::size_type position;
};
struct HtmlTemplate { struct HtmlTemplate {
base::StringPiece::size_type start; base::StringPiece::size_type start;
...@@ -40,60 +33,26 @@ struct HtmlTemplate { ...@@ -40,60 +33,26 @@ struct HtmlTemplate {
HtmlTemplateType type; HtmlTemplateType type;
}; };
TemplatePosition FindHtmlTemplateEnd(const base::StringPiece& source) {
enum State { OPEN, IN_ESCAPE, IN_TICK };
State state = OPEN;
for (base::StringPiece::size_type i = 0; i < source.length(); i++) {
if (state == IN_ESCAPE) {
state = OPEN; // Consume
continue;
}
switch (source[i]) {
case '\\':
state = IN_ESCAPE;
break;
case '`':
state = IN_TICK;
break;
case ',':
if (state == IN_TICK)
return {LEGACY, i - 1};
FALLTHROUGH;
default:
state = OPEN;
}
}
return {NONE, base::StringPiece::npos};
}
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 == base::StringPiece::npos out.start = found + kHtmlTemplateStartSize;
? found_min + kHtmlTemplateStartMinSize base::StringPiece::size_type found_end =
: found + kHtmlTemplateStartSize; source.find(kHtmlTemplateEnd, out.start);
TemplatePosition end = FindHtmlTemplateEnd(source.substr(out.start));
// Template is not terminated. // Template is not terminated.
if (end.type == NONE) { if (found_end == base::StringPiece::npos) {
out.type = INVALID; out.type = INVALID;
return out; return out;
} }
out.length = end.position; out.length = found_end - out.start;
// Check for a nested template // Check for a nested template
if (source.substr(out.start, out.length).find(kHtmlTemplateStart) != if (source.substr(out.start, out.length).find(kHtmlTemplateStart) !=
base::StringPiece::npos) { base::StringPiece::npos) {
...@@ -101,7 +60,7 @@ HtmlTemplate FindHtmlTemplate(const base::StringPiece& source) { ...@@ -101,7 +60,7 @@ HtmlTemplate FindHtmlTemplate(const base::StringPiece& source) {
return out; return out;
} }
out.type = LEGACY; out.type = VALID;
return out; return out;
} }
......
This diff is collapsed.
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