Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
dschuyler | 6c937646 | 2015-07-17 03:47:43 | [diff] [blame] | 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> |
Helmut Januschka | 36619c1 | 2024-04-24 14:33:19 | [diff] [blame] | 13 | #include <string_view> |
dschuyler | 6c937646 | 2015-07-17 03:47:43 | [diff] [blame] | 14 | |
Henrique Ferreiro | 376c765 | 2020-05-22 09:00:04 | [diff] [blame] | 15 | #include "base/component_export.h" |
David Bienvenu | 31c293c | 2022-04-05 20:16:32 | [diff] [blame] | 16 | #include "base/values.h" |
dschuyler | fe6f590 | 2017-01-05 01:10:08 | [diff] [blame] | 17 | |
dschuyler | 6c937646 | 2015-07-17 03:47:43 | [diff] [blame] | 18 | namespace ui { |
| 19 | |
dschuyler | 119857a | 2016-01-29 01:22:20 | [diff] [blame] | 20 | // Map of strings for template replacement in |ReplaceTemplateExpressions|. |
Alex Yang | 690544aa3 | 2025-02-03 22:30:23 | [diff] [blame] | 21 | typedef std::map<std::string, std::string> TemplateReplacements; |
dschuyler | 119857a | 2016-01-29 01:22:20 | [diff] [blame] | 22 | |
dschuyler | fe6f590 | 2017-01-05 01:10:08 | [diff] [blame] | 23 | // 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 Ferreiro | 376c765 | 2020-05-22 09:00:04 | [diff] [blame] | 26 | COMPONENT_EXPORT(UI_BASE) |
| 27 | void TemplateReplacementsFromDictionaryValue( |
David Bienvenu | 31c293c | 2022-04-05 20:16:32 | [diff] [blame] | 28 | const base::Value::Dict& dictionary, |
dschuyler | fe6f590 | 2017-01-05 01:10:08 | [diff] [blame] | 29 | TemplateReplacements* replacements); |
| 30 | |
dschuyler | 342667b | 2016-04-05 17:58:40 | [diff] [blame] | 31 | // Replace $i18n*{foo} in the format string with the value for the foo key in |
rbpotter | 953ca77 | 2019-08-09 23:57:12 | [diff] [blame] | 32 | // |replacements|. If the key is not found in the |replacements| that item will |
dschuyler | 6c937646 | 2015-07-17 03:47:43 | [diff] [blame] | 33 | // be unaltered. |
Henrique Ferreiro | 376c765 | 2020-05-22 09:00:04 | [diff] [blame] | 34 | COMPONENT_EXPORT(UI_BASE) |
| 35 | std::string ReplaceTemplateExpressions( |
Helmut Januschka | 36619c1 | 2024-04-24 14:33:19 | [diff] [blame] | 36 | std::string_view source, |
Esmael El-Moslimany | 95e72bb | 2020-04-22 00:46:09 | [diff] [blame] | 37 | const TemplateReplacements& replacements, |
| 38 | bool skip_unexpected_placeholder_check = false); |
dschuyler | 6c937646 | 2015-07-17 03:47:43 | [diff] [blame] | 39 | |
rbpotter | 953ca77 | 2019-08-09 23:57:12 | [diff] [blame] | 40 | // 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 Ferreiro | 376c765 | 2020-05-22 09:00:04 | [diff] [blame] | 53 | COMPONENT_EXPORT(UI_BASE) |
Helmut Januschka | 36619c1 | 2024-04-24 14:33:19 | [diff] [blame] | 54 | bool ReplaceTemplateExpressionsInJS(std::string_view source, |
Henrique Ferreiro | 376c765 | 2020-05-22 09:00:04 | [diff] [blame] | 55 | const TemplateReplacements& replacements, |
| 56 | std::string* output); |
rbpotter | 953ca77 | 2019-08-09 23:57:12 | [diff] [blame] | 57 | |
dschuyler | 6c937646 | 2015-07-17 03:47:43 | [diff] [blame] | 58 | } // namespace ui |
| 59 | |
| 60 | #endif // UI_BASE_TEMPLATE_EXPRESSIONS_H_ |