Skip to content
Browse files
Comment out <base> elements when serving from auto
  • Loading branch information
da2x committed Aug 5, 2015
1 parent 2244cca commit 90f2992dcb0f1b1c41a52b9ccdc4c1be86c76dc2
Showing 1 changed file with 18 additions and 6 deletions.
@@ -301,21 +301,33 @@ def serve_static(self, environ, start_response):
elif os.path.isfile(f_path):
with open(f_path, 'rb') as fd:
start_response('200 OK', [('Content-type', mimetype)])
return [self.inject_js(mimetype,]
return [self.file_filter(mimetype,]
elif p_uri.path == '/livereload.js':
with open(LRJS_PATH, 'rb') as fd:
start_response('200 OK', [('Content-type', mimetype)])
return [self.inject_js(mimetype,]
return [self.file_filter(mimetype,]
start_response('404 ERR', [])
return [self.inject_js('text/html', ERROR_N.format(404).format(uri).encode('utf-8'))]
return [self.file_filter('text/html', ERROR_N.format(404).format(uri).encode('utf-8'))]

def inject_js(self, mimetype, data):
"""Inject livereload.js in HTML files."""
"""Apply necessary changes to document before serving."""
def file_filter(self, mimetype, data):
if mimetype == 'text/html':
data = re.sub('</head>', self.snippet, data.decode('utf8'), 1, re.IGNORECASE)
data = data.decode('utf8')
data = self.remove_base_tag(data)
data = self.inject_js(data)
data = data.encode('utf8')
return data

"""Inject livereload.js."""
def inject_js(self, data):
data = re.sub('</head>', self.snippet, data, 1, re.IGNORECASE)
return data

"""Comment out any <base> to allow local resolution of relative URLs."""
def remove_base_tag(self, data):
data = re.sub(r'<base\s([^>]*)>', '<!--base \g<1>-->', data, re.IGNORECASE)
return data

pending = []

0 comments on commit 90f2992

Please sign in to comment.