Skip to content

Commit c08ebdf

Browse files
authored
Merge pull request #120 from sanmai/pr/document-whitespace-control
AbstractBlock: document changes, add an extra test
2 parents 3ae052a + f7890df commit c08ebdf

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/Liquid/AbstractBlock.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ class AbstractBlock extends AbstractTag
2222
const TAG_PREFIX = '\Liquid\Tag\Tag';
2323

2424
/**
25-
* @var AbstractTag[]
25+
* @var AbstractTag[]|Variable[]|string[]
2626
*/
2727
protected $nodelist = array();
2828

2929
/**
30+
* Whenever next token should be ltrim'med.
31+
*
3032
* @var bool
3133
*/
3234
protected static $trimWhitespace = false;
@@ -92,6 +94,7 @@ public function parse(array &$tokens)
9294
$this->whitespaceHandler($token);
9395
$this->nodelist[] = $this->createVariable($token);
9496
} else {
97+
// This is neither a tag or a variable, proceed with an ltrim
9598
if (self::$trimWhitespace) {
9699
$token = ltrim($token);
97100
}
@@ -109,16 +112,24 @@ public function parse(array &$tokens)
109112
*
110113
* @param string $token
111114
*/
112-
protected function whitespaceHandler(&$token)
115+
protected function whitespaceHandler($token)
113116
{
117+
/*
118+
* This assumes that TAG_START is always '{%', and a whitespace control indicator
119+
* is exactly one character long, on a third position.
120+
*/
114121
if (mb_substr($token, 2, 1) === Liquid::get('WHITESPACE_CONTROL')) {
115122
$previousToken = end($this->nodelist);
116-
if (is_string($previousToken)) {
123+
if (is_string($previousToken)) { // this can also be a tag or a variable
117124
$this->nodelist[key($this->nodelist)] = rtrim($previousToken);
118125
}
119126
}
120127

121-
self::$trimWhitespace = (mb_substr($token, -3, 1) === Liquid::get('WHITESPACE_CONTROL'));
128+
/*
129+
* This assumes that TAG_END is always '%}', and a whitespace control indicator
130+
* is exactly one character long, on a third position from the end.
131+
*/
132+
self::$trimWhitespace = mb_substr($token, -3, 1) === Liquid::get('WHITESPACE_CONTROL');
122133
}
123134

124135
/**

tests/Liquid/AbstractBlockTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,18 @@ public function testUnterminatedBlockError()
2222
{
2323
$this->assertTemplateResult('', '{% block }');
2424
}
25+
26+
public function testWhitespaceHandler()
27+
{
28+
$this->assertTemplateResult('foo', '{% if true %}foo{% endif %}');
29+
$this->assertTemplateResult(' foo ', '{% if true %} foo {% endif %}');
30+
$this->assertTemplateResult(' foo ', ' {% if true %} foo {% endif %} ');
31+
$this->assertTemplateResult('foo ', '{% if true -%} foo {% endif %}');
32+
$this->assertTemplateResult('foo', '{% if true -%} foo {%- endif %}');
33+
$this->assertTemplateResult('foo', ' {%- if true -%} foo {%- endif %}');
34+
$this->assertTemplateResult('foo', ' {%- if true -%} foo {%- endif -%} ');
35+
$this->assertTemplateResult('foo', ' {%- if true -%} foo {%- endif -%} {%- if false -%} bar {%- endif -%} ');
36+
$this->assertTemplateResult('foobar', ' {%- if true -%} foo {%- endif -%} {%- if true -%} bar {%- endif -%} ');
37+
$this->assertTemplateResult('-> foo', '{% if true %}-> {% endif %} {%- if true -%} foo {%- endif -%}');
38+
}
2539
}

0 commit comments

Comments
 (0)