Skip to content

Commit

Permalink
Merge pull request #5 from achlee/ignore-invalid-paths
Browse files Browse the repository at this point in the history
Ignore invalid paths
  • Loading branch information
Chikashi-Kato committed Jul 11, 2019
2 parents 03f46cc + e0348c7 commit 628b97f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/Assetic/SourceMapFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ protected function doFilterDump(CHAssetBag $assetBag, string $tmpOutput, array $
// the 'sources' property is what dev tools (such as Chrome DevTools) display as the filename for the original source
// transform tmp file names back to original file name
$sourceMap['sources'] = array_map(function ($source) use ($tmpInputToAssetMap) {

if (array_key_exists($source, $tmpInputToAssetMap)) {
$asset = $tmpInputToAssetMap[$source];
if ($asset instanceof HttpAsset) {
Expand All @@ -161,13 +162,15 @@ protected function doFilterDump(CHAssetBag $assetBag, string $tmpOutput, array $

$source = $asset->getSourceRoot() . '/' . $asset->getSourcePath();
}

// remove the first part of the path - what's left should be relative to the root project directory
$source = preg_replace("#^{$this->sourceMapSourcePathTrim}#", '', $source);

$source = Utils\removeRelPathComponents($source);

try {
$source = Utils\removeRelPathComponents($source);
} catch (\Exception $e) {
// relative paths above the root are ok here
}
$source = ltrim($source, '/');

return $source;
}, $sourceMap['sources']);

Expand Down
9 changes: 5 additions & 4 deletions tests/SourceMapFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testSourceMapFilterSimple()
$collection->add($this->makeStringAsset(__DIR__ . '/simple', 'asset1.js'));
$collection->add($this->makeStringAsset(__DIR__ . '/simple', 'asset2.js'));
$collection->add($this->makeStringAsset(__DIR__ . '/simple', 'asset3.js'));
$collection->add($this->makeStringAsset('/../..', 'climb-above-root.js'));
$collection = $worker->process($collection, $factory);

$this->assertEquals(file_get_contents('tests/simple/expected.js'), $collection->dump());
Expand All @@ -59,7 +60,7 @@ public function testSourceMapFilterComplex()
]]
]
]);

$factory = $this->createMock(AssetFactory::class);

$collection = new AssetCollection();
Expand All @@ -70,7 +71,7 @@ public function testSourceMapFilterComplex()
$collection = $worker->process($collection, $factory);

$this->assertEquals(file_get_contents('tests/complex/expected.js'), $collection->dump());

$sourceMap = $this->loadSourceMap("$asseticWriteTo/expected.js.map");
$this->assertEquals($this->loadSourceMap('tests/complex/expected.js.map'), $sourceMap);
}
Expand All @@ -90,7 +91,7 @@ public function testSourceMapFilterComposedSourceMap()
]]
]
]);

$factory = $this->createMock(AssetFactory::class);

$collection = new AssetCollection();
Expand All @@ -100,7 +101,7 @@ public function testSourceMapFilterComposedSourceMap()
$collection = $worker->process($collection, $factory);

$this->assertEquals(file_get_contents('tests/composed/expected.js'), $collection->dump());

$sourceMap = $this->loadSourceMap("$asseticWriteTo/composed.js.map");
$this->assertEquals($this->loadSourceMap('tests/composed/expected.js.map'), $sourceMap);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/simple/expected.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions tests/simple/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 628b97f

Please sign in to comment.