diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py index dfca0f4a2d..e97355831b 100644 --- a/django/contrib/staticfiles/storage.py +++ b/django/contrib/staticfiles/storage.py @@ -42,15 +42,21 @@ class StaticFilesStorage(FileSystemStorage): class HashedFilesMixin: - default_template = """url("%s")""" + default_template = """url("%(url)s")""" max_post_process_passes = 5 patterns = ( ("*.css", ( - r"""(url\(['"]{0,1}\s*(.*?)["']{0,1}\))""", - (r"""(@import\s*["']\s*(.*?)["'])""", """@import url("%s")"""), + r"""(?Purl\(['"]{0,1}\s*(?P.*?)["']{0,1}\))""", + ( + r"""(?P@import\s*["']\s*(?P.*?)["'])""", + """@import url("%(url)s")""", + ), )), ('*.js', ( - (r'(?m)^(//# (?-i:sourceMappingURL)=(.*))$', '//# sourceMappingURL=%s'), + ( + r'(?P)^(//# (?-i:sourceMappingURL)=(?P.*))$', + '//# sourceMappingURL=%(url)s', + ), )), ) keep_intermediate_files = True @@ -163,7 +169,9 @@ class HashedFilesMixin: This requires figuring out which files the matched URL resolves to and calling the url() method of the storage. """ - matched, url = matchobj.groups() + matches = matchobj.groupdict() + matched = matches['matched'] + url = matches['url'] # Ignore absolute/protocol-relative and data-uri URLs. if re.match(r'^[a-z]+:', url): @@ -199,7 +207,8 @@ class HashedFilesMixin: transformed_url += ('?#' if '?#' in url else '#') + fragment # Return the hashed version to the file - return template % unquote(transformed_url) + matches['url'] = unquote(transformed_url) + return template % matches return converter diff --git a/tests/staticfiles_tests/storage.py b/tests/staticfiles_tests/storage.py index 05fdec8db2..e94534fff0 100644 --- a/tests/staticfiles_tests/storage.py +++ b/tests/staticfiles_tests/storage.py @@ -84,7 +84,10 @@ class ExtraPatternsStorage(ManifestStaticFilesStorage): patterns = tuple(ManifestStaticFilesStorage.patterns) + ( ( "*.js", ( - (r"""(url\(['"]{0,1}\s*(.*?)["']{0,1}\))""", 'JS_URL("%s")'), + ( + r"""(?Purl\(['"]{0,1}\s*(?P.*?)["']{0,1}\))""", + 'JS_URL("%(url)s")', + ), ), ), )