| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/common/jstemplate_builder.h" | 8 #include "chrome/common/jstemplate_builder.h" |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 std::string output(html_template.data(), html_template.size()); | 40 std::string output(html_template.data(), html_template.size()); |
| 41 AppendI18nTemplateSourceHtml(&output); | 41 AppendI18nTemplateSourceHtml(&output); |
| 42 AppendJsTemplateSourceHtml(&output); | 42 AppendJsTemplateSourceHtml(&output); |
| 43 AppendJsonHtml(json, &output); | 43 AppendJsonHtml(json, &output); |
| 44 AppendI18nTemplateProcessHtml(&output); | 44 AppendI18nTemplateProcessHtml(&output); |
| 45 AppendJsTemplateProcessHtml(template_id, &output); | 45 AppendJsTemplateProcessHtml(template_id, &output); |
| 46 return output; | 46 return output; |
| 47 } | 47 } |
| 48 | 48 |
| 49 void AppendJsonHtml(const DictionaryValue* json, std::string* output) { | 49 void AppendJsonHtml(const DictionaryValue* json, std::string* output) { |
| 50 std::string javascript_string; |
| 51 AppendJsonJS(json, &javascript_string); |
| 52 |
| 53 // </ confuses the HTML parser because it could be a </script> tag. So we |
| 54 // replace </ with <\/. The extra \ will be ignored by the JS engine. |
| 55 ReplaceSubstringsAfterOffset(&javascript_string, 0, "</", "<\\/"); |
| 56 |
| 57 output->append("<script>"); |
| 58 output->append(javascript_string); |
| 59 output->append("</script>"); |
| 60 } |
| 61 |
| 62 void AppendJsonJS(const DictionaryValue* json, std::string* output) { |
| 50 // Convert the template data to a json string. | 63 // Convert the template data to a json string. |
| 51 DCHECK(json) << "must include json data structure"; | 64 DCHECK(json) << "must include json data structure"; |
| 52 | 65 |
| 53 std::string jstext; | 66 std::string jstext; |
| 54 JSONStringValueSerializer serializer(&jstext); | 67 JSONStringValueSerializer serializer(&jstext); |
| 55 serializer.Serialize(*json); | 68 serializer.Serialize(*json); |
| 56 // </ confuses the HTML parser because it could be a </script> tag. So we | |
| 57 // replace </ with <\/. The extra \ will be ignored by the JS engine. | |
| 58 ReplaceSubstringsAfterOffset(&jstext, 0, "</", "<\\/"); | |
| 59 | |
| 60 output->append("<script>"); | |
| 61 output->append("var templateData = "); | 69 output->append("var templateData = "); |
| 62 output->append(jstext); | 70 output->append(jstext); |
| 63 output->append(";"); | 71 output->append(";"); |
| 64 output->append("</script>"); | |
| 65 } | 72 } |
| 66 | 73 |
| 67 void AppendJsTemplateSourceHtml(std::string* output) { | 74 void AppendJsTemplateSourceHtml(std::string* output) { |
| 68 // fetch and cache the pointer of the jstemplate resource source text. | 75 // fetch and cache the pointer of the jstemplate resource source text. |
| 69 static const base::StringPiece jstemplate_src( | 76 static const base::StringPiece jstemplate_src( |
| 70 ResourceBundle::GetSharedInstance().GetRawDataResource( | 77 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 71 IDR_JSTEMPLATE_JS)); | 78 IDR_JSTEMPLATE_JS)); |
| 72 | 79 |
| 73 if (jstemplate_src.empty()) { | 80 if (jstemplate_src.empty()) { |
| 74 NOTREACHED() << "Unable to get jstemplate src"; | 81 NOTREACHED() << "Unable to get jstemplate src"; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 100 NOTREACHED() << "Unable to get i18n template src"; | 107 NOTREACHED() << "Unable to get i18n template src"; |
| 101 return; | 108 return; |
| 102 } | 109 } |
| 103 | 110 |
| 104 output->append("<script>"); | 111 output->append("<script>"); |
| 105 output->append(i18n_template_src.data(), i18n_template_src.size()); | 112 output->append(i18n_template_src.data(), i18n_template_src.size()); |
| 106 output->append("</script>"); | 113 output->append("</script>"); |
| 107 } | 114 } |
| 108 | 115 |
| 109 void AppendI18nTemplateProcessHtml(std::string* output) { | 116 void AppendI18nTemplateProcessHtml(std::string* output) { |
| 117 static const base::StringPiece i18n_process_src( |
| 118 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 119 IDR_I18N_PROCESS_JS)); |
| 120 |
| 121 if (i18n_process_src.empty()) { |
| 122 NOTREACHED() << "Unable to get i18n process src"; |
| 123 return; |
| 124 } |
| 125 |
| 110 output->append("<script>"); | 126 output->append("<script>"); |
| 111 output->append("i18nTemplate.process(document, templateData);"); | 127 output->append(i18n_process_src.data(), i18n_process_src.size()); |
| 112 output->append("</script>"); | 128 output->append("</script>"); |
| 113 } | 129 } |
| 114 | 130 |
| 115 } // namespace jstemplate_builder | 131 } // namespace jstemplate_builder |
| OLD | NEW |