forked from scssphp/scssphp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TL-40011: Adding in webkit-style transformation support
- Loading branch information
1 parent
022cbf1
commit 661e768
Showing
6 changed files
with
298 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
use ScssPhp\ScssPhp\Block; | ||
|
||
/** | ||
* A resource represents the scss file that is in the process of transforming. | ||
*/ | ||
class Resource { | ||
private bool $modified = false; | ||
|
||
public function __construct(protected string $path, protected Block $ast) { | ||
|
||
} | ||
|
||
/** | ||
* Return the modified Tree | ||
* | ||
* @return Block | ||
*/ | ||
public function getAst(): Block { | ||
return $this->ast; | ||
} | ||
|
||
/** | ||
* Marks the AST has been modified | ||
* | ||
* @return void | ||
*/ | ||
public function markASTModified(): void { | ||
$this->modified = true; | ||
} | ||
|
||
/** | ||
* Indicates if the resource AST has been modified or not. | ||
* | ||
* @return bool | ||
*/ | ||
public function isASTOnly(): bool { | ||
return $this->modified; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
use ScssPhp\ScssPhp\Block; | ||
|
||
class ResourceFactory { | ||
/** | ||
* Create a new resource instance to be used in transformations. | ||
* | ||
* @return Resource | ||
*/ | ||
public function createResource(string $path, Block $ast): Resource { | ||
return new Resource($path, $ast); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
interface Transform { | ||
/** | ||
* @param Resource $resource | ||
* @return void | ||
*/ | ||
public function execute(\ScssPhp\ScssPhp\Transforms\Resource $resource): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
use ScssPhp\ScssPhp\Block; | ||
|
||
class Transformer { | ||
|
||
public function __construct(protected ?ResourceFactory $factory = null, protected array $transforms = []) { | ||
|
||
} | ||
|
||
public function setResourceFactory(?ResourceFactory $factory): void { | ||
$this->factory = $factory; | ||
} | ||
|
||
public function registerTransform(string $name, Transform $transform): void { | ||
$this->transforms[$name] = $transform; | ||
} | ||
|
||
public function applyTransformations(array $transforms, string $path, Block $tree): Block { | ||
// Make the resource | ||
$resource = ($this->factory ?? new ResourceFactory())->createResource($path, $tree); | ||
|
||
// transforms execute from right to left (like webpack) | ||
$transforms = array_reverse($transforms, true); | ||
foreach ($transforms as $name) { | ||
if (!isset($this->transforms[$name])) { | ||
throw new \Exception('Unknown transform "' . $name . '"'); | ||
} | ||
$this->transforms[$name]->execute($resource); | ||
} | ||
|
||
return $resource->getAst(); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @param bool $split | ||
* @return array<string, array|string> | ||
*/ | ||
public function extractTransformsFromPath(string $path, bool $split = false): array { | ||
$pos = strrpos($path, '!'); | ||
$transforms = ""; | ||
|
||
if ($pos !== false) { | ||
$transforms = substr($path, 0, $pos); | ||
$path = substr($path, $pos + 1); | ||
} | ||
|
||
if ($split) { | ||
$transforms = empty($transforms) ? [] : explode('!', $transforms); | ||
} | ||
|
||
return [$path, $transforms]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ScssPhp\ScssPhp\Compiler; | ||
use ScssPhp\ScssPhp\OutputStyle; | ||
use ScssPhp\ScssPhp\Transforms\Resource; | ||
use ScssPhp\ScssPhp\Transforms\Transform; | ||
use ScssPhp\ScssPhp\Transforms\Transformer; | ||
|
||
class TransformerTest extends TestCase { | ||
|
||
public function testExtractTransformsFromPath(): void { | ||
$transformer = new Transformer(); | ||
|
||
[$path, $transforms] = $transformer->extractTransformsFromPath('/a/b/c'); | ||
$this->assertSame('/a/b/c', $path); | ||
$this->assertEmpty($transforms); | ||
|
||
[$path, $transforms] = $transformer->extractTransformsFromPath('a!/a/b/c'); | ||
$this->assertSame('/a/b/c', $path); | ||
$this->assertSame('a', $transforms); | ||
|
||
[$path, $transforms] = $transformer->extractTransformsFromPath('a!b!c!/a/b/c'); | ||
$this->assertSame('/a/b/c', $path); | ||
$this->assertSame('a!b!c', $transforms); | ||
|
||
[$path, $transforms] = $transformer->extractTransformsFromPath('/a/b/c', true); | ||
$this->assertSame('/a/b/c', $path); | ||
$this->assertIsArray($transforms); | ||
$this->assertEmpty($transforms, var_export($transforms, true)); | ||
|
||
[$path, $transforms] = $transformer->extractTransformsFromPath('a!/a/b/c', true); | ||
$this->assertSame('/a/b/c', $path); | ||
$this->assertIsArray($transforms); | ||
$this->assertSame(['a'], $transforms); | ||
|
||
[$path, $transforms] = $transformer->extractTransformsFromPath('a!b!c!/a/b/c', true); | ||
$this->assertSame('/a/b/c', $path); | ||
$this->assertIsArray($transforms); | ||
$this->assertSame(['a', 'b', 'c'], $transforms); | ||
} | ||
|
||
/** | ||
* Assert that a custom transformer can be applied | ||
*/ | ||
public function testCustomTransform(): void { | ||
$transform = new class() implements Transform { | ||
public function execute(Resource $resource): void { | ||
$resource->getAst()->children[0][1] = '/* TestTestD */'; | ||
} | ||
}; | ||
|
||
$transformer = new Transformer(); | ||
$transformer->registerTransform('test', $transform); | ||
|
||
$compiler = new Compiler(); | ||
$compiler->setOutputStyle(OutputStyle::EXPANDED); | ||
$compiler->setTransformer($transformer); | ||
|
||
$dummyData = fn($url) => '/* TestC */ a { color: red; }'; | ||
|
||
$compiler->setImportPaths([$dummyData]); | ||
$compiler->setFileLoader($dummyData); | ||
|
||
$input = '@import "test!my_file"; a { color: blue; }'; | ||
$expected = <<<HERE | ||
/* TestTestD */ | ||
a { | ||
color: red; | ||
} | ||
a { | ||
color: blue; | ||
} | ||
HERE; | ||
|
||
$result = $compiler->compileString($input)->getCss(); | ||
$this->assertSame($expected, $result); | ||
|
||
$compiler->setOutputStyle(OutputStyle::COMPRESSED); | ||
$result = $compiler->compileString($input)->getCss(); | ||
$this->assertSame('a{color:red}a{color:blue}', $result); | ||
|
||
$input = '@import "my_file"; a { color: blue; }'; | ||
$compiler->setOutputStyle(OutputStyle::EXPANDED); | ||
$expected = <<<HERE | ||
/* TestC */ | ||
a { | ||
color: red; | ||
} | ||
a { | ||
color: blue; | ||
} | ||
HERE; | ||
$result = $compiler->compileString($input)->getCss(); | ||
$this->assertSame($expected, $result); | ||
} | ||
|
||
} |