From: Mark Henderson Date: Fri, 26 Feb 2016 15:46:40 +0000 (-0500) Subject: Moved the rendering of CSS and JS assets to their own instance methods X-Git-Tag: v4.5.0~34^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ad43811a61cc0e11239bbca62ba350dfe433a22;p=thirdparty%2Ftornado.git Moved the rendering of CSS and JS assets to their own instance methods --- diff --git a/tornado/web.py b/tornado/web.py old mode 100644 new mode 100755 index 8826c62b0..504becad9 --- a/tornado/web.py +++ b/tornado/web.py @@ -731,45 +731,21 @@ class RequestHandler(object): if body_part: html_bodies.append(utf8(body_part)) - def is_absolute(path): - return any(path.startswith(x) for x in ["/", "http:", "https:"]) if js_files: # Maintain order of JavaScript files given by modules - paths = [] - unique_paths = set() - for path in js_files: - if not is_absolute(path): - path = self.static_url(path) - if path not in unique_paths: - paths.append(path) - unique_paths.add(path) - js = ''.join('' - for p in paths) + js = self.render_linked_js(js_files) sloc = html.rindex(b'') html = html[:sloc] + utf8(js) + b'\n' + html[sloc:] if js_embed: - js = b'' + js = self.render_embed_js(js_embed) sloc = html.rindex(b'') html = html[:sloc] + js + b'\n' + html[sloc:] if css_files: - paths = [] - unique_paths = set() - for path in css_files: - if not is_absolute(path): - path = self.static_url(path) - if path not in unique_paths: - paths.append(path) - unique_paths.add(path) - css = ''.join('' - for p in paths) + css = self.render_linked_css(css_files) hloc = html.index(b'') html = html[:hloc] + utf8(css) + b'\n' + html[hloc:] if css_embed: - css = b'' + css = self.render_embed_css(css_embed) hloc = html.index(b'') html = html[:hloc] + css + b'\n' + html[hloc:] if html_heads: @@ -780,6 +756,64 @@ class RequestHandler(object): html = html[:hloc] + b''.join(html_bodies) + b'\n' + html[hloc:] self.finish(html) + def render_linked_js(self, js_files): + """Default method used to render the final js links for the + rendered webpage. + + Override this method in a sub-classed controller to change the output. + """ + paths = [] + unique_paths = set() + + for path in js_files: + if not is_absolute(path): + path = self.static_url(path) + if path not in unique_paths: + paths.append(path) + unique_paths.add(path) + + return ''.join('' + for p in paths) + + def render_embed_js(self, js_embed): + """Default method used to render the final embedded js for the + rendered webpage. + + Override this method in a sub-classed controller to change the output. + """ + return b'' + + def render_linked_css(self, css_files): + """Default method used to render the final css links for the + rendered webpage. + + Override this method in a sub-classed controller to change the output. + """ + paths = [] + unique_paths = set() + + for path in css_files: + if not is_absolute(path): + path = self.static_url(path) + if path not in unique_paths: + paths.append(path) + unique_paths.add(path) + + return ''.join('' + for p in paths) + + def render_embed_css(self, css_embed): + """Default method used to render the final embedded css for the + rendered webpage. + + Override this method in a sub-classed controller to change the output. + """ + return b'' + def render_string(self, template_name, **kwargs): """Generate the given template with the given arguments. @@ -3285,3 +3319,6 @@ def _unquote_or_none(s): if s is None: return s return escape.url_unescape(s, encoding=None, plus=False) + +def is_absolute(path): + return any(path.startswith(x) for x in ["/", "http:", "https:"])