Skip to content

Commit

Permalink
CSS variable
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Mar 20, 2024
1 parent e41f194 commit d86e973
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Languages/Css/CssLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Tempest\Highlight\Languages\Css\Patterns\CssCommentPattern;
use Tempest\Highlight\Languages\Css\Patterns\CssFunctionPattern;
use Tempest\Highlight\Languages\Css\Patterns\CssSelectorPattern;
use Tempest\Highlight\Languages\Css\Patterns\CssVariablePattern;

class CssLanguage extends BaseLanguage
{
Expand All @@ -27,6 +28,7 @@ public function getPatterns(): array
new CssSelectorPattern(),
new CssAttributePattern(),
new CssFunctionPattern(),
new CssVariablePattern(),
];
}
}
27 changes: 27 additions & 0 deletions src/Languages/Css/Patterns/CssVariablePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Css\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: '--var-name: foo', output: '--var-name')]
#[PatternTest(input: 'var(--var-name)', output: '--var-name')]
final readonly class CssVariablePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<match>\-\-[\w\-]+)';
}

public function getTokenType(): TokenType
{
return TokenType::PROPERTY;
}
}
43 changes: 43 additions & 0 deletions tests/Languages/Css/CssLanguageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Tests\Languages\Css;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Tempest\Highlight\Highlighter;

class CssLanguageTest extends TestCase
{
#[DataProvider('data')]
public function test_highlight(string $content, string $expected): void
{
$highlighter = new Highlighter();

$this->assertSame(
$expected,
$highlighter->parse($content, 'css'),
);
}

public static function data(): array
{
return [
[
<<<TXT
.foo {
--var: bar;
color: var(--var);
}
TXT,
<<<TXT
<span class="hl-keyword">.foo </span>{
<span class="hl-property">--var</span>: bar;
<span class="hl-property">color</span>: <span class="hl-keyword">var</span>(<span class="hl-property">--var</span>);
}
TXT,
],
];
}
}
7 changes: 5 additions & 2 deletions tests/test.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
```php
$this->extends('View/base.view.php');
```css
.foo {
--var: bar;
color: var(--var);
}
```

0 comments on commit d86e973

Please sign in to comment.