| 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 "app/resource_bundle.h" | 10 #include "app/resource_bundle.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "chrome/common/json_value_serializer.h" | 13 #include "chrome/common/json_value_serializer.h" |
| 14 | 14 |
| 15 #include "grit/common_resources.h" | 15 #include "grit/common_resources.h" |
| 16 | 16 |
| 17 namespace jstemplate_builder { | 17 namespace jstemplate_builder { |
| 18 | 18 |
| 19 std::string GetTemplateHtml(const StringPiece& html_template, | 19 std::string GetTemplateHtml(const base::StringPiece& html_template, |
| 20 const DictionaryValue* json, | 20 const DictionaryValue* json, |
| 21 const StringPiece& template_id) { | 21 const base::StringPiece& template_id) { |
| 22 std::string output(html_template.data(), html_template.size()); | 22 std::string output(html_template.data(), html_template.size()); |
| 23 AppendJsonHtml(json, &output); | 23 AppendJsonHtml(json, &output); |
| 24 AppendJsTemplateSourceHtml(&output); | 24 AppendJsTemplateSourceHtml(&output); |
| 25 AppendJsTemplateProcessHtml(template_id, &output); | 25 AppendJsTemplateProcessHtml(template_id, &output); |
| 26 return output; | 26 return output; |
| 27 } | 27 } |
| 28 | 28 |
| 29 std::string GetI18nTemplateHtml(const StringPiece& html_template, | 29 std::string GetI18nTemplateHtml(const base::StringPiece& html_template, |
| 30 const DictionaryValue* json) { | 30 const DictionaryValue* json) { |
| 31 std::string output(html_template.data(), html_template.size()); | 31 std::string output(html_template.data(), html_template.size()); |
| 32 AppendJsonHtml(json, &output); | 32 AppendJsonHtml(json, &output); |
| 33 AppendI18nTemplateSourceHtml(&output); | 33 AppendI18nTemplateSourceHtml(&output); |
| 34 AppendI18nTemplateProcessHtml(&output); | 34 AppendI18nTemplateProcessHtml(&output); |
| 35 return output; | 35 return output; |
| 36 } | 36 } |
| 37 | 37 |
| 38 std::string GetTemplatesHtml(const StringPiece& html_template, | 38 std::string GetTemplatesHtml(const base::StringPiece& html_template, |
| 39 const DictionaryValue* json, | 39 const DictionaryValue* json, |
| 40 const StringPiece& template_id) { | 40 const base::StringPiece& template_id) { |
| 41 std::string output(html_template.data(), html_template.size()); | 41 std::string output(html_template.data(), html_template.size()); |
| 42 AppendI18nTemplateSourceHtml(&output); | 42 AppendI18nTemplateSourceHtml(&output); |
| 43 AppendJsTemplateSourceHtml(&output); | 43 AppendJsTemplateSourceHtml(&output); |
| 44 AppendJsonHtml(json, &output); | 44 AppendJsonHtml(json, &output); |
| 45 AppendI18nTemplateProcessHtml(&output); | 45 AppendI18nTemplateProcessHtml(&output); |
| 46 AppendJsTemplateProcessHtml(template_id, &output); | 46 AppendJsTemplateProcessHtml(template_id, &output); |
| 47 return output; | 47 return output; |
| 48 } | 48 } |
| 49 | 49 |
| 50 void AppendJsonHtml(const DictionaryValue* json, std::string* output) { | 50 void AppendJsonHtml(const DictionaryValue* json, std::string* output) { |
| 51 // Convert the template data to a json string. | 51 // Convert the template data to a json string. |
| 52 DCHECK(json) << "must include json data structure"; | 52 DCHECK(json) << "must include json data structure"; |
| 53 | 53 |
| 54 std::string jstext; | 54 std::string jstext; |
| 55 JSONStringValueSerializer serializer(&jstext); | 55 JSONStringValueSerializer serializer(&jstext); |
| 56 serializer.Serialize(*json); | 56 serializer.Serialize(*json); |
| 57 // </ confuses the HTML parser because it could be a </script> tag. So we | 57 // </ confuses the HTML parser because it could be a </script> tag. So we |
| 58 // replace </ with <\/. The extra \ will be ignored by the JS engine. | 58 // replace </ with <\/. The extra \ will be ignored by the JS engine. |
| 59 ReplaceSubstringsAfterOffset(&jstext, 0, "</", "<\\/"); | 59 ReplaceSubstringsAfterOffset(&jstext, 0, "</", "<\\/"); |
| 60 | 60 |
| 61 output->append("<script>"); | 61 output->append("<script>"); |
| 62 output->append("var templateData = "); | 62 output->append("var templateData = "); |
| 63 output->append(jstext); | 63 output->append(jstext); |
| 64 output->append(";"); | 64 output->append(";"); |
| 65 output->append("</script>"); | 65 output->append("</script>"); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void AppendJsTemplateSourceHtml(std::string* output) { | 68 void AppendJsTemplateSourceHtml(std::string* output) { |
| 69 // fetch and cache the pointer of the jstemplate resource source text. | 69 // fetch and cache the pointer of the jstemplate resource source text. |
| 70 static const StringPiece jstemplate_src(ResourceBundle::GetSharedInstance(). | 70 static const base::StringPiece jstemplate_src( |
| 71 GetRawDataResource(IDR_JSTEMPLATE_JS)); | 71 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 72 IDR_JSTEMPLATE_JS)); |
| 72 | 73 |
| 73 if (jstemplate_src.empty()) { | 74 if (jstemplate_src.empty()) { |
| 74 NOTREACHED() << "Unable to get jstemplate src"; | 75 NOTREACHED() << "Unable to get jstemplate src"; |
| 75 return; | 76 return; |
| 76 } | 77 } |
| 77 | 78 |
| 78 output->append("<script>"); | 79 output->append("<script>"); |
| 79 output->append(jstemplate_src.data(), jstemplate_src.size()); | 80 output->append(jstemplate_src.data(), jstemplate_src.size()); |
| 80 output->append("</script>"); | 81 output->append("</script>"); |
| 81 } | 82 } |
| 82 | 83 |
| 83 void AppendJsTemplateProcessHtml(const StringPiece& template_id, | 84 void AppendJsTemplateProcessHtml(const base::StringPiece& template_id, |
| 84 std::string* output) { | 85 std::string* output) { |
| 85 output->append("<script>"); | 86 output->append("<script>"); |
| 86 output->append("var tp = document.getElementById('"); | 87 output->append("var tp = document.getElementById('"); |
| 87 output->append(template_id.data(), template_id.size()); | 88 output->append(template_id.data(), template_id.size()); |
| 88 output->append("');"); | 89 output->append("');"); |
| 89 output->append("jstProcess(new JsEvalContext(templateData), tp);"); | 90 output->append("jstProcess(new JsEvalContext(templateData), tp);"); |
| 90 output->append("</script>"); | 91 output->append("</script>"); |
| 91 } | 92 } |
| 92 | 93 |
| 93 void AppendI18nTemplateSourceHtml(std::string* output) { | 94 void AppendI18nTemplateSourceHtml(std::string* output) { |
| 94 // fetch and cache the pointer of the jstemplate resource source text. | 95 // fetch and cache the pointer of the jstemplate resource source text. |
| 95 static const StringPiece i18n_template_src( | 96 static const base::StringPiece i18n_template_src( |
| 96 ResourceBundle::GetSharedInstance().GetRawDataResource( | 97 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 97 IDR_I18N_TEMPLATE_JS)); | 98 IDR_I18N_TEMPLATE_JS)); |
| 98 | 99 |
| 99 if (i18n_template_src.empty()) { | 100 if (i18n_template_src.empty()) { |
| 100 NOTREACHED() << "Unable to get i18n template src"; | 101 NOTREACHED() << "Unable to get i18n template src"; |
| 101 return; | 102 return; |
| 102 } | 103 } |
| 103 | 104 |
| 104 output->append("<script>"); | 105 output->append("<script>"); |
| 105 output->append(i18n_template_src.data(), i18n_template_src.size()); | 106 output->append(i18n_template_src.data(), i18n_template_src.size()); |
| 106 output->append("</script>"); | 107 output->append("</script>"); |
| 107 } | 108 } |
| 108 | 109 |
| 109 void AppendI18nTemplateProcessHtml(std::string* output) { | 110 void AppendI18nTemplateProcessHtml(std::string* output) { |
| 110 output->append("<script>"); | 111 output->append("<script>"); |
| 111 output->append("i18nTemplate.process(document, "); | 112 output->append("i18nTemplate.process(document, "); |
| 112 output->append("templateData);"); | 113 output->append("templateData);"); |
| 113 output->append("</script>"); | 114 output->append("</script>"); |
| 114 } | 115 } |
| 115 | 116 |
| 116 } // namespace jstemplate_builder | 117 } // namespace jstemplate_builder |
| OLD | NEW |