16
16
use Paneon \VueToTwig \Models \Replacements ;
17
17
use Paneon \VueToTwig \Models \Slot ;
18
18
use Paneon \VueToTwig \Utils \NodeHelper ;
19
+ use Paneon \VueToTwig \Utils \StyleBuilder ;
19
20
use Paneon \VueToTwig \Utils \TwigBuilder ;
20
21
use Psr \Log \LoggerInterface ;
21
22
use ReflectionException ;
@@ -53,6 +54,11 @@ class Compiler
53
54
*/
54
55
protected $ builder ;
55
56
57
+ /**
58
+ * @var StyleBuilder
59
+ */
60
+ protected $ styleBuilder ;
61
+
56
62
/**
57
63
* @var NodeHelper
58
64
*/
@@ -94,6 +100,7 @@ class Compiler
94
100
public function __construct (DOMDocument $ document , LoggerInterface $ logger )
95
101
{
96
102
$ this ->builder = new TwigBuilder ();
103
+ $ this ->styleBuilder = new StyleBuilder ();
97
104
$ this ->nodeHelper = new NodeHelper ();
98
105
$ this ->document = $ document ;
99
106
$ this ->logger = $ logger ;
@@ -130,6 +137,8 @@ public function convert(): string
130
137
/** @var DOMElement|null $scriptElement */
131
138
$ scriptElement = $ this ->document ->getElementsByTagName ('script ' )->item (0 );
132
139
140
+ $ styleBlocks = $ this ->document ->getElementsByTagName ('style ' );
141
+
133
142
$ twigBlocks = $ this ->document ->getElementsByTagName ('twig ' );
134
143
135
144
if ($ scriptElement ) {
@@ -144,6 +153,13 @@ public function convert(): string
144
153
}
145
154
}
146
155
156
+ if ($ styleBlocks ->length ) {
157
+ foreach ($ styleBlocks as $ styleBlock ) {
158
+ /* @var DOMElement $styleBlock */
159
+ $ this ->rawBlocks [] = $ this ->styleBuilder ->compile ($ styleBlock );
160
+ }
161
+ }
162
+
147
163
if (!$ templateElement ) {
148
164
throw new Exception ('The template file does not contain a template tag. ' );
149
165
}
@@ -203,6 +219,7 @@ public function convertNode(DOMNode $node, int $level = 0): DOMNode
203
219
$ this ->stripEventHandlers ($ node );
204
220
$ this ->handleSlots ($ node );
205
221
$ this ->cleanupAttributes ($ node );
222
+ $ this ->addScopedAttribute ($ node );
206
223
}
207
224
208
225
// Registered Component
@@ -849,6 +866,13 @@ public function disableStyleInclude(): Compiler
849
866
return $ this ;
850
867
}
851
868
869
+ public function setStyleBlockOutputType (int $ outputType ): Compiler
870
+ {
871
+ $ this ->styleBuilder ->setOutputType ($ outputType );
872
+
873
+ return $ this ;
874
+ }
875
+
852
876
/**
853
877
* @param mixed $value
854
878
*/
@@ -997,4 +1021,13 @@ private function twigRemove(DOMElement $node): bool
997
1021
998
1022
return false ;
999
1023
}
1024
+
1025
+ private function addScopedAttribute (DOMElement $ node ): void
1026
+ {
1027
+ if (!$ this ->styleBuilder ->hasScoped ()) {
1028
+ return ;
1029
+ }
1030
+ $ scopedAttribute = $ this ->styleBuilder ->getScopedAttribute ();
1031
+ $ node ->setAttributeNode (new DOMAttr ($ scopedAttribute , '' ));
1032
+ }
1000
1033
}
0 commit comments