diff options
author | arv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-30 00:41:37 +0000 |
---|---|---|
committer | arv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-30 00:41:37 +0000 |
commit | a02afd36072896ba584f32664cc8e4bc1889bae0 (patch) | |
tree | d202bf35af23f91f629df2680d3e2af3218f9b8d /tools | |
parent | e030e767b4f0fef8ef1de21ebe977c2a4dec840b (diff) | |
download | chromium_src-a02afd36072896ba584f32664cc8e4bc1889bae0.zip chromium_src-a02afd36072896ba584f32664cc8e4bc1889bae0.tar.gz chromium_src-a02afd36072896ba584f32664cc8e4bc1889bae0.tar.bz2 |
NTP - Refactor the most visited code to uncouple it from the rest of the NTP.
The goal of this refactoring is to allow splitting the different parts of the NTP into different reusable components.
BUG=None
TEST=Manually
Review URL: http://codereview.chromium.org/1695022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46021 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/grit/grit/format/html_inline.py | 61 |
1 files changed, 45 insertions, 16 deletions
diff --git a/tools/grit/grit/format/html_inline.py b/tools/grit/grit/format/html_inline.py index 625ef26..8be2e81 100755 --- a/tools/grit/grit/format/html_inline.py +++ b/tools/grit/grit/format/html_inline.py @@ -86,30 +86,60 @@ def InlineFile(input_filename, output_filename): if len(distribution) > 1 and distribution[0] == '_': distribution = distribution[1:].lower() - def SrcReplace(src_match): + def SrcReplace(src_match, filepath=input_filepath): """Helper function to provide SrcInlineAsDataURL with the base file path""" - return SrcInlineAsDataURL(src_match, input_filepath, distribution) + return SrcInlineAsDataURL(src_match, filepath, distribution) - def InlineFileContents(src_match, pattern): - """Helper function to inline external script and css files""" + def GetFilepath(src_match): filename = src_match.group('filename') if filename.find(':') != -1: # filename is probably a URL, which we don't want to bother inlining - return src_match.group(0) + return None filename = filename.replace('%DISTRIBUTION%', distribution) - filepath = os.path.join(input_filepath, filename) + return os.path.join(input_filepath, filename) + def InlineFileContents(src_match, pattern): + """Helper function to inline external script and css files""" + filepath = GetFilepath(src_match) + if filepath is None: + return src_match.group(0) return pattern % ReadFile(filepath) def InlineScript(src_match): """Helper function to inline external script files""" return InlineFileContents(src_match, '<script>%s</script>') - def InlineCss(src_match): - """Helper function to inline external css files""" - return InlineFileContents(src_match, '<style>%s</style>') + def InlineCssText(text, css_filepath): + """Helper function that inlines external resources in CSS text""" + filepath = os.path.dirname(css_filepath) + return InlineCssBackgroundImages(text, filepath) + + def InlineCssFile(src_match): + """Helper function to inline external css files. + + Args: + src_match: A regular expression match with a named group named "filename". + + Returns: + The text that should replace the reference to the CSS file. + """ + filepath = GetFilepath(src_match) + if filepath is None: + return src_match.group(0) + + # When resolving CSS files we need to pass in the path so that relative URLs + # can be resolved. + return '<style>%s</style>' % InlineCssText(ReadFile(filepath), filepath) + + def InlineCssBackgroundImages(text, filepath=input_filepath): + """Helper function that inlines external images in CSS backgrounds.""" + return re.sub('background(?:-image)?:[ ]*url\((?:\'|\")' + + '(?P<filename>[^"\'\)\(]*)(?:\'|\")', + lambda m: SrcReplace(m, filepath), + text) + # We need to inline css and js before we inline images so that image # references gets inlined in the css and js @@ -117,19 +147,18 @@ def InlineFile(input_filename, output_filename): InlineScript, ReadFile(input_filename)) - flat_text = re.sub('<link rel="stylesheet".+?href="(?P<filename>[^"\']*)".*?>', - InlineCss, - flat_text) + flat_text = re.sub( + '<link rel="stylesheet".+?href="(?P<filename>[^"\']*)".*?>', + InlineCssFile, + flat_text) # TODO(glen): Make this regex not match src="" text that is not inside a tag flat_text = re.sub('src="(?P<filename>[^"\']*)"', SrcReplace, flat_text) - # TODO(glen): Make this regex not match url('') that is not inside a style - flat_text = re.sub('background(?:-image)?:[ ]*url\(\'(?P<filename>[^"\']*)\'', - SrcReplace, - flat_text) + # TODO(arv): Only do this inside <style> tags. + flat_text = InlineCssBackgroundImages(flat_text) flat_text = re.sub('<link rel="icon".+?href="(?P<filename>[^"\']*)"', SrcReplace, |