blob: a2768258553686acbd655bf116b126918264e52e [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,
38 const TemplateReplacements& replacements);
dschuyler6c9376462015-07-17 03:47:4339
rbpotter953ca772019-08-09 23:57:1240// Replace $i18n*{foo} in the HTML template contained in |source| with the
41// value for the foo key in |replacements| and return the result in |output|.
42// Only $i18n*{...} expressions in the HTML portion of the JS source will be
43// replaced; such expressions in the rest of the JS code will be left unaltered.
44// If no template is found, |source| will be returned in |output| unaltered. If
45// a key is not found in the |replacements| that item will be unaltered. Returns
46// true on success, false on failure. On failure, |output| will not populated.
47// Replacement will fail if a single HTML template string cannot be identified
48// (e.g. not terminated, multiple _template: html`... in a single file), or if
49// executing the replacements would be unsafe (e.g. result in unescaped
50// backticks or "${" within the HTML string).
51// Note: Currently, this only supports the legacy Polymer syntax, i.e.:
52// _template: html` ... `,
53UI_BASE_EXPORT bool ReplaceTemplateExpressionsInJS(
54 base::StringPiece source,
55 const TemplateReplacements& replacements,
56 std::string* output);
57
dschuyler6c9376462015-07-17 03:47:4358} // namespace ui
59
60#endif // UI_BASE_TEMPLATE_EXPRESSIONS_H_