OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/webui/jstemplate_builder.h" | 8 #include "ui/webui/jstemplate_builder.h" |
9 | 9 |
10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 UseVersion2::UseVersion2() { | 27 UseVersion2::UseVersion2() { |
28 g_version2++; | 28 g_version2++; |
29 } | 29 } |
30 | 30 |
31 UseVersion2::~UseVersion2() { | 31 UseVersion2::~UseVersion2() { |
32 g_version2--; | 32 g_version2--; |
33 } | 33 } |
34 | 34 |
35 std::string GetTemplateHtml(const base::StringPiece& html_template, | 35 std::string GetTemplateHtml(const base::StringPiece& html_template, |
36 const DictionaryValue* json, | 36 const base::DictionaryValue* json, |
37 const base::StringPiece& template_id) { | 37 const base::StringPiece& template_id) { |
38 std::string output(html_template.data(), html_template.size()); | 38 std::string output(html_template.data(), html_template.size()); |
39 AppendJsonHtml(json, &output); | 39 AppendJsonHtml(json, &output); |
40 AppendJsTemplateSourceHtml(&output); | 40 AppendJsTemplateSourceHtml(&output); |
41 AppendJsTemplateProcessHtml(template_id, &output); | 41 AppendJsTemplateProcessHtml(template_id, &output); |
42 return output; | 42 return output; |
43 } | 43 } |
44 | 44 |
45 std::string GetI18nTemplateHtml(const base::StringPiece& html_template, | 45 std::string GetI18nTemplateHtml(const base::StringPiece& html_template, |
46 const DictionaryValue* json) { | 46 const base::DictionaryValue* json) { |
47 std::string output(html_template.data(), html_template.size()); | 47 std::string output(html_template.data(), html_template.size()); |
48 AppendJsonHtml(json, &output); | 48 AppendJsonHtml(json, &output); |
49 AppendI18nTemplateSourceHtml(&output); | 49 AppendI18nTemplateSourceHtml(&output); |
50 AppendI18nTemplateProcessHtml(&output); | 50 AppendI18nTemplateProcessHtml(&output); |
51 return output; | 51 return output; |
52 } | 52 } |
53 | 53 |
54 std::string GetTemplatesHtml(const base::StringPiece& html_template, | 54 std::string GetTemplatesHtml(const base::StringPiece& html_template, |
55 const DictionaryValue* json, | 55 const base::DictionaryValue* json, |
56 const base::StringPiece& template_id) { | 56 const base::StringPiece& template_id) { |
57 std::string output(html_template.data(), html_template.size()); | 57 std::string output(html_template.data(), html_template.size()); |
58 AppendI18nTemplateSourceHtml(&output); | 58 AppendI18nTemplateSourceHtml(&output); |
59 AppendJsTemplateSourceHtml(&output); | 59 AppendJsTemplateSourceHtml(&output); |
60 AppendJsonHtml(json, &output); | 60 AppendJsonHtml(json, &output); |
61 AppendI18nTemplateProcessHtml(&output); | 61 AppendI18nTemplateProcessHtml(&output); |
62 AppendJsTemplateProcessHtml(template_id, &output); | 62 AppendJsTemplateProcessHtml(template_id, &output); |
63 return output; | 63 return output; |
64 } | 64 } |
65 | 65 |
66 void AppendJsonHtml(const DictionaryValue* json, std::string* output) { | 66 void AppendJsonHtml(const base::DictionaryValue* json, std::string* output) { |
67 std::string javascript_string; | 67 std::string javascript_string; |
68 AppendJsonJS(json, &javascript_string); | 68 AppendJsonJS(json, &javascript_string); |
69 | 69 |
70 // </ confuses the HTML parser because it could be a </script> tag. So we | 70 // </ confuses the HTML parser because it could be a </script> tag. So we |
71 // replace </ with <\/. The extra \ will be ignored by the JS engine. | 71 // replace </ with <\/. The extra \ will be ignored by the JS engine. |
72 ReplaceSubstringsAfterOffset(&javascript_string, 0, "</", "<\\/"); | 72 ReplaceSubstringsAfterOffset(&javascript_string, 0, "</", "<\\/"); |
73 | 73 |
74 output->append("<script>"); | 74 output->append("<script>"); |
75 output->append(javascript_string); | 75 output->append(javascript_string); |
76 output->append("</script>"); | 76 output->append("</script>"); |
77 } | 77 } |
78 | 78 |
79 void AppendJsonJS(const DictionaryValue* json, std::string* output) { | 79 void AppendJsonJS(const base::DictionaryValue* json, std::string* output) { |
80 // Convert the template data to a json string. | 80 // Convert the template data to a json string. |
81 DCHECK(json) << "must include json data structure"; | 81 DCHECK(json) << "must include json data structure"; |
82 | 82 |
83 std::string jstext; | 83 std::string jstext; |
84 JSONStringValueSerializer serializer(&jstext); | 84 JSONStringValueSerializer serializer(&jstext); |
85 serializer.Serialize(*json); | 85 serializer.Serialize(*json); |
86 output->append(g_version2 ? "loadTimeData.data = " : "var templateData = "); | 86 output->append(g_version2 ? "loadTimeData.data = " : "var templateData = "); |
87 output->append(jstext); | 87 output->append(jstext); |
88 output->append(";"); | 88 output->append(";"); |
89 } | 89 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 NOTREACHED() << "Unable to get i18n process src"; | 147 NOTREACHED() << "Unable to get i18n process src"; |
148 return; | 148 return; |
149 } | 149 } |
150 | 150 |
151 output->append("<script>"); | 151 output->append("<script>"); |
152 output->append(i18n_process_src.data(), i18n_process_src.size()); | 152 output->append(i18n_process_src.data(), i18n_process_src.size()); |
153 output->append("</script>"); | 153 output->append("</script>"); |
154 } | 154 } |
155 | 155 |
156 } // namespace webui | 156 } // namespace webui |
OLD | NEW |