From a231f9b08df8cbcd64ec3fb7f6f831d88d9774a2 Mon Sep 17 00:00:00 2001 From: GappleBee Date: Fri, 27 Sep 2024 13:37:57 +0100 Subject: [PATCH] Fixed #35371 -- Ensured that only unhashed file paths were being processed. Used different functions for increased readability. Fixed edge case and added test for newlines. Used a different method for splitting the content which didn't remove the ending newline. --- django/contrib/staticfiles/storage.py | 23 ++++++++++-- .../project/documents/cached/module.js | 35 ++++++++++++++++++- tests/staticfiles_tests/test_storage.py | 4 +-- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py index dfc3137f76..c9451cdbaa 100644 --- a/django/contrib/staticfiles/storage.py +++ b/django/contrib/staticfiles/storage.py @@ -364,7 +364,8 @@ class HashedFilesMixin: if name in adjustable_paths: old_hashed_name = hashed_name try: - content = original_file.read().decode("utf-8") + unprocessed_content = original_file.read().decode("utf-8") + final_content = unprocessed_content except UnicodeDecodeError as exc: yield name, None, exc, False for extension, patterns in self._patterns.items(): @@ -374,13 +375,29 @@ class HashedFilesMixin: name, hashed_files, template ) try: - content = pattern.sub(converter, content) + processed_content = pattern.sub( + converter, unprocessed_content + ) + final_content_lines = final_content.split("\n") + processed_content_lines = processed_content.split( + "\n" + ) + for i, final_content_line in enumerate( + final_content_lines + ): + if len(final_content_line) < len( + processed_content_lines[i] + ): + final_content_lines[i] = ( + processed_content_lines[i] + ) # Keep the processed line + final_content = "\n".join(final_content_lines) except ValueError as exc: yield name, None, exc, False if hashed_file_exists: self.delete(hashed_name) # then save the processed result - content_file = ContentFile(content.encode()) + content_file = ContentFile(final_content.encode()) if self.keep_intermediate_files: # Save intermediate file for reference self._save(hashed_name, content_file) diff --git a/tests/staticfiles_tests/project/documents/cached/module.js b/tests/staticfiles_tests/project/documents/cached/module.js index c56530aea6..cf0d1e3366 100644 --- a/tests/staticfiles_tests/project/documents/cached/module.js +++ b/tests/staticfiles_tests/project/documents/cached/module.js @@ -1,3 +1,21 @@ + + + + + + + + + + + + + +const moduleConst2 = "module2"; +export default moduleConst2; +export const moduleConst = "module"; +// export keyword test + // Static imports. import rootConst from "/static/absolute_root.js"; import testConst from "./module_test.js"; @@ -18,9 +36,24 @@ import relativeModule from "../nested/js/nested.js"; const dynamicModule = import("./module_test.js"); // Modules exports to aggregate modules. -export * from "./module_test.js"; +export * from "./module_test.js"; // export keyword test export { testConst } from "./module_test.js"; export { firstVar as firstVarAlias, secondVar as secondVarAlias } from "./module_test.js"; + + + + + + + + + + + + + + + diff --git a/tests/staticfiles_tests/test_storage.py b/tests/staticfiles_tests/test_storage.py index 9ca4d62553..042a5f2665 100644 --- a/tests/staticfiles_tests/test_storage.py +++ b/tests/staticfiles_tests/test_storage.py @@ -676,7 +676,7 @@ class TestCollectionJSModuleImportAggregationManifestStorage(CollectionTestCase) def test_module_import(self): relpath = self.hashed_file_path("cached/module.js") - self.assertEqual(relpath, "cached/module.4326210cf0bd.js") + self.assertEqual(relpath, "cached/module.f0c3a6133ad5.js") tests = [ # Relative imports. b'import testConst from "./module_test.477bbebe77f0.js";', @@ -708,7 +708,7 @@ class TestCollectionJSModuleImportAggregationManifestStorage(CollectionTestCase) def test_aggregating_modules(self): relpath = self.hashed_file_path("cached/module.js") - self.assertEqual(relpath, "cached/module.4326210cf0bd.js") + self.assertEqual(relpath, "cached/module.f0c3a6133ad5.js") tests = [ b'export * from "./module_test.477bbebe77f0.js";', b'export { testConst } from "./module_test.477bbebe77f0.js";',