1. Olemis Lang
  2. trac-mq

Commits

Olemis Lang  committed 37cde1d

Trac : Introduce expand_chrome_uri function. Do not modify network-path references. Refs #11277

  • Participants
  • Parent commits d2313d7
  • Branches trac_t11277

Comments (0)

Files changed (2)

File series

View file
+t11277/t11277_r11954_network_path_uri.diff
 # Placed by Bitbucket

File t11277/t11277_r11954_network_path_uri.diff

View file
+# HG changeset patch
+# Parent a64bab1558a36f3c54efb7720315e9d7738e1c72
+Trac: Network-path URI
+
+diff -r a64bab1558a3 trac/web/chrome.py
+--- a/trac/web/chrome.py	Fri Aug 16 16:54:39 2013 +0000
++++ b/trac/web/chrome.py	Sun Aug 18 14:23:26 2013 -0500
+@@ -131,6 +131,25 @@
+     links.setdefault(rel, []).append(link)
+     linkset.add(linkid)
+ 
++def expand_chrome_uri(req, filename):
++    """Expand an URI reference before inserting it in e.g. HTML web page
++
++    If the filename is a network-path reference (i.e. starts with two slashs),
++    the link will not be modified (see #11277).
++    If the filename is absolute (i.e. starts with a slash), the generated link
++    will be based off the application root path. If it is relative, the link
++    will be based off the `/chrome/` path.
++    """
++    if filename.startswith(('http://', 'https://', '//')):
++        return filename
++    elif filename.startswith('common/') and 'htdocs_location' in req.chrome:
++        return Href(req.chrome['htdocs_location'])(filename[7:])
++    else:
++        href = req.href
++        if not filename.startswith('/'):
++            href = href.chrome
++        return href(filename)
++
+ def add_stylesheet(req, filename, mimetype='text/css', media=None):
+     """Add a link to a style sheet to the chrome info so that it gets included
+     in the generated HTML page.
+@@ -139,15 +158,7 @@
+     will be based off the application root path. If it is relative, the link
+     will be based off the `/chrome/` path.
+     """
+-    if filename.startswith(('http://', 'https://')):
+-        href = filename
+-    elif filename.startswith('common/') and 'htdocs_location' in req.chrome:
+-        href = Href(req.chrome['htdocs_location'])(filename[7:])
+-    else:
+-        href = req.href
+-        if not filename.startswith('/'):
+-            href = href.chrome
+-        href = href(filename)
++    href = expand_chrome_uri(req, filename)
+     add_link(req, 'stylesheet', href, mimetype=mimetype, media=media)
+ 
+ def add_script(req, filename, mimetype='text/javascript', charset='utf-8',
+@@ -162,15 +173,7 @@
+     if filename in scriptset:
+         return False # Already added that script
+ 
+-    if filename.startswith(('http://', 'https://')):
+-        href = filename
+-    elif filename.startswith('common/') and 'htdocs_location' in req.chrome:
+-        href = Href(req.chrome['htdocs_location'])(filename[7:])
+-    else:
+-        href = req.href
+-        if not filename.startswith('/'):
+-            href = href.chrome
+-        href = href(filename)
++    href = expand_chrome_uri(req, filename)
+     script = {'href': href, 'type': mimetype, 'charset': charset,
+               'prefix': Markup('<!--[if %s]>' % ie_if) if ie_if else None,
+               'suffix': Markup('<![endif]-->') if ie_if else None}