Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Unified Diff: grit/format/html_inline.py

Issue 12261055: Correctly resolve relative paths when inlining @import directives in CSS files. (Closed) Base URL: https://grit-i18n.googlecode.com/svn/trunk
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | grit/format/html_inline_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: grit/format/html_inline.py
diff --git a/grit/format/html_inline.py b/grit/format/html_inline.py
index f1e6bd4dc5f9012197e6df10969c64e1a93e3e6d..6aea5f71a1cc94c2d99a2e7c2f31a619ea0684c8 100755
--- a/grit/format/html_inline.py
+++ b/grit/format/html_inline.py
@@ -74,7 +74,7 @@ def SrcInlineAsDataURL(
return src_match.group(0)
filename = filename.replace(DIST_SUBSTR , distribution)
- filepath = os.path.join(base_path, filename)
+ filepath = os.path.normpath(os.path.join(base_path, filename))
inlined_files.add(filepath)
if names_only:
@@ -129,7 +129,7 @@ def DoInline(
return SrcInlineAsDataURL(
src_match, filepath, distribution, inlined_files, names_only=names_only)
- def GetFilepath(src_match):
+ def GetFilepath(src_match, base_path = input_filepath):
filename = src_match.group('filename')
if filename.find(':') != -1:
@@ -137,7 +137,7 @@ def DoInline(
return None
filename = filename.replace('%DISTRIBUTION%', distribution)
- return os.path.join(input_filepath, filename)
+ return os.path.normpath(os.path.join(base_path, filename))
def IsConditionSatisfied(src_match):
expression = src_match.group('expression')
@@ -217,16 +217,18 @@ def DoInline(
text = InlineCSSImages(text, filepath)
return InlineCSSImports(text, filepath)
- def InlineCSSFile(src_match, inlined_files=inlined_files):
- """Helper function to inline external css files.
+ def InlineCSSFile(src_match, pattern, base_path=input_filepath):
+ """Helper function to inline external CSS files.
Args:
src_match: A regular expression match with a named group named "filename".
+ pattern: The pattern to replace with the contents of the CSS file.
+ base_path: The base path to use for resolving the CSS file.
Returns:
The text that should replace the reference to the CSS file.
"""
- filepath = GetFilepath(src_match)
+ filepath = GetFilepath(src_match, base_path)
if filepath is None:
return src_match.group(0)
@@ -235,7 +237,8 @@ def DoInline(
inlined_files.add(filepath)
# When resolving CSS files we need to pass in the path so that relative URLs
# can be resolved.
- return InlineCSSText(util.ReadFile(filepath, util.BINARY), filepath)
+ return pattern % InlineCSSText(util.ReadFile(filepath, util.BINARY),
+ filepath)
def InlineCSSImages(text, filepath=input_filepath):
"""Helper function that inlines external images in CSS backgrounds."""
@@ -261,8 +264,8 @@ def DoInline(
directive.
"""
return re.sub('@import\s+url\((?P<quote>"|\'|)(?P<filename>[^"\'()]*)' +
- '(?P=quote)\)',
- InlineCSSFile,
+ '(?P=quote)\);',
+ lambda m: InlineCSSFile(m, '%s', filepath),
text)
@@ -284,7 +287,7 @@ def DoInline(
flat_text = re.sub(
'<link rel="stylesheet".+?href="(?P<filename>[^"]*)".*?>',
- lambda m: '<style>%s</style>' % InlineCSSFile(m),
+ lambda m: InlineCSSFile(m, '<style>%s</style>'),
flat_text)
flat_text = re.sub(
@@ -326,10 +329,14 @@ def InlineToString(input_filename, grd_node, allow_external_script=False,
Returns:
the inlined data as a string
"""
- return DoInline(input_filename,
- grd_node,
- allow_external_script=allow_external_script,
- rewrite_function=rewrite_function).inlined_data
+ try:
+ return DoInline(input_filename,
+ grd_node,
+ allow_external_script=allow_external_script,
+ rewrite_function=rewrite_function).inlined_data
+ except IOError, e:
+ raise Exception("Failed to open %s while trying to flatten %s. (%s)" %
+ (e.filename, input_filename, e.strerror))
def InlineToFile(input_filename, output_filename, grd_node):
« no previous file with comments | « no previous file | grit/format/html_inline_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698