Commit a1b3926d authored by catch's avatar catch
Browse files

Issue #3414173 by joelpittet, anybody, nicxvan: Add support for minified external CSS libraries

(cherry picked from commit 8986a105)
parent cf7c02a6
Loading
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -148,8 +148,15 @@ public function optimizeGroup(array $group): string {
        $data .= "/* @license " . $css_asset['license']['name'] . " " . $css_asset['license']['url'] . " */\n";
      }
      $current_license = $css_asset['license'];

      // Append this file if already minified; otherwise optimize it.
      if (isset($css_asset['minified']) && $css_asset['minified']) {
        $data .= file_get_contents($css_asset['data']);
      }
      else {
        $data .= $this->optimizer->optimize($css_asset);
      }
    }
    // Per the W3C specification at
    // https://www.w3.org/TR/REC-CSS2/cascade.html#at-import, @import rules must
    // precede any other style, so we move those to the top. The regular
+36 −0
Original line number Diff line number Diff line
@@ -148,4 +148,40 @@ public function testCssLicenseAggregation(): void {
    self::assertStringEqualsFile(__DIR__ . '/css_test_files/css_license.css.optimized.aggregated.css', $aggregate);
  }

  /**
   * Test that external minified CSS assets do not trigger optimization.
   *
   * This ensures that fully external asset groups do not result in a
   * CssOptimizer exception and are safely ignored.
   */
  public function testExternalMinifiedCssAssetOptimizationIsSkipped(): void {
    $mock_grouper = $this->createMock(AssetCollectionGrouperInterface::class);
    $mock_optimizer = $this->createMock(AssetOptimizerInterface::class);
    $mock_optimizer->expects($this->never())->method('optimize');

    $optimizer = new CssCollectionOptimizerLazy(
      $mock_grouper,
      $mock_optimizer,
      $this->createMock(ThemeManagerInterface::class),
      $this->createMock(LibraryDependencyResolverInterface::class),
      new RequestStack(),
      $this->createMock(FileSystemInterface::class),
      $this->createMock(ConfigFactoryInterface::class),
      $this->createMock(FileUrlGeneratorInterface::class),
      $this->createMock(TimeInterface::class),
      $this->createMock(LanguageManagerInterface::class)
    );
    $optimizer->optimizeGroup([
      'items' => [
        [
          'type' => 'external',
          'data' => __DIR__ . '/css_test_files/css_external.optimized.aggregated.css',
          'license' => FALSE,
          'preprocess' => TRUE,
          'minified' => TRUE,
        ],
      ],
    ]);
  }

}
+1 −0
Original line number Diff line number Diff line
/* Placeholder external CSS file. */