Skip to content

Commit 2c36d37

Browse files
committed
Change style builder
1 parent bfa16f0 commit 2c36d37

File tree

4 files changed

+62
-26
lines changed

4 files changed

+62
-26
lines changed

src/Compiler.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ public function convert(): string
137137
/** @var DOMElement|null $scriptElement */
138138
$scriptElement = $this->document->getElementsByTagName('script')->item(0);
139139

140-
/** @var DOMElement|null $scriptElement */
141-
$styleElement = $this->document->getElementsByTagName('style')->item(0);
140+
$styleBlocks = $this->document->getElementsByTagName('style');
142141

143142
$twigBlocks = $this->document->getElementsByTagName('twig');
144143

@@ -154,8 +153,11 @@ public function convert(): string
154153
}
155154
}
156155

157-
if ($styleElement && !empty($styleElement->textContent)) {
158-
$this->rawBlocks[] = $this->styleBuilder->compile($styleElement);
156+
if ($styleBlocks->length) {
157+
foreach ($styleBlocks as $styleBlock) {
158+
/* @var DOMElement $styleBlock */
159+
$this->rawBlocks[] = $this->styleBuilder->compile($styleBlock);
160+
}
159161
}
160162

161163
if (!$templateElement) {
@@ -1015,7 +1017,7 @@ private function twigRemove(DOMElement $node): bool
10151017

10161018
private function addScopedAttribute(DOMElement $node): void
10171019
{
1018-
if (!$this->styleBuilder->isScoped()) {
1020+
if (!$this->styleBuilder->hasScoped()) {
10191021
return;
10201022
}
10211023
$scopedAttribute = $this->styleBuilder->getScopedAttribute();

src/Utils/StyleBuilder.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Paneon\VueToTwig\Utils;
66

77
use DOMElement;
8-
use DOMNode;
98
use Exception;
109
use Ramsey\Uuid\Uuid;
1110
use ScssPhp\ScssPhp\Compiler as ScssCompiler;
@@ -23,9 +22,9 @@ class StyleBuilder
2322
private $lang;
2423

2524
/**
26-
* @var bool|null
25+
* @var bool
2726
*/
28-
private $isScoped;
27+
private $hasScoped;
2928

3029
/**
3130
* @var string|null
@@ -40,50 +39,46 @@ class StyleBuilder
4039
public function __construct()
4140
{
4241
$this->scopedAttribute = 'data-v-' . substr(md5(Uuid::uuid4()->toString()), 0, 8);
42+
$this->hasScoped = false;
4343
}
4444

4545
/**
46-
* @param DOMNode|DOMElement|null $styleElement
46+
* @param DOMElement|null $styleElement
4747
*/
4848
public function compile($styleElement): ?string
4949
{
5050
if (!$styleElement instanceof DOMElement) {
5151
return null;
5252
}
5353

54-
$this->handle($styleElement);
54+
if (
55+
!$this->scssCompiler instanceof ScssCompiler
56+
&& $styleElement->hasAttribute('lang')
57+
&& $styleElement->getAttribute('lang') === 'scss'
58+
) {
59+
$this->lang = 'scss';
60+
$this->scssCompiler = new ScssCompiler();
61+
}
5562

5663
$style = $styleElement->textContent;
5764
if ($this->lang === 'scss') {
5865
$style = $this->scssCompiler->compile($style);
5966
}
60-
if ($this->isScoped) {
67+
if ($styleElement->hasAttribute('scoped')) {
68+
$this->hasScoped = true;
6169
$style = preg_replace('/((?:^|[^},]*?)\S+)(\s*[{,])/i', '$1[' . $this->scopedAttribute . ']$2', $style);
6270
}
6371

6472
return '<style>' . $style . '</style>';
6573
}
6674

67-
public function isScoped(): ?bool
75+
public function hasScoped(): ?bool
6876
{
69-
return $this->isScoped;
77+
return $this->hasScoped;
7078
}
7179

7280
public function getScopedAttribute(): string
7381
{
7482
return $this->scopedAttribute;
7583
}
76-
77-
private function handle(DOMElement $styleElement): void
78-
{
79-
$this->isScoped = $styleElement->hasAttribute('scoped');
80-
if (
81-
!$this->scssCompiler instanceof ScssCompiler
82-
&& $styleElement->hasAttribute('lang')
83-
&& $styleElement->getAttribute('lang') === 'scss'
84-
) {
85-
$this->lang = 'scss';
86-
$this->scssCompiler = new ScssCompiler();
87-
}
88-
}
8984
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<style> .foo { color: red; }</style>
2+
<style> .bar[data-v-12345678] { color: blue; }</style>
3+
<style>.baz { color: #00FF00; }</style>
4+
<div data-v-12345678="" class="{{ class|default('') }}" style="{{ style|default('') }}">
5+
<div class="foo bar baz" data-v-12345678="">
6+
42
7+
</div>
8+
</div>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<template>
2+
<div>
3+
<div class="foo bar baz">
4+
42
5+
</div>
6+
</div>
7+
</template>
8+
9+
<style>
10+
.foo {
11+
color: red;
12+
}
13+
</style>
14+
15+
<style scoped>
16+
.bar {
17+
color: blue;
18+
}
19+
</style>
20+
21+
<style lang="scss">
22+
$green: #00FF00;
23+
.baz {
24+
color: $green;
25+
}
26+
</style>
27+
28+
<script>
29+
export default {
30+
}
31+
</script>

0 commit comments

Comments
 (0)