protected function stripWhitespace(&$tokens, $position)
{
switch ($tokens[$position][0]) {
case self::TOKEN_PLACEHOLDER:
case self::TOKEN_SECTION:
case self::TOKEN_SECTION_INVERT:
// Analyze first token of section, and strip leading newlines
$sectionTokens = $tokens[$position][1]['content'];
if (0 === count($sectionTokens)) {
break;
}
$token = $sectionTokens[0];
if ($token[0] === self::TOKEN_CONTENT) {
$content = preg_replace('/^\\s*?(\\r\\n?|\\n)/s', '', $token[1]);
$token = array(self::TOKEN_CONTENT, $content, 'original_content' => $token[1]);
$sectionTokens[0] = $token;
$tokens[$position][1]['content'] = $sectionTokens;
break;
}
break;
default:
break;
}
// Analyze preceding token; if content token, and ending with a newline
// and optionally whitespace, trim the whitespace.
if ($position - 1 > -1) {
$previous = $tokens[$position - 1];
$type = $previous[0];
if ($type === self::TOKEN_CONTENT) {
$content = preg_replace('/(\\r\\n?|\\n)\\s+$/s', '$1', $previous[1]);
$previous = array(self::TOKEN_CONTENT, $content, 'original_content' => $previous[1]);
$tokens[$position - 1] = $previous;
}
}
// Analyze next token. If it is a content token, and begins with optional
// whitespace, followed by a newline, trim this whitespace.
if (isset($tokens[$position + 1])) {
$next = $tokens[$position + 1];
$type = $next[0];
if ($type === self::TOKEN_CONTENT) {
$content = preg_replace('/^\\s*?(\\r\\n?|\\n)/s', '', $next[1]);
$next = array(self::TOKEN_CONTENT, $content, 'original_content' => $next[1]);
$tokens[$position + 1] = $next;
}
}
}