dschuyler | 6c937646 | 2015-07-17 03:47:43 | [diff] [blame] | 1 | // 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 | |
dschuyler | fe6f590 | 2017-01-05 01:10:08 | [diff] [blame] | 17 | namespace base { |
| 18 | class DictionaryValue; |
| 19 | } |
| 20 | |
dschuyler | 6c937646 | 2015-07-17 03:47:43 | [diff] [blame] | 21 | namespace ui { |
| 22 | |
dschuyler | 119857a | 2016-01-29 01:22:20 | [diff] [blame] | 23 | // Map of strings for template replacement in |ReplaceTemplateExpressions|. |
| 24 | typedef std::map<const std::string, std::string> TemplateReplacements; |
| 25 | |
dschuyler | fe6f590 | 2017-01-05 01:10:08 | [diff] [blame] | 26 | // 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. |
| 29 | UI_BASE_EXPORT void TemplateReplacementsFromDictionaryValue( |
| 30 | const base::DictionaryValue& dictionary, |
| 31 | TemplateReplacements* replacements); |
| 32 | |
dschuyler | 342667b | 2016-04-05 17:58:40 | [diff] [blame] | 33 | // 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] | 34 | // |replacements|. If the key is not found in the |replacements| that item will |
dschuyler | 6c937646 | 2015-07-17 03:47:43 | [diff] [blame] | 35 | // be unaltered. |
| 36 | UI_BASE_EXPORT std::string ReplaceTemplateExpressions( |
dschuyler | 342667b | 2016-04-05 17:58:40 | [diff] [blame] | 37 | base::StringPiece source, |
| 38 | const TemplateReplacements& replacements); |
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` ... `, |
| 53 | UI_BASE_EXPORT bool ReplaceTemplateExpressionsInJS( |
| 54 | base::StringPiece source, |
| 55 | const TemplateReplacements& replacements, |
| 56 | std::string* output); |
| 57 | |
dschuyler | 6c937646 | 2015-07-17 03:47:43 | [diff] [blame] | 58 | } // namespace ui |
| 59 | |
| 60 | #endif // UI_BASE_TEMPLATE_EXPRESSIONS_H_ |