Commit 3347abd9 authored by Mike Frysinger's avatar Mike Frysinger Committed by Commit Bot

grit: chrome_html: rewrite re.sub to avoid Python 3.7+ change

The Python folks decided to break re.sub behavior and have it randomly
replace empty matches with non-empty strings.  For example:
  >>> re.sub(r'(.*)', r'foo', 'asd')
  'foofoo'

To avoid this crazy pants behavior, we could make the regex match the
entire string (by adding ^ and $ anchors), but this bit of code doesn't
really need a regex at all.  We're simply inserting the string "2x"
before the last "/" in the filename.  Rewrite the code to do a little
ad-hoc string splitting instead as this is a bit easier to read.

Bug: 983071
Test: `./grit/test_suite_all.py` passes
Change-Id: I7aa005d685bb1019c464af1c1f12ce2701431ddb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1755391Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#687291}
parent 2858b9f6
...@@ -101,9 +101,12 @@ def GetImageList( ...@@ -101,9 +101,12 @@ def GetImageList(
scale_image_path = os.path.join(scale_path[0], scale_factor, scale_path[1]) scale_image_path = os.path.join(scale_path[0], scale_factor, scale_path[1])
if os.path.isfile(scale_image_path): if os.path.isfile(scale_image_path):
# HTML/CSS always uses forward slashed paths. # HTML/CSS always uses forward slashed paths.
scale_image_name = re.sub('(?P<path>(.*/)?)(?P<file>[^/]*)', parts = filename.rsplit('/', 1)
'\\g<path>' + scale_factor + '/\\g<file>', if len(parts) == 1:
filename) path = ''
else:
path = parts[0] + '/'
scale_image_name = path + scale_factor + '/' + parts[-1]
images.append((scale_factor, scale_image_name)) images.append((scale_factor, scale_image_name))
return images return images
......
...@@ -146,6 +146,35 @@ class ChromeHtmlUnittest(unittest.TestCase): ...@@ -146,6 +146,35 @@ class ChromeHtmlUnittest(unittest.TestCase):
''')) '''))
tmp_dir.CleanUp() tmp_dir.CleanUp()
def testFileResourcesNoFlattenSubdir(self):
'''Tests non-inlined image file resources w/high DPI assets in subdirs.'''
tmp_dir = util.TempDir({
'test.css': '''
.image {
background: url('sub/test.png');
}
''',
'sub/test.png': 'PNG DATA',
'sub/1.4x/test.png': '1.4x PNG DATA',
'sub/1.8x/test.png': '1.8x PNG DATA',
})
html = chrome_html.ChromeHtml(tmp_dir.GetPath('test.css'))
html.SetDefines({'scale_factors': '1.4x,1.8x'})
html.SetAttributes({'flattenhtml': 'false'})
html.Parse()
self.failUnlessEqual(StandardizeHtml(html.GetData('en', 'utf-8')),
StandardizeHtml('''
.image {
background: -webkit-image-set(url('sub/test.png') 1x, url('sub/1.4x/test.png') 1.4x, url('sub/1.8x/test.png') 1.8x);
}
'''))
tmp_dir.CleanUp()
def testFileResourcesPreprocess(self): def testFileResourcesPreprocess(self):
'''Tests preprocessed image file resources with available high DPI '''Tests preprocessed image file resources with available high DPI
assets.''' assets.'''
...@@ -230,6 +259,33 @@ class ChromeHtmlUnittest(unittest.TestCase): ...@@ -230,6 +259,33 @@ class ChromeHtmlUnittest(unittest.TestCase):
''')) '''))
tmp_dir.CleanUp() tmp_dir.CleanUp()
def testFileResourcesSubdirs(self):
'''Tests inlined image file resources if url() filename is in a subdir.'''
tmp_dir = util.TempDir({
'test.css': '''
.image {
background: url('some/sub/path/test.png');
}
''',
'some/sub/path/test.png': 'PNG DATA',
'some/sub/path/2x/test.png': '2x PNG DATA',
})
html = chrome_html.ChromeHtml(tmp_dir.GetPath('test.css'))
html.SetDefines({'scale_factors': '2x'})
html.SetAttributes({'flattenhtml': 'true'})
html.Parse()
self.failUnlessEqual(StandardizeHtml(html.GetData('en', 'utf-8')),
StandardizeHtml('''
.image {
background: -webkit-image-set(url('data:image/png;base64,UE5HIERBVEE=') 1x, url('data:image/png;base64,MnggUE5HIERBVEE=') 2x);
}
'''))
tmp_dir.CleanUp()
def testFileResourcesNoFile(self): def testFileResourcesNoFile(self):
'''Tests inlined image file resources without available high DPI assets.''' '''Tests inlined image file resources without available high DPI assets.'''
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment