From 5f07e3ce56a49b916f8fcbc3c404df9d7370e509 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Tue, 1 Aug 2023 10:13:20 +0100
Subject: [PATCH] Issue #3317745 by heykarthikwithu, mkalkbrenner, catch,
 smustgrave, Schoenef: CSS Aggregation should not rewrite # url

---
 core/lib/Drupal/Core/Asset/CssOptimizer.php          |  4 ++--
 .../Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php | 12 ++++++++++++
 .../Tests/Core/Asset/css_test_files/import3.css      |  6 ++++++
 .../Asset/css_test_files/import3.css.optimized.css   |  1 +
 4 files changed, 21 insertions(+), 2 deletions(-)
 create mode 100644 core/tests/Drupal/Tests/Core/Asset/css_test_files/import3.css
 create mode 100644 core/tests/Drupal/Tests/Core/Asset/css_test_files/import3.css.optimized.css

diff --git a/core/lib/Drupal/Core/Asset/CssOptimizer.php b/core/lib/Drupal/Core/Asset/CssOptimizer.php
index bc2aef723866..413106cf9833 100644
--- a/core/lib/Drupal/Core/Asset/CssOptimizer.php
+++ b/core/lib/Drupal/Core/Asset/CssOptimizer.php
@@ -90,8 +90,8 @@ protected function processFile($css_asset) {
     // Store base path.
     $this->rewriteFileURIBasePath = $css_base_path . '/';
 
-    // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
-    return preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', [$this, 'rewriteFileURI'], $contents);
+    // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths and paths starting with '#'.
+    return preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+|#|%23)([^\'")]+)[\'"]?\s*\)/i', [$this, 'rewriteFileURI'], $contents);
   }
 
   /**
diff --git a/core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php b/core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php
index 2c4992841629..347759527f1a 100644
--- a/core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php
+++ b/core/tests/Drupal/Tests/Core/Asset/CssOptimizerUnitTest.php
@@ -222,6 +222,18 @@ public function providerTestOptimize() {
         ],
         file_get_contents($absolute_path . 'quotes.css.optimized.css'),
       ],
+      [
+        [
+          'group' => -100,
+          'type' => 'file',
+          'weight' => 0.013,
+          'media' => 'all',
+          'preprocess' => TRUE,
+          'data' => $path . 'import3.css',
+          'basename' => 'import3.css',
+        ],
+        file_get_contents($absolute_path . 'import3.css.optimized.css'),
+      ],
     ];
   }
 
diff --git a/core/tests/Drupal/Tests/Core/Asset/css_test_files/import3.css b/core/tests/Drupal/Tests/Core/Asset/css_test_files/import3.css
new file mode 100644
index 000000000000..b2ba7a03ece4
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/css_test_files/import3.css
@@ -0,0 +1,6 @@
+div {
+  clip-path: url('#clip-cloud');
+}
+div {
+  clip-path: url('/abc/#clip-cloud');
+}
diff --git a/core/tests/Drupal/Tests/Core/Asset/css_test_files/import3.css.optimized.css b/core/tests/Drupal/Tests/Core/Asset/css_test_files/import3.css.optimized.css
new file mode 100644
index 000000000000..119376b467fe
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Asset/css_test_files/import3.css.optimized.css
@@ -0,0 +1 @@
+div{clip-path:url('#clip-cloud');}div{clip-path:url('/abc/#clip-cloud');}
-- 
GitLab