summaryrefslogtreecommitdiffstats
path: root/chrome/common/jstemplate_builder.cc
blob: 628edd7f6844937065cee4d69d662b8010a8079d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// A helper function for using JsTemplate. See jstemplate_builder.h for more
// info.

#include "chrome/common/jstemplate_builder.h"

#include "app/resource_bundle.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "chrome/common/json_value_serializer.h"

#include "grit/common_resources.h"

namespace jstemplate_builder {

std::string GetTemplateHtml(const base::StringPiece& html_template,
                            const DictionaryValue* json,
                            const base::StringPiece& template_id) {
  std::string output(html_template.data(), html_template.size());
  AppendJsonHtml(json, &output);
  AppendJsTemplateSourceHtml(&output);
  AppendJsTemplateProcessHtml(template_id, &output);
  return output;
}

std::string GetI18nTemplateHtml(const base::StringPiece& html_template,
                                const DictionaryValue* json) {
  std::string output(html_template.data(), html_template.size());
  AppendJsonHtml(json, &output);
  AppendI18nTemplateSourceHtml(&output);
  AppendI18nTemplateProcessHtml(&output);
  return output;
}

std::string GetTemplatesHtml(const base::StringPiece& html_template,
                             const DictionaryValue* json,
                             const base::StringPiece& template_id) {
  std::string output(html_template.data(), html_template.size());
  AppendI18nTemplateSourceHtml(&output);
  AppendJsTemplateSourceHtml(&output);
  AppendJsonHtml(json, &output);
  AppendI18nTemplateProcessHtml(&output);
  AppendJsTemplateProcessHtml(template_id, &output);
  return output;
}

void AppendJsonHtml(const DictionaryValue* json, std::string* output) {
  // Convert the template data to a json string.
  DCHECK(json) << "must include json data structure";

  std::string jstext;
  JSONStringValueSerializer serializer(&jstext);
  serializer.Serialize(*json);
  // </ confuses the HTML parser because it could be a </script> tag.  So we
  // replace </ with <\/.  The extra \ will be ignored by the JS engine.
  ReplaceSubstringsAfterOffset(&jstext, 0, "</", "<\\/");

  output->append("<script>");
  output->append("var templateData = ");
  output->append(jstext);
  output->append(";");
  output->append("</script>");
}

void AppendJsTemplateSourceHtml(std::string* output) {
  // fetch and cache the pointer of the jstemplate resource source text.
  static const base::StringPiece jstemplate_src(
      ResourceBundle::GetSharedInstance().GetRawDataResource(
          IDR_JSTEMPLATE_JS));

  if (jstemplate_src.empty()) {
    NOTREACHED() << "Unable to get jstemplate src";
    return;
  }

  output->append("<script>");
  output->append(jstemplate_src.data(), jstemplate_src.size());
  output->append("</script>");
}

void AppendJsTemplateProcessHtml(const base::StringPiece& template_id,
                                 std::string* output) {
  output->append("<script>");
  output->append("var tp = document.getElementById('");
  output->append(template_id.data(), template_id.size());
  output->append("');");
  output->append("jstProcess(new JsEvalContext(templateData), tp);");
  output->append("</script>");
}

void AppendI18nTemplateSourceHtml(std::string* output) {
  // fetch and cache the pointer of the jstemplate resource source text.
  static const base::StringPiece i18n_template_src(
      ResourceBundle::GetSharedInstance().GetRawDataResource(
          IDR_I18N_TEMPLATE_JS));

  if (i18n_template_src.empty()) {
    NOTREACHED() << "Unable to get i18n template src";
    return;
  }

  output->append("<script>");
  output->append(i18n_template_src.data(), i18n_template_src.size());
  output->append("</script>");
}

void AppendI18nTemplateProcessHtml(std::string* output) {
  output->append("<script>");
  output->append("i18nTemplate.process(document, templateData);");
  output->append("</script>");
}

}  // namespace jstemplate_builder