DevTools: kill codeschool extension from whitelist
[chromium-blink-merge.git] / ui / base / webui / jstemplate_builder.cc
blob2aa80a39889b6e5ac39b39bff127a0a604e088c5
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
3 // found in the LICENSE file.
5 // A helper function for using JsTemplate. See jstemplate_builder.h for more
6 // info.
8 #include "ui/base/webui/jstemplate_builder.h"
10 #include "base/json/json_file_value_serializer.h"
11 #include "base/json/json_string_value_serializer.h"
12 #include "base/logging.h"
13 #include "base/strings/string_util.h"
14 #include "ui/base/layout.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/resources/grit/webui_resources.h"
18 namespace webui {
20 namespace {
22 // Appends a script tag with a variable name |templateData| that has the JSON
23 // assigned to it.
24 void AppendJsonHtml(const base::DictionaryValue* json, std::string* output) {
25 std::string javascript_string;
26 AppendJsonJS(json, &javascript_string);
28 // </ confuses the HTML parser because it could be a </script> tag. So we
29 // replace </ with <\/. The extra \ will be ignored by the JS engine.
30 ReplaceSubstringsAfterOffset(&javascript_string, 0, "</", "<\\/");
32 output->append("<script>");
33 output->append(javascript_string);
34 output->append("</script>");
37 // Appends the source for load_time_data.js in a script tag.
38 void AppendLoadTimeData(std::string* output) {
39 // fetch and cache the pointer of the jstemplate resource source text.
40 base::StringPiece load_time_data_src(
41 ResourceBundle::GetSharedInstance().GetRawDataResource(
42 IDR_WEBUI_JS_LOAD_TIME_DATA));
44 if (load_time_data_src.empty()) {
45 NOTREACHED() << "Unable to get loadTimeData src";
46 return;
49 output->append("<script>");
50 load_time_data_src.AppendToString(output);
51 output->append("</script>");
54 // Appends the source for JsTemplates in a script tag.
55 void AppendJsTemplateSourceHtml(std::string* output) {
56 // fetch and cache the pointer of the jstemplate resource source text.
57 base::StringPiece jstemplate_src(
58 ResourceBundle::GetSharedInstance().GetRawDataResource(
59 IDR_WEBUI_JSTEMPLATE_JS));
61 if (jstemplate_src.empty()) {
62 NOTREACHED() << "Unable to get jstemplate src";
63 return;
66 output->append("<script>");
67 jstemplate_src.AppendToString(output);
68 output->append("</script>");
71 // Appends the code that processes the JsTemplate with the JSON. You should
72 // call AppendJsTemplateSourceHtml and AppendJsonHtml before calling this.
73 void AppendJsTemplateProcessHtml(
74 const base::StringPiece& template_id,
75 std::string* output) {
76 output->append("<script>");
77 output->append("var tp = document.getElementById('");
78 output->append(template_id.data(), template_id.size());
79 output->append("');");
80 output->append("jstProcess(loadTimeData.createJsEvalContext(), tp);");
81 output->append("</script>");
84 // Appends the source for i18n Templates in a script tag.
85 void AppendI18nTemplateSourceHtml(std::string* output) {
86 base::StringPiece i18n_template_src(
87 ResourceBundle::GetSharedInstance().GetRawDataResource(
88 IDR_WEBUI_I18N_TEMPLATE_JS));
90 if (i18n_template_src.empty()) {
91 NOTREACHED() << "Unable to get i18n template src";
92 return;
95 output->append("<script>");
96 i18n_template_src.AppendToString(output);
97 output->append("</script>");
100 } // namespace
102 std::string GetI18nTemplateHtml(const base::StringPiece& html_template,
103 const base::DictionaryValue* json) {
104 std::string output(html_template.data(), html_template.size());
105 AppendLoadTimeData(&output);
106 AppendJsonHtml(json, &output);
107 AppendI18nTemplateSourceHtml(&output);
108 return output;
111 std::string GetTemplatesHtml(const base::StringPiece& html_template,
112 const base::DictionaryValue* json,
113 const base::StringPiece& template_id) {
114 std::string output(html_template.data(), html_template.size());
115 AppendLoadTimeData(&output);
116 AppendJsonHtml(json, &output);
117 AppendI18nTemplateSourceHtml(&output);
118 AppendJsTemplateSourceHtml(&output);
119 AppendJsTemplateProcessHtml(template_id, &output);
120 return output;
123 void AppendJsonJS(const base::DictionaryValue* json, std::string* output) {
124 // Convert the template data to a json string.
125 DCHECK(json) << "must include json data structure";
127 std::string jstext;
128 JSONStringValueSerializer serializer(&jstext);
129 serializer.Serialize(*json);
130 output->append("loadTimeData.data = ");
131 output->append(jstext);
132 output->append(";");
135 } // namespace webui