10
10
use Microsoft \PhpParser \Parser ;
11
11
use Microsoft \PhpParser \Range ;
12
12
use Stillat \BladeParser \Document \Document ;
13
+ use Stillat \BladeParser \Nodes \BaseNode ;
13
14
use Stillat \BladeParser \Nodes \DirectiveNode ;
14
15
use Stillat \BladeParser \Nodes \EchoNode ;
15
16
use Stillat \BladeParser \Nodes \EchoType ;
17
+ use Stillat \BladeParser \Nodes \LiteralNode ;
16
18
17
19
class InlineHtmlParser extends AbstractParser
18
20
{
21
+ protected $ echoStrings = [
22
+ '{!! ' => '!!} ' ,
23
+ '{{{ ' => '}}} ' ,
24
+ '{{ ' => '}} ' ,
25
+ ];
26
+
19
27
/**
20
28
* @var Blade
21
29
*/
@@ -42,6 +50,11 @@ public function parse(InlineHtml $node)
42
50
protected function parseBladeContent ($ node )
43
51
{
44
52
foreach ($ node ->getNodes () as $ child ) {
53
+ // TODO: Add other echo types as well
54
+ if ($ child instanceof LiteralNode) {
55
+ $ this ->parseLiteralNode ($ child );
56
+ }
57
+
45
58
if ($ child instanceof DirectiveNode) {
46
59
$ this ->parseBladeDirective ($ child );
47
60
}
@@ -54,22 +67,18 @@ protected function parseBladeContent($node)
54
67
}
55
68
}
56
69
57
- protected function parseBladeDirective ( DirectiveNode $ node )
70
+ protected function doEchoParse ( BaseNode $ node, $ prefix , $ content )
58
71
{
59
- if ($ node ->isClosingDirective || !$ node ->hasArguments ()) {
60
- return ;
61
- }
62
-
63
- $ methodUsed = '@ ' . $ node ->content ;
64
- $ safetyPrefix = 'directive ' ;
65
- $ snippet = "<?php \n" . str_repeat (' ' , $ node ->getStartIndentationLevel ()) . str_replace ($ methodUsed , $ safetyPrefix . $ node ->content , $ node ->toString () . '; ' );
72
+ $ snippet = "<?php \n" . str_repeat (' ' , $ node ->getStartIndentationLevel ()) . str_replace ($ prefix , '' , $ content ) . '; ' ;
66
73
67
74
$ sourceFile = (new Parser )->parseSourceFile ($ snippet );
68
75
69
- Settings::$ calculatePosition = function (Range $ range ) use ($ node , $ safetyPrefix ) {
76
+ $ suffix = $ this ->echoStrings [$ prefix ];
77
+
78
+ Settings::$ calculatePosition = function (Range $ range ) use ($ node , $ prefix , $ suffix ) {
70
79
if ($ range ->start ->line === 1 ) {
71
- $ range ->start ->character -= strlen ( $ safetyPrefix ) - 1 ;
72
- $ range ->end ->character -= strlen ( $ safetyPrefix ) - 1 ;
80
+ $ range ->start ->character += mb_strlen ( $ prefix ) ;
81
+ $ range ->end ->character += mb_strlen ( $ suffix ) ;
73
82
}
74
83
75
84
$ range ->start ->line += $ node ->position ->startLine - 2 ;
@@ -80,35 +89,42 @@ protected function parseBladeDirective(DirectiveNode $node)
80
89
81
90
$ result = Parse::parse ($ sourceFile );
82
91
83
- $ child = $ result ->children [0 ];
92
+ if (count ($ result ->children ) === 0 ) {
93
+ return ;
94
+ }
84
95
85
- $ child-> methodName = ' @ ' . substr ( $ child -> methodName , strlen ( $ safetyPrefix )) ;
96
+ $ child = $ result -> children [ 0 ] ;
86
97
87
98
$ this ->items [] = $ child ;
88
99
}
89
100
90
- protected function parseEchoNode ( EchoNode $ node )
101
+ protected function parseLiteralNode ( LiteralNode $ node )
91
102
{
92
- $ snippet = "<?php \n" . str_repeat (' ' , $ node ->getStartIndentationLevel ()) . $ node ->innerContent . '; ' ;
103
+ foreach ($ this ->echoStrings as $ prefix => $ suffix ) {
104
+ if (!str_starts_with ($ node ->content , $ prefix )) {
105
+ continue ;
106
+ }
93
107
94
- $ sourceFile = (new Parser )->parseSourceFile ($ snippet );
108
+ $ this ->doEchoParse ($ node , $ prefix , $ node ->content );
109
+ }
110
+ }
95
111
96
- Settings::$ calculatePosition = function (Range $ range ) use ($ node ) {
97
- $ prefix = match ($ node ->type ) {
98
- EchoType::RawEcho => '{!! ' ,
99
- EchoType::TripleEcho => '{{{ ' ,
100
- default => '{{ ' ,
101
- };
112
+ protected function parseBladeDirective (DirectiveNode $ node )
113
+ {
114
+ if ($ node ->isClosingDirective || !$ node ->hasArguments ()) {
115
+ return ;
116
+ }
102
117
103
- $ suffix = match ($ node ->type ) {
104
- EchoType::RawEcho => '!!} ' ,
105
- EchoType::TripleEcho => '}}} ' ,
106
- default => '}} ' ,
107
- };
118
+ $ methodUsed = '@ ' . $ node ->content ;
119
+ $ safetyPrefix = 'directive ' ;
120
+ $ snippet = "<?php \n" . str_repeat (' ' , $ node ->getStartIndentationLevel ()) . str_replace ($ methodUsed , $ safetyPrefix . $ node ->content , $ node ->toString () . '; ' );
108
121
122
+ $ sourceFile = (new Parser )->parseSourceFile ($ snippet );
123
+
124
+ Settings::$ calculatePosition = function (Range $ range ) use ($ node , $ safetyPrefix ) {
109
125
if ($ range ->start ->line === 1 ) {
110
- $ range ->start ->character += strlen ( $ prefix ) ;
111
- $ range ->end ->character += strlen ( $ suffix ) ;
126
+ $ range ->start ->character -= mb_strlen ( $ safetyPrefix ) - 1 ;
127
+ $ range ->end ->character -= mb_strlen ( $ safetyPrefix ) - 1 ;
112
128
}
113
129
114
130
$ range ->start ->line += $ node ->position ->startLine - 2 ;
@@ -119,12 +135,21 @@ protected function parseEchoNode(EchoNode $node)
119
135
120
136
$ result = Parse::parse ($ sourceFile );
121
137
122
- if (count ($ result ->children ) === 0 ) {
123
- return ;
124
- }
125
-
126
138
$ child = $ result ->children [0 ];
127
139
140
+ $ child ->methodName = '@ ' . substr ($ child ->methodName , mb_strlen ($ safetyPrefix ));
141
+
128
142
$ this ->items [] = $ child ;
129
143
}
144
+
145
+ protected function parseEchoNode (EchoNode $ node )
146
+ {
147
+ $ prefix = match ($ node ->type ) {
148
+ EchoType::RawEcho => '{!! ' ,
149
+ EchoType::TripleEcho => '{{{ ' ,
150
+ default => '{{ ' ,
151
+ };
152
+
153
+ $ this ->doEchoParse ($ node , $ prefix , $ node ->innerContent );
154
+ }
130
155
}
0 commit comments