Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: ui/base/template_expressions.cc

Issue 2607843002: [MD settings] i18n template replacements in shared HTML resources (Closed)
Patch Set: added TemplateReplacementsSetBool Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/template_expressions.h ('k') | ui/base/webui/jstemplate_builder.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/base/template_expressions.h" 5 #include "ui/base/template_expressions.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/values.h"
10 #include "net/base/escape.h" 11 #include "net/base/escape.h"
11 12
12 namespace { 13 namespace {
13 const char kLeader[] = "$i18n"; 14 const char kLeader[] = "$i18n";
14 const size_t kLeaderSize = arraysize(kLeader) - 1; 15 const size_t kLeaderSize = arraysize(kLeader) - 1;
15 const char kKeyOpen = '{'; 16 const char kKeyOpen = '{';
16 const char kKeyClose = '}'; 17 const char kKeyClose = '}';
17 } // namespace 18 } // namespace
18 19
19 namespace ui { 20 namespace ui {
20 21
22 void TemplateReplacementsFromDictionaryValue(
23 const base::DictionaryValue& dictionary,
24 TemplateReplacements* replacements) {
25 for (base::DictionaryValue::Iterator it(dictionary); !it.IsAtEnd();
26 it.Advance()) {
27 if (it.value().IsType(base::Value::Type::STRING)) {
28 std::string str_value;
29 if (it.value().GetAsString(&str_value))
30 (*replacements)[it.key()] = str_value;
31 } else if (it.value().IsType(base::Value::Type::BOOLEAN)) {
32 bool bool_value;
33 if (it.value().GetAsBoolean(&bool_value))
34 TemplateReplacementsSetBool(it.key(), bool_value, replacements);
35 }
36 }
37 }
38
39 void TemplateReplacementsSetBool(const std::string& name,
40 bool value,
41 TemplateReplacements* replacements) {
42 // These are HTML Boolean attributes which are represented by their name
43 // if truthy and by complete absence (empty string) if falsy.
Dan Beam 2017/01/03 20:03:15 this comment is confusing. hidden="" hides stuff,
dschuyler 2017/01/03 20:36:56 Correct, so in this case: <div $i18n{guestMode}>.
44 (*replacements)[name] = value ? name : "";
45 }
46
21 std::string ReplaceTemplateExpressions( 47 std::string ReplaceTemplateExpressions(
22 base::StringPiece source, 48 base::StringPiece source,
23 const TemplateReplacements& replacements) { 49 const TemplateReplacements& replacements) {
24 std::string formatted; 50 std::string formatted;
25 const size_t kValueLengthGuess = 16; 51 const size_t kValueLengthGuess = 16;
26 formatted.reserve(source.length() + replacements.size() * kValueLengthGuess); 52 formatted.reserve(source.length() + replacements.size() * kValueLengthGuess);
27 // Two position markers are used as cursors through the |source|. 53 // Two position markers are used as cursors through the |source|.
28 // The |current_pos| will follow behind |next_pos|. 54 // The |current_pos| will follow behind |next_pos|.
29 size_t current_pos = 0; 55 size_t current_pos = 0;
30 while (true) { 56 while (true) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 } 94 }
69 95
70 formatted.append(replacement); 96 formatted.append(replacement);
71 97
72 current_pos = key_end + sizeof(kKeyClose); 98 current_pos = key_end + sizeof(kKeyClose);
73 } 99 }
74 return formatted; 100 return formatted;
75 } 101 }
76 102
77 } // namespace ui 103 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/template_expressions.h ('k') | ui/base/webui/jstemplate_builder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698