2 # Copyright (c) 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
13 srcdir = os.path.abspath(os.path.join(os.path.dirname(__file__),
14 "..", "..", "..", "src"))
16 html_warning_message = """
20 WARNING: This file is auto generated.
26 js_warning_message = """
27 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
28 // Use of this source code is governed by a BSD-style license that can be
29 // found in the LICENSE file.
31 /* WARNING: This file is auto generated.
33 * Do not edit directly.
37 css_warning_message = """
38 /* Copyright (c) 2014 The Chromium Authors. All rights reserved.
39 * Use of this source code is governed by a BSD-style license that can be
40 * found in the LICENSE file. */
42 /* WARNING: This file is auto-generated.
44 * Do not edit directly.
48 def generate_css(load_sequence):
49 style_sheet_chunks = [css_warning_message, '\n']
50 for module in load_sequence:
51 for style_sheet in module.style_sheets:
52 style_sheet_chunks.append(style_sheet.contents_with_inlined_images)
53 style_sheet_chunks.append('\n')
54 return ''.join(style_sheet_chunks)
56 def generate_js(load_sequence, include_html_templates=True):
57 js_chunks = [js_warning_message, '\n']
58 js_chunks.append("window.FLATTENED = {};\n")
59 js_chunks.append("window.FLATTENED_RAW_SCRIPTS = {};\n")
61 for module in load_sequence:
62 for dependent_raw_scripts in module.dependent_raw_scripts:
63 js_chunks.append("window.FLATTENED_RAW_SCRIPTS['%s'] = true;\n" %
64 dependent_raw_script.resource.unix_style_relative_path)
65 js_chunks.append( "window.FLATTENED['%s'] = true;\n" % module.name)
67 if include_html_templates:
68 html_encoded = base64.b64encode(
69 generate_html_for_combined_templates(load_sequence))
70 js_chunks.append("var templateData_ = window.atob('" +
71 html_encoded + "');\n");
72 js_chunks.append("var templateElem_ = document.createElement('div');\n");
73 js_chunks.append("templateElem_.innerHTML = templateData_;\n");
74 js_chunks.append("while (templateElem_.hasChildNodes()) {\n");
75 js_chunks.append(" document.head.appendChild(" +
76 "templateElem_.removeChild(templateElem_.firstChild));\n");
77 js_chunks.append("}\n\n");
79 for module in load_sequence:
80 for dependent_raw_script in module.dependent_raw_scripts:
81 js_chunks.append(dependent_raw_script.contents)
82 js_chunks.append('\n')
83 js_chunks.append(module.contents)
84 js_chunks.append("\n")
86 return ''.join(js_chunks)
88 def generate_deps_js(load_sequence, project):
89 chunks = [js_warning_message, '\n']
90 loader = load_sequence[0].loader
91 for module in loader.loaded_modules.values():
92 chunks.append("tvcm.setResourceFileName('%s','%s');\n" % (
93 module.name, module.resource.relative_path))
95 for module in load_sequence:
96 for dependent_module in module.dependent_modules:
97 chunks.append("tvcm.addModuleDependency('%s','%s');\n" % (
98 module.name, dependent_module.name));
100 for dependent_raw_script in module.dependent_raw_scripts:
101 relative_path = dependent_raw_script.resource.relative_path
103 "tvcm.addModuleRawScriptDependency('%s','%s');\n" % (
104 module.name, relative_path));
106 for style_sheet in module.style_sheets:
107 chunks.append("tvcm.addModuleStylesheet('%s','%s');\n" % (
108 module.name, style_sheet.name));
109 return "".join(chunks)
111 def generate_html_for_combined_templates(load_sequence):
113 for module in load_sequence:
114 for html_template in module.html_templates:
115 chunks.append(html_template.contents)
116 return "\n".join(chunks)
118 class ExtraScript(object):
119 def __init__(self, script_id, text_content, content_type=None):
120 if script_id != None:
121 assert script_id[0] != '#'
122 assert isinstance(text_content, basestring)
123 self.script_id = script_id
124 self.text_content = text_content
125 self.content_type = content_type
127 def generate_standalone_html_file(load_sequence,
129 flattened_js_url=None,
131 extra_scripts = extra_scripts or []
133 head_html_chunks = []
134 head_html_chunks.append("<style>")
135 head_html_chunks.append(generate_css(load_sequence))
136 head_html_chunks.append("</style>")
137 head_html_chunks.append(generate_html_for_combined_templates(load_sequence))
139 head_html_chunks.append('<script src="%s"></script>' % flattened_js_url)
141 head_html_chunks.append('<script>')
142 head_html_chunks.append(generate_js(load_sequence,
143 include_html_templates=False))
144 head_html_chunks.append('</script>')
146 for extra_script in extra_scripts:
148 if extra_script.script_id:
149 attrs.append('id="%s"' % extra_script.script_id)
150 if extra_script.content_type:
151 attrs.append('content-type="%s"' % extra_script.content_type)
153 head_html_chunks.append('<script %s>' % ' '.join(attrs))
155 head_html_chunks.append('<script>')
156 head_html_chunks.append(extra_script.text_content)
157 head_html_chunks.append('</script>')
163 <head i18n-values="dir:textdirection;">
170 """ % (html_warning_message,
172 '\n'.join(head_html_chunks))