| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Prepares a Chrome HTML file by inlining resources and adding references to | 6 """Prepares a Chrome HTML file by inlining resources and adding references to |
| 7 high DPI resources and removing references to unsupported scale factors. | 7 high DPI resources and removing references to unsupported scale factors. |
| 8 | 8 |
| 9 This is a small gatherer that takes a HTML file, looks for src attributes | 9 This is a small gatherer that takes a HTML file, looks for src attributes |
| 10 and inlines the specified file, producing one HTML file with no external | 10 and inlines the specified file, producing one HTML file with no external |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 from grit.gather import interface | 24 from grit.gather import interface |
| 25 | 25 |
| 26 | 26 |
| 27 # Distribution string to replace with distribution. | 27 # Distribution string to replace with distribution. |
| 28 DIST_SUBSTR = '%DISTRIBUTION%' | 28 DIST_SUBSTR = '%DISTRIBUTION%' |
| 29 | 29 |
| 30 | 30 |
| 31 # Matches a chrome theme source URL. | 31 # Matches a chrome theme source URL. |
| 32 _THEME_SOURCE = lazy_re.compile( | 32 _THEME_SOURCE = lazy_re.compile( |
| 33 '(?P<baseurl>chrome://theme/IDR_[A-Z0-9_]*)(?P<query>\?.*)?') | 33 '(?P<baseurl>chrome://theme/IDR_[A-Z0-9_]*)(?P<query>\?.*)?') |
| 34 # Matches CSS image urls with the capture group 'filename'. | 34 # Pattern for matching CSS url() function. |
| 35 _CSS_URL_PATTERN = 'url\((?P<quote>"|\'|)(?P<filename>[^"\'()]*)(?P=quote)\)' |
| 36 # Matches CSS url() functions with the capture group 'filename'. |
| 37 _CSS_URL = lazy_re.compile(_CSS_URL_PATTERN) |
| 38 # Matches one or more CSS image urls used in given properties. |
| 35 _CSS_IMAGE_URLS = lazy_re.compile( | 39 _CSS_IMAGE_URLS = lazy_re.compile( |
| 36 '(?P<attribute>content|background|[\w-]*-image):[ ]*' + | 40 '(?P<attribute>content|background|[\w-]*-image):\s*' + |
| 37 'url\((?P<quote>"|\'|)(?P<filename>[^"\'()]*)(?P=quote)') | 41 '(?P<urls>(' + _CSS_URL_PATTERN + '\s*,?\s*)+)') |
| 38 # Matches CSS image sets. | 42 # Matches CSS image sets. |
| 39 _CSS_IMAGE_SETS = lazy_re.compile( | 43 _CSS_IMAGE_SETS = lazy_re.compile( |
| 40 '(?P<attribute>content|background|[\w-]*-image):[ ]*' + | 44 '(?P<attribute>content|background|[\w-]*-image):[ ]*' + |
| 41 '-webkit-image-set\((?P<images>' + | 45 '-webkit-image-set\((?P<images>' + |
| 42 '([,\r\n ]*url\((?P<quote>"|\'|)[^"\'()]*(?P=quote)\)[ ]*[0-9.]*x)*)\)', | 46 '(\s*,?\s*url\((?P<quote>"|\'|)[^"\'()]*(?P=quote)\)[ ]*[0-9.]*x)*)\)', |
| 43 re.MULTILINE) | 47 re.MULTILINE) |
| 44 # Matches a single image in a CSS image set with the capture group scale. | 48 # Matches a single image in a CSS image set with the capture group scale. |
| 45 _CSS_IMAGE_SET_IMAGE = lazy_re.compile('[,\r\n ]*' + | 49 _CSS_IMAGE_SET_IMAGE = lazy_re.compile('\s*,?\s*' + |
| 46 'url\((?P<quote>"|\'|)[^"\'()]*(?P=quote)\)[ ]*(?P<scale>[0-9.]*x)', | 50 'url\((?P<quote>"|\'|)[^"\'()]*(?P=quote)\)[ ]*(?P<scale>[0-9.]*x)', |
| 47 re.MULTILINE) | 51 re.MULTILINE) |
| 48 _HTML_IMAGE_SRC = lazy_re.compile( | 52 _HTML_IMAGE_SRC = lazy_re.compile( |
| 49 '<img[^>]+src=\"(?P<filename>[^">]*)\"[^>]*>') | 53 '<img[^>]+src=\"(?P<filename>[^">]*)\"[^>]*>') |
| 50 | 54 |
| 51 def GetImageList( | 55 def GetImageList( |
| 52 base_path, filename, scale_factors, distribution, | 56 base_path, filename, scale_factors, distribution, |
| 53 filename_expansion_function=None): | 57 filename_expansion_function=None): |
| 54 """Generate the list of images which match the provided scale factors. | 58 """Generate the list of images which match the provided scale factors. |
| 55 | 59 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 Returns: | 118 Returns: |
| 115 string giving a -webkit-image-set rule referencing the provided images. | 119 string giving a -webkit-image-set rule referencing the provided images. |
| 116 (i.e. '-webkit-image-set(url('image.png') 1x, url('2x/image.png') 2x)') | 120 (i.e. '-webkit-image-set(url('image.png') 1x, url('2x/image.png') 2x)') |
| 117 """ | 121 """ |
| 118 imageset = [] | 122 imageset = [] |
| 119 for (scale_factor, filename) in images: | 123 for (scale_factor, filename) in images: |
| 120 imageset.append("url(%s%s%s) %s" % (quote, filename, quote, scale_factor)) | 124 imageset.append("url(%s%s%s) %s" % (quote, filename, quote, scale_factor)) |
| 121 return "-webkit-image-set(%s)" % (', '.join(imageset)) | 125 return "-webkit-image-set(%s)" % (', '.join(imageset)) |
| 122 | 126 |
| 123 | 127 |
| 124 def InsertImageSet( | 128 def UrlToImageSet( |
| 125 src_match, base_path, scale_factors, distribution, | 129 src_match, base_path, scale_factors, distribution, |
| 126 filename_expansion_function=None): | 130 filename_expansion_function=None): |
| 127 """Regex replace function which inserts -webkit-image-set. | 131 """Regex replace function which replaces url() with -webkit-image-set. |
| 128 | 132 |
| 129 Takes a regex match for url('path'). If the file is local, checks for | 133 Takes a regex match for url('path'). If the file is local, checks for |
| 130 files of the same name in folders corresponding to the supported scale | 134 files of the same name in folders corresponding to the supported scale |
| 131 factors. If the file is from a chrome://theme/ source, inserts the | 135 factors. If the file is from a chrome://theme/ source, inserts the |
| 132 supported @Nx scale factor request. In either case inserts a | 136 supported @Nx scale factor request. In either case inserts a |
| 133 -webkit-image-set rule to fetch the appropriate image for the current | 137 -webkit-image-set rule to fetch the appropriate image for the current |
| 134 scale factor. | 138 scale factor. |
| 135 | 139 |
| 136 Args: | 140 Args: |
| 137 src_match: regex match object from _CSS_IMAGE_URLS | 141 src_match: regex match object from _CSS_URLS |
| 138 base_path: path to look for relative file paths in | 142 base_path: path to look for relative file paths in |
| 139 scale_factors: a list of the supported scale factors (i.e. ['2x']) | 143 scale_factors: a list of the supported scale factors (i.e. ['2x']) |
| 140 distribution: string that should replace %DISTRIBUTION%. | 144 distribution: string that should replace %DISTRIBUTION%. |
| 141 | 145 |
| 142 Returns: | 146 Returns: |
| 143 string | 147 string |
| 144 """ | 148 """ |
| 145 quote = src_match.group('quote') | 149 quote = src_match.group('quote') |
| 146 filename = src_match.group('filename') | 150 filename = src_match.group('filename') |
| 147 attr = src_match.group('attribute') | |
| 148 image_list = GetImageList( | 151 image_list = GetImageList( |
| 149 base_path, filename, scale_factors, distribution, | 152 base_path, filename, scale_factors, distribution, |
| 150 filename_expansion_function=filename_expansion_function) | 153 filename_expansion_function=filename_expansion_function) |
| 151 | 154 |
| 152 # Don't modify the source if there is only one image. | 155 # Don't modify the source if there is only one image. |
| 153 if len(image_list) == 1: | 156 if len(image_list) == 1: |
| 154 return src_match.group(0) | 157 return src_match.group(0) |
| 155 | 158 |
| 156 return "%s: %s" % (attr, GenerateImageSet(image_list, quote)[:-1]) | 159 return GenerateImageSet(image_list, quote) |
| 160 |
| 161 |
| 162 def InsertImageSet( |
| 163 src_match, base_path, scale_factors, distribution, |
| 164 filename_expansion_function=None): |
| 165 """Regex replace function which inserts -webkit-image-set rules. |
| 166 |
| 167 Takes a regex match for `property: url('path')[, url('path')]+`. |
| 168 Replaces one or more occurances of the match with image set rules. |
| 169 |
| 170 Args: |
| 171 src_match: regex match object from _CSS_IMAGE_URLS |
| 172 base_path: path to look for relative file paths in |
| 173 scale_factors: a list of the supported scale factors (i.e. ['2x']) |
| 174 distribution: string that should replace %DISTRIBUTION%. |
| 175 |
| 176 Returns: |
| 177 string |
| 178 """ |
| 179 attr = src_match.group('attribute') |
| 180 urls = _CSS_URL.sub( |
| 181 lambda m: UrlToImageSet(m, base_path, scale_factors, distribution, |
| 182 filename_expansion_function), |
| 183 src_match.group('urls')) |
| 184 |
| 185 return "%s: %s" % (attr, urls) |
| 157 | 186 |
| 158 | 187 |
| 159 def InsertImageStyle( | 188 def InsertImageStyle( |
| 160 src_match, base_path, scale_factors, distribution, | 189 src_match, base_path, scale_factors, distribution, |
| 161 filename_expansion_function=None): | 190 filename_expansion_function=None): |
| 162 """Regex replace function which adds a content style to an <img>. | 191 """Regex replace function which adds a content style to an <img>. |
| 163 | 192 |
| 164 Takes a regex match from _HTML_IMAGE_SRC and replaces the attribute with a CSS | 193 Takes a regex match from _HTML_IMAGE_SRC and replaces the attribute with a CSS |
| 165 style which defines the image set. | 194 style which defines the image set. |
| 166 """ | 195 """ |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 filename_expansion_function=self.filename_expansion_function), | 351 filename_expansion_function=self.filename_expansion_function), |
| 323 filename_expansion_function=self.filename_expansion_function) | 352 filename_expansion_function=self.filename_expansion_function) |
| 324 else: | 353 else: |
| 325 distribution = html_inline.GetDistribution() | 354 distribution = html_inline.GetDistribution() |
| 326 self.inlined_text_ = ProcessImageSets( | 355 self.inlined_text_ = ProcessImageSets( |
| 327 os.path.dirname(filename), | 356 os.path.dirname(filename), |
| 328 util.ReadFile(filename, 'utf-8'), | 357 util.ReadFile(filename, 'utf-8'), |
| 329 self.scale_factors_, | 358 self.scale_factors_, |
| 330 distribution, | 359 distribution, |
| 331 filename_expansion_function=self.filename_expansion_function) | 360 filename_expansion_function=self.filename_expansion_function) |
| OLD | NEW |