| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // A helper function for using JsTemplate. See jstemplate_builder.h for more | 5 // A helper function for using JsTemplate. See jstemplate_builder.h for more |
| 6 // info. | 6 // info. |
| 7 | 7 |
| 8 #include "ui/base/webui/jstemplate_builder.h" | 8 #include "ui/base/webui/jstemplate_builder.h" |
| 9 | 9 |
| 10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| 11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "ui/base/layout.h" | 14 #include "ui/base/layout.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/base/template_expressions.h" |
| 16 #include "ui/resources/grit/webui_resources.h" | 17 #include "ui/resources/grit/webui_resources.h" |
| 17 | 18 |
| 18 namespace webui { | 19 namespace webui { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // Appends a script tag with a variable name |templateData| that has the JSON | 23 // Appends a script tag with a variable name |templateData| that has the JSON |
| 23 // assigned to it. | 24 // assigned to it. |
| 24 void AppendJsonHtml(const base::DictionaryValue* json, std::string* output) { | 25 void AppendJsonHtml(const base::DictionaryValue* json, std::string* output) { |
| 25 std::string javascript_string; | 26 std::string javascript_string; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 | 95 |
| 95 output->append("<script>"); | 96 output->append("<script>"); |
| 96 i18n_template_src.AppendToString(output); | 97 i18n_template_src.AppendToString(output); |
| 97 output->append("</script>"); | 98 output->append("</script>"); |
| 98 } | 99 } |
| 99 | 100 |
| 100 } // namespace | 101 } // namespace |
| 101 | 102 |
| 102 std::string GetI18nTemplateHtml(const base::StringPiece& html_template, | 103 std::string GetI18nTemplateHtml(const base::StringPiece& html_template, |
| 103 const base::DictionaryValue* json) { | 104 const base::DictionaryValue* json) { |
| 104 std::string output(html_template.data(), html_template.size()); | 105 ui::TemplateReplacements replacements; |
| 106 ui::TemplateReplacementsFromDictionaryValue(*json, &replacements); |
| 107 std::string output = |
| 108 ui::ReplaceTemplateExpressions(html_template, replacements); |
| 109 |
| 110 // TODO(dschuyler): After the i18n-content and i18n-values are replaced with |
| 111 // $i18n{} replacements, we will be able to return output at this point. |
| 112 // Remove Append*() lines that builds up the i18n replacement work to be done |
| 113 // in JavaScript. |
| 105 AppendLoadTimeData(&output); | 114 AppendLoadTimeData(&output); |
| 106 AppendJsonHtml(json, &output); | 115 AppendJsonHtml(json, &output); |
| 107 AppendI18nTemplateSourceHtml(&output); | 116 AppendI18nTemplateSourceHtml(&output); |
| 117 |
| 108 return output; | 118 return output; |
| 109 } | 119 } |
| 110 | 120 |
| 111 std::string GetTemplatesHtml(const base::StringPiece& html_template, | 121 std::string GetTemplatesHtml(const base::StringPiece& html_template, |
| 112 const base::DictionaryValue* json, | 122 const base::DictionaryValue* json, |
| 113 const base::StringPiece& template_id) { | 123 const base::StringPiece& template_id) { |
| 114 std::string output(html_template.data(), html_template.size()); | 124 std::string output(html_template.data(), html_template.size()); |
| 115 AppendLoadTimeData(&output); | 125 AppendLoadTimeData(&output); |
| 116 AppendJsonHtml(json, &output); | 126 AppendJsonHtml(json, &output); |
| 117 AppendI18nTemplateSourceHtml(&output); | 127 AppendI18nTemplateSourceHtml(&output); |
| 118 AppendJsTemplateSourceHtml(&output); | 128 AppendJsTemplateSourceHtml(&output); |
| 119 AppendJsTemplateProcessHtml(template_id, &output); | 129 AppendJsTemplateProcessHtml(template_id, &output); |
| 120 return output; | 130 return output; |
| 121 } | 131 } |
| 122 | 132 |
| 123 void AppendJsonJS(const base::DictionaryValue* json, std::string* output) { | 133 void AppendJsonJS(const base::DictionaryValue* json, std::string* output) { |
| 124 // Convert the template data to a json string. | 134 // Convert the template data to a json string. |
| 125 DCHECK(json) << "must include json data structure"; | 135 DCHECK(json) << "must include json data structure"; |
| 126 | 136 |
| 127 std::string jstext; | 137 std::string jstext; |
| 128 JSONStringValueSerializer serializer(&jstext); | 138 JSONStringValueSerializer serializer(&jstext); |
| 129 serializer.Serialize(*json); | 139 serializer.Serialize(*json); |
| 130 output->append("loadTimeData.data = "); | 140 output->append("loadTimeData.data = "); |
| 131 output->append(jstext); | 141 output->append(jstext); |
| 132 output->append(";"); | 142 output->append(";"); |
| 133 } | 143 } |
| 134 | 144 |
| 135 } // namespace webui | 145 } // namespace webui |
| OLD | NEW |