blob: a12f5b2fad8504841f492ff25d5518b86164ed5f [file] [log] [blame]
dschuyler6c9376462015-07-17 03:47:431// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file defines utility functions for replacing template expressions.
6// For example "Hello ${name}" could have ${name} replaced by the user's name.
7
8#ifndef UI_BASE_TEMPLATE_EXPRESSIONS_H_
9#define UI_BASE_TEMPLATE_EXPRESSIONS_H_
10
11#include <map>
12#include <string>
13
14#include "base/strings/string_piece.h"
15#include "ui/base/ui_base_export.h"
16
dschuylerfe6f5902017-01-05 01:10:0817namespace base {
18class DictionaryValue;
19}
20
dschuyler6c9376462015-07-17 03:47:4321namespace ui {
22
dschuyler119857a2016-01-29 01:22:2023// Map of strings for template replacement in |ReplaceTemplateExpressions|.
24typedef std::map<const std::string, std::string> TemplateReplacements;
25
dschuylerfe6f5902017-01-05 01:10:0826// Convert a dictionary to a replacement map. This helper function is to assist
27// migration to using TemplateReplacements directly (which is preferred).
28// TODO(dschuyler): remove this function by using TemplateReplacements directly.
29UI_BASE_EXPORT void TemplateReplacementsFromDictionaryValue(
30 const base::DictionaryValue& dictionary,
31 TemplateReplacements* replacements);
32
dschuyler342667b2016-04-05 17:58:4033// Replace $i18n*{foo} in the format string with the value for the foo key in
rbpotter953ca772019-08-09 23:57:1234// |replacements|. If the key is not found in the |replacements| that item will
dschuyler6c9376462015-07-17 03:47:4335// be unaltered.
36UI_BASE_EXPORT std::string ReplaceTemplateExpressions(
dschuyler342667b2016-04-05 17:58:4037 base::StringPiece source,
Esmael El-Moslimany95e72bb2020-04-22 00:46:0938 const TemplateReplacements& replacements,
39 bool skip_unexpected_placeholder_check = false);
dschuyler6c9376462015-07-17 03:47:4340
rbpotter953ca772019-08-09 23:57:1241// Replace $i18n*{foo} in the HTML template contained in |source| with the
42// value for the foo key in |replacements| and return the result in |output|.
43// Only $i18n*{...} expressions in the HTML portion of the JS source will be
44// replaced; such expressions in the rest of the JS code will be left unaltered.
45// If no template is found, |source| will be returned in |output| unaltered. If
46// a key is not found in the |replacements| that item will be unaltered. Returns
47// true on success, false on failure. On failure, |output| will not populated.
48// Replacement will fail if a single HTML template string cannot be identified
49// (e.g. not terminated, multiple _template: html`... in a single file), or if
50// executing the replacements would be unsafe (e.g. result in unescaped
51// backticks or "${" within the HTML string).
52// Note: Currently, this only supports the legacy Polymer syntax, i.e.:
53// _template: html` ... `,
54UI_BASE_EXPORT bool ReplaceTemplateExpressionsInJS(
55 base::StringPiece source,
56 const TemplateReplacements& replacements,
57 std::string* output);
58
dschuyler6c9376462015-07-17 03:47:4359} // namespace ui
60
61#endif // UI_BASE_TEMPLATE_EXPRESSIONS_H_