summaryrefslogtreecommitdiffstats
path: root/ui/base
diff options
context:
space:
mode:
Diffstat (limited to 'ui/base')
-rw-r--r--ui/base/BUILD.gn3
-rw-r--r--ui/base/template_expressions.cc42
-rw-r--r--ui/base/template_expressions.h28
-rw-r--r--ui/base/template_expressions_unittest.cc36
-rw-r--r--ui/base/ui_base.gyp3
-rw-r--r--ui/base/ui_base_tests.gyp1
-rw-r--r--ui/base/webui/web_ui_util.cc11
7 files changed, 119 insertions, 5 deletions
diff --git a/ui/base/BUILD.gn b/ui/base/BUILD.gn
index d6f5ed3..00ad6a4 100644
--- a/ui/base/BUILD.gn
+++ b/ui/base/BUILD.gn
@@ -222,6 +222,8 @@ component("base") {
"resource/resource_data_dll_win.cc",
"resource/resource_data_dll_win.h",
"resource/resource_handle.h",
+ "template_expressions.cc",
+ "template_expressions.h",
"text/bytes_formatting.cc",
"text/bytes_formatting.h",
"theme_provider.cc",
@@ -639,6 +641,7 @@ test("ui_base_unittests") {
"resource/data_pack_unittest.cc",
"resource/material_design/material_design_controller_unittest.cc",
"resource/resource_bundle_unittest.cc",
+ "template_expressions_unittest.cc",
"test/run_all_unittests.cc",
"test/scoped_fake_nswindow_fullscreen_unittest.mm",
"test/test_clipboard_unittest.cc",
diff --git a/ui/base/template_expressions.cc b/ui/base/template_expressions.cc
new file mode 100644
index 0000000..43cfa53
--- /dev/null
+++ b/ui/base/template_expressions.cc
@@ -0,0 +1,42 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/base/template_expressions.h"
+
+#include "base/logging.h"
+
+namespace ui {
+
+std::string ReplaceTemplateExpressions(
+ base::StringPiece format_string,
+ const std::map<base::StringPiece, std::string>& substitutions) {
+ std::string formatted;
+ const size_t kValueLengthGuess = 16;
+ formatted.reserve(format_string.length() +
+ substitutions.size() * kValueLengthGuess);
+ base::StringPiece::const_iterator i = format_string.begin();
+ while (i < format_string.end()) {
+ if (*i == '$' && i + 1 < format_string.end() && i[1] == '{') {
+ size_t key_start = i + strlen("${") - format_string.begin();
+ size_t key_length = format_string.find('}', key_start);
+ if (key_length == base::StringPiece::npos)
+ NOTREACHED() << "TemplateExpression missing ending brace '}'";
+ key_length -= key_start;
+ base::StringPiece key(format_string.begin() + key_start, key_length);
+ const auto& replacement = substitutions.find(key);
+ if (replacement != substitutions.end()) {
+ formatted.append(replacement->second);
+ i += strlen("${") + key_length + strlen("}");
+ continue;
+ } else {
+ NOTREACHED() << "TemplateExpression key not found: " << key;
+ }
+ }
+ formatted.push_back(*i);
+ ++i;
+ }
+ return formatted;
+}
+
+} // namespace ui
diff --git a/ui/base/template_expressions.h b/ui/base/template_expressions.h
new file mode 100644
index 0000000..ff14ee1
--- /dev/null
+++ b/ui/base/template_expressions.h
@@ -0,0 +1,28 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file defines utility functions for replacing template expressions.
+// For example "Hello ${name}" could have ${name} replaced by the user's name.
+
+#ifndef UI_BASE_TEMPLATE_EXPRESSIONS_H_
+#define UI_BASE_TEMPLATE_EXPRESSIONS_H_
+
+#include <map>
+#include <string>
+
+#include "base/strings/string_piece.h"
+#include "ui/base/ui_base_export.h"
+
+namespace ui {
+
+// Replace ${foo} in the format string with the value for the foo key in
+// |subst|. If the key is not found in the |substitutions| that item will
+// be unaltered.
+UI_BASE_EXPORT std::string ReplaceTemplateExpressions(
+ base::StringPiece format_string,
+ const std::map<base::StringPiece, std::string>& substitutions);
+
+} // namespace ui
+
+#endif // UI_BASE_TEMPLATE_EXPRESSIONS_H_
diff --git a/ui/base/template_expressions_unittest.cc b/ui/base/template_expressions_unittest.cc
new file mode 100644
index 0000000..f5c3631
--- /dev/null
+++ b/ui/base/template_expressions_unittest.cc
@@ -0,0 +1,36 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/base/template_expressions.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace ui {
+
+TEST(TemplateExpressionsTest, ReplaceTemplateExpressionsPieces) {
+ std::map<base::StringPiece, std::string> substitutions;
+ substitutions["test"] = "word";
+ substitutions["5"] = "number";
+ substitutions[""] = "blank";
+
+ EXPECT_EQ(ReplaceTemplateExpressions("${}", substitutions), "blank");
+ EXPECT_EQ(ReplaceTemplateExpressions("", substitutions), "");
+ EXPECT_EQ(ReplaceTemplateExpressions("${test}", substitutions), "word");
+ EXPECT_EQ(ReplaceTemplateExpressions("${5} ", substitutions), "number ");
+ EXPECT_EQ(
+ ReplaceTemplateExpressions("multiple: ${test}, ${5}.", substitutions),
+ "multiple: word, number.");
+}
+
+TEST(TemplateExpressionsTest,
+ ReplaceTemplateExpressionsConsecutiveDollarSignsPieces) {
+ std::map<base::StringPiece, std::string> substitutions;
+ substitutions["a"] = "x";
+ EXPECT_EQ(ReplaceTemplateExpressions("$ $$ $$$", substitutions), "$ $$ $$$");
+ EXPECT_EQ(ReplaceTemplateExpressions("$${a}", substitutions), "$x");
+ EXPECT_EQ(ReplaceTemplateExpressions("$$${a}", substitutions), "$$x");
+ EXPECT_EQ(ReplaceTemplateExpressions("$12", substitutions), "$12");
+}
+
+} // namespace ui
diff --git a/ui/base/ui_base.gyp b/ui/base/ui_base.gyp
index 3f8f69e..9787853 100644
--- a/ui/base/ui_base.gyp
+++ b/ui/base/ui_base.gyp
@@ -271,6 +271,8 @@
'resource/resource_data_dll_win.cc',
'resource/resource_data_dll_win.h',
'resource/resource_handle.h',
+ 'template_expressions.cc',
+ 'template_expressions.h',
'text/bytes_formatting.cc',
'text/bytes_formatting.h',
'theme_provider.cc',
@@ -368,6 +370,7 @@
['include', '^layout'],
['include', '^page_transition_type'],
['include', '^resource/'],
+ ['include', 'template_expressions.cc'],
['include', '^ui_base_'],
['include', '^webui/'],
['include', '^window_open_disposition\\.cc'],
diff --git a/ui/base/ui_base_tests.gyp b/ui/base/ui_base_tests.gyp
index ef433e0..d47436b 100644
--- a/ui/base/ui_base_tests.gyp
+++ b/ui/base/ui_base_tests.gyp
@@ -44,6 +44,7 @@
'resource/data_pack_literal.cc',
'resource/data_pack_unittest.cc',
'resource/resource_bundle_unittest.cc',
+ 'template_expressions_unittest.cc',
'test/run_all_unittests.cc',
],
'all_sources': [
diff --git a/ui/base/webui/web_ui_util.cc b/ui/base/webui/web_ui_util.cc
index c0f5a9d..6483f07 100644
--- a/ui/base/webui/web_ui_util.cc
+++ b/ui/base/webui/web_ui_util.cc
@@ -16,6 +16,7 @@
#include "net/base/escape.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
+#include "ui/base/template_expressions.h"
#include "ui/base/window_open_disposition.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/font.h"
@@ -119,10 +120,10 @@ void SetLoadTimeDataDefaults(const std::string& app_locale,
}
std::string GetWebUiCssTextDefaults() {
- std::vector<std::string> placeholders;
- placeholders.push_back(GetTextDirection()); // $1
- placeholders.push_back(GetFontFamily()); // $2
- placeholders.push_back(GetFontSize()); // $3
+ std::map<base::StringPiece, std::string> placeholders;
+ placeholders["textDirection"] = GetTextDirection();
+ placeholders["fontFamily"] = GetFontFamily();
+ placeholders["fontSize"] = GetFontSize();
const ui::ResourceBundle& resource_bundle =
ui::ResourceBundle::GetSharedInstance();
@@ -130,7 +131,7 @@ std::string GetWebUiCssTextDefaults() {
resource_bundle.GetRawDataResource(IDR_WEBUI_CSS_TEXT_DEFAULTS)
.as_string();
- return base::ReplaceStringPlaceholders(css_template, placeholders, nullptr);
+ return ui::ReplaceTemplateExpressions(css_template, placeholders);
}
void AppendWebUiCssTextDefaults(std::string* html) {