@@ -30,7 +30,10 @@ public function __construct(int $startingLine = 1)
30
30
$ this ->line = $ startingLine ;
31
31
}
32
32
33
- public function preLexComponents (string $ input ): string
33
+ /**
34
+ * @param bool $insideOfBlock Are we sub-parsing the content inside a block?
35
+ */
36
+ public function preLexComponents (string $ input , bool $ insideOfBlock = false ): string
34
37
{
35
38
$ this ->input = $ input ;
36
39
$ this ->length = \strlen ($ input );
@@ -53,6 +56,15 @@ public function preLexComponents(string $input): string
53
56
continue ;
54
57
}
55
58
59
+ // if we're already inside a component, and we're not inside a block,
60
+ // *and* we've just found a new component, then we should try to
61
+ // open the default block
62
+ if (!$ insideOfBlock
63
+ && !empty ($ this ->currentComponents )
64
+ && !$ this ->currentComponents [\count ($ this ->currentComponents ) - 1 ]['hasDefaultBlock ' ]) {
65
+ $ output .= $ this ->addDefaultBlock ();
66
+ }
67
+
56
68
$ attributes = $ this ->consumeAttributes ($ componentName );
57
69
$ isSelfClosing = $ this ->consume ('/> ' );
58
70
if (!$ isSelfClosing ) {
@@ -100,13 +112,12 @@ public function preLexComponents(string $input): string
100
112
++$ this ->line ;
101
113
}
102
114
103
- // handle adding a default block if needed
115
+ // handle adding a default block if we find non-whitespace outside of a block
104
116
if (!empty ($ this ->currentComponents )
105
117
&& !$ this ->currentComponents [\count ($ this ->currentComponents ) - 1 ]['hasDefaultBlock ' ]
106
118
&& preg_match ('/\S/ ' , $ char )
107
119
) {
108
- $ this ->currentComponents [\count ($ this ->currentComponents ) - 1 ]['hasDefaultBlock ' ] = true ;
109
- $ output .= '{% block content %} ' ;
120
+ $ output .= $ this ->addDefaultBlock ();
110
121
}
111
122
112
123
$ output .= $ char ;
@@ -202,10 +213,15 @@ private function consumeAttributes(string $componentName): string
202
213
return implode (', ' , $ attributes );
203
214
}
204
215
216
+ /**
217
+ * If the next character(s) exactly matches the given string, then
218
+ * consume it (move forward) and return true.
219
+ */
205
220
private function consume (string $ string ): bool
206
221
{
207
- if (substr ($ this ->input , $ this ->position , \strlen ($ string )) === $ string ) {
208
- $ this ->position += \strlen ($ string );
222
+ $ stringLength = \strlen ($ string );
223
+ if (substr ($ this ->input , $ this ->position , $ stringLength ) === $ string ) {
224
+ $ this ->position += $ stringLength ;
209
225
210
226
return true ;
211
227
}
@@ -317,7 +333,7 @@ private function consumeBlock(string $componentName): string
317
333
$ blockContents = $ this ->consumeUntilEndBlock ();
318
334
319
335
$ subLexer = new self ($ this ->line );
320
- $ output .= $ subLexer ->preLexComponents ($ blockContents );
336
+ $ output .= $ subLexer ->preLexComponents ($ blockContents, true );
321
337
322
338
$ this ->consume ($ closingTag );
323
339
$ output .= '{% endblock %} ' ;
@@ -395,4 +411,11 @@ private function doesStringEventuallyExist(string $needle): bool
395
411
396
412
return str_contains ($ remainingString , $ needle );
397
413
}
414
+
415
+ private function addDefaultBlock (): string
416
+ {
417
+ $ this ->currentComponents [\count ($ this ->currentComponents ) - 1 ]['hasDefaultBlock ' ] = true ;
418
+
419
+ return '{% block content %} ' ;
420
+ }
398
421
}
0 commit comments