@@ -26,6 +26,11 @@ class AbstractBlock extends AbstractTag
26
26
*/
27
27
protected $ nodelist = array ();
28
28
29
+ /**
30
+ * @var bool
31
+ */
32
+ protected static $ trimWhitespace = false ;
33
+
29
34
/**
30
35
* @return array
31
36
*/
@@ -45,7 +50,7 @@ public function getNodelist()
45
50
public function parse (array &$ tokens )
46
51
{
47
52
$ startRegexp = new Regexp ('/^ ' . Liquid::get ('TAG_START ' ) . '/ ' );
48
- $ tagRegexp = new Regexp ('/^ ' . Liquid::get ('TAG_START ' ) . ' \s*(\w+)\s*(.*?) ' . Liquid::get ('TAG_END ' ) . '$/ ' );
53
+ $ tagRegexp = new Regexp ('/^ ' . Liquid::get ('TAG_START ' ) . Liquid:: get ( ' WHITESPACE_CONTROL ' ) . ' ? \s*(\w+)\s*(.*?)' . Liquid:: get ( ' WHITESPACE_CONTROL ' ) . ' ? ' . Liquid::get ('TAG_END ' ) . '$/ ' );
49
54
$ variableStartRegexp = new Regexp ('/^ ' . Liquid::get ('VARIABLE_START ' ) . '/ ' );
50
55
51
56
$ this ->nodelist = array ();
@@ -56,6 +61,7 @@ public function parse(array &$tokens)
56
61
$ token = array_shift ($ tokens );
57
62
58
63
if ($ startRegexp ->match ($ token )) {
64
+ $ this ->whitespaceHandler ($ token );
59
65
if ($ tagRegexp ->match ($ token )) {
60
66
// If we found the proper block delimitor just end parsing here and let the outer block proceed
61
67
if ($ tagRegexp ->matches [1 ] == $ this ->blockDelimiter ()) {
@@ -83,15 +89,38 @@ public function parse(array &$tokens)
83
89
throw new ParseException ("Tag $ token was not properly terminated (won't match $ tagRegexp) " );
84
90
}
85
91
} elseif ($ variableStartRegexp ->match ($ token )) {
92
+ $ this ->whitespaceHandler ($ token );
86
93
$ this ->nodelist [] = $ this ->createVariable ($ token );
87
- } elseif ($ token != '' ) {
94
+ } else {
95
+ if (self ::$ trimWhitespace ) {
96
+ $ token = ltrim ($ token );
97
+ }
98
+
99
+ self ::$ trimWhitespace = false ;
88
100
$ this ->nodelist [] = $ token ;
89
101
}
90
102
}
91
103
92
104
$ this ->assertMissingDelimitation ();
93
105
}
94
106
107
+ /**
108
+ * Handle the whitespace.
109
+ *
110
+ * @param string $token
111
+ */
112
+ protected function whitespaceHandler (&$ token )
113
+ {
114
+ if (mb_substr ($ token , 2 , 1 ) === Liquid::get ('WHITESPACE_CONTROL ' )) {
115
+ $ previousToken = end ($ this ->nodelist );
116
+ if (is_string ($ previousToken )) {
117
+ $ this ->nodelist [key ($ this ->nodelist )] = rtrim ($ previousToken );
118
+ }
119
+ }
120
+
121
+ self ::$ trimWhitespace = (mb_substr ($ token , -3 , 1 ) === Liquid::get ('WHITESPACE_CONTROL ' ));
122
+ }
123
+
95
124
/**
96
125
* Render the block.
97
126
*
@@ -214,7 +243,7 @@ private function blockName()
214
243
*/
215
244
private function createVariable ($ token )
216
245
{
217
- $ variableRegexp = new Regexp ('/^ ' . Liquid::get ('VARIABLE_START ' ) . ' (.*) ' . Liquid::get ('VARIABLE_END ' ) . '$/ ' );
246
+ $ variableRegexp = new Regexp ('/^ ' . Liquid::get ('VARIABLE_START ' ) . Liquid:: get ( ' WHITESPACE_CONTROL ' ) . ' ? (.*?) ' . Liquid:: get ( ' WHITESPACE_CONTROL ' ) . ' ? ' . Liquid::get ('VARIABLE_END ' ) . '$/ ' );
218
247
if ($ variableRegexp ->match ($ token )) {
219
248
return new Variable ($ variableRegexp ->matches [1 ]);
220
249
}
0 commit comments