Commit e1396322 authored by webchick's avatar webchick

Issue #2400287 by hass, cutesquirrel, borisson_, rteijeiro, pfrenssen,...

Issue #2400287 by hass, cutesquirrel, borisson_, rteijeiro, pfrenssen, cilefen: Remove all occurences of sourceMappingURL and sourceURL when JS files are aggregated
parent 3153536c
......@@ -22,4 +22,15 @@ interface AssetOptimizerInterface {
*/
public function optimize(array $asset);
/**
* Removes unwanted content from an asset.
*
* @param string $content
* The content of an asset.
*
* @return string
* The cleaned asset's contents.
*/
public function clean($content);
}
......@@ -39,12 +39,31 @@ public function optimize(array $css_asset) {
}
}
/**
* Processes the contents of a CSS asset for cleanup.
*
* @param string $contents
* The contents of the CSS asset.
*
* @return string
* Contents of the CSS asset.
*/
public function clean($contents) {
// Remove multiple charset declarations for standards compliance (and fixing
// Safari problems).
$contents = preg_replace('/^@charset\s+[\'"](\S*?)\b[\'"];/i', '', $contents);
return $contents;
}
/**
* Build aggregate CSS file.
*/
protected function processFile($css_asset) {
$contents = $this->loadFile($css_asset['data'], TRUE);
$contents = $this->clean($contents);
// Get the parent directory of this file, relative to the Drupal root.
$css_base_path = substr($css_asset['data'], 0, strrpos($css_asset['data'], '/'));
// Store base path.
......@@ -161,8 +180,8 @@ protected function loadNestedFile($matches) {
* Contents of the stylesheet including the imported stylesheets.
*/
protected function processCss($contents, $optimize = FALSE) {
// Remove multiple charset declarations for standards compliance (and fixing Safari problems).
$contents = preg_replace('/^@charset\s+[\'"](\S*?)\b[\'"];/i', '', $contents);
// Remove unwanted CSS code that cause issues.
$contents = $this->clean($contents);
if ($optimize) {
// Perform some safe CSS optimizations.
......
......@@ -123,6 +123,8 @@ public function optimize(array $js_assets) {
// from running together.
$data .= ";\n";
}
// Remove unwanted JS code that cause issues.
$data = $this->optimizer->clean($data);
// Dump the optimized JS for this group into an aggregate file.
$uri = $this->dumper->dump($data, 'js');
// Set the URI for this group's aggregate file.
......
......@@ -28,4 +28,20 @@ public function optimize(array $js_asset) {
return file_get_contents($js_asset['data']);
}
/**
* Processes the contents of a javascript asset for cleanup.
*
* @param string $contents
* The contents of the javascript asset.
*
* @return string
* Contents of the javascript asset.
*/
public function clean($contents) {
// Remove JS source and source mapping urls or these may cause 404 errors.
$contents = preg_replace('/\/\/(#|@)\s(sourceURL|sourceMappingURL)=\s*(\S*?)\s*$/m', '', $contents);
return $contents;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Asset\JsOptimizerUnitTest.
*/
namespace Drupal\Tests\Core\Asset;
use Drupal\Core\Asset\JsOptimizer;
use Drupal\Tests\UnitTestCase;
/**
* Tests the JS asset optimizer.
*
* @group Asset
*/
class JsOptimizerUnitTest extends UnitTestCase {
/**
* A JS asset optimizer.
*
* @var \Drupal\Core\Asset\JsOptimizer object.
*/
protected $optimizer;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->optimizer = new JsOptimizer();
}
/**
* Provides data for the JS asset cleaning test.
*
* @see \Drupal\Core\Asset\JsOptimizer::clean().
*
* @returns array
* An array of test data.
*/
function providerTestClean() {
$path = dirname(__FILE__) . '/js_test_files/';
return array(
// File. Tests:
// - Stripped sourceMappingURL with comment # syntax.
0 => array(
file_get_contents($path . 'source_mapping_url.min.js'),
file_get_contents($path . 'source_mapping_url.min.js.optimized.js'),
),
// File. Tests:
// - Stripped sourceMappingURL with comment @ syntax.
1 => array(
file_get_contents($path . 'source_mapping_url_old.min.js'),
file_get_contents($path . 'source_mapping_url_old.min.js.optimized.js'),
),
// File. Tests:
// - Stripped sourceURL with comment # syntax.
2 => array(
file_get_contents($path . 'source_url.min.js'),
file_get_contents($path . 'source_url.min.js.optimized.js'),
),
// File. Tests:
// - Stripped sourceURL with comment @ syntax.
3 => array(
file_get_contents($path . 'source_url_old.min.js'),
file_get_contents($path . 'source_url_old.min.js.optimized.js'),
),
);
}
/**
* Tests cleaning of a JS asset group containing 'type' => 'file'.
*
* @dataProvider providerTestClean
*/
function testClean($js_asset, $expected) {
$this->assertEquals($expected, $this->optimizer->clean($js_asset));
}
}
@charset 'UTF-8';
@charset "UTF-8";
html{font-family:"sans-serif";}
(function($) { "use strict"; })
//# sourceMappingURL=source_mapping_url.min.map
(function($) { "use strict"; })
//@ sourceMappingURL=source_mapping_url.min.map
(function($) { "use strict"; })
//# sourceURL=source_mapping_url.js
(function($) { "use strict"; })
//@ sourceURL=source_mapping_url.js
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