blob: 3186ca0fb59aad4a8cbf7730384fdd95aab52693 [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2015 The Chromium Authors
dschuyler6c9376462015-07-17 03:47:432// 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>
Helmut Januschka36619c12024-04-24 14:33:1913#include <string_view>
dschuyler6c9376462015-07-17 03:47:4314
Henrique Ferreiro376c7652020-05-22 09:00:0415#include "base/component_export.h"
David Bienvenu31c293c2022-04-05 20:16:3216#include "base/values.h"
dschuylerfe6f5902017-01-05 01:10:0817
dschuyler6c9376462015-07-17 03:47:4318namespace ui {
19
dschuyler119857a2016-01-29 01:22:2020// Map of strings for template replacement in |ReplaceTemplateExpressions|.
Alex Yang690544aa32025-02-03 22:30:2321typedef std::map<std::string, std::string> TemplateReplacements;
dschuyler119857a2016-01-29 01:22:2022
dschuylerfe6f5902017-01-05 01:10:0823// Convert a dictionary to a replacement map. This helper function is to assist
24// migration to using TemplateReplacements directly (which is preferred).
25// TODO(dschuyler): remove this function by using TemplateReplacements directly.
Henrique Ferreiro376c7652020-05-22 09:00:0426COMPONENT_EXPORT(UI_BASE)
27void TemplateReplacementsFromDictionaryValue(
David Bienvenu31c293c2022-04-05 20:16:3228 const base::Value::Dict& dictionary,
dschuylerfe6f5902017-01-05 01:10:0829 TemplateReplacements* replacements);
30
dschuyler342667b2016-04-05 17:58:4031// Replace $i18n*{foo} in the format string with the value for the foo key in
rbpotter953ca772019-08-09 23:57:1232// |replacements|. If the key is not found in the |replacements| that item will
dschuyler6c9376462015-07-17 03:47:4333// be unaltered.
Henrique Ferreiro376c7652020-05-22 09:00:0434COMPONENT_EXPORT(UI_BASE)
35std::string ReplaceTemplateExpressions(
Helmut Januschka36619c12024-04-24 14:33:1936 std::string_view source,
Esmael El-Moslimany95e72bb2020-04-22 00:46:0937 const TemplateReplacements& replacements,
38 bool skip_unexpected_placeholder_check = false);
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` ... `,
Henrique Ferreiro376c7652020-05-22 09:00:0453COMPONENT_EXPORT(UI_BASE)
Helmut Januschka36619c12024-04-24 14:33:1954bool ReplaceTemplateExpressionsInJS(std::string_view source,
Henrique Ferreiro376c7652020-05-22 09:00:0455 const TemplateReplacements& replacements,
56 std::string* output);
rbpotter953ca772019-08-09 23:57:1257
dschuyler6c9376462015-07-17 03:47:4358} // namespace ui
59
60#endif // UI_BASE_TEMPLATE_EXPRESSIONS_H_