Skip to content

Commit f3aa034

Browse files
committed
Isolate deduplicatePhpTags process
1 parent 2cdf939 commit f3aa034

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/Phug/Formatter/AbstractTwigFormat.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ protected function formatAttributes(MarkupElement $element)
9898
return $code;
9999
}
100100

101+
protected function deduplicatePhpTags($commentPattern, &$content, &$childContent)
102+
{
103+
// @codeCoverageIgnoreStart
104+
$content = preg_replace('/\\s\\?>$/', '', $content);
105+
$childContent = preg_replace('/^<\\?(?:php)?\\s/', '', $childContent);
106+
if ($commentPattern &&
107+
($pos = mb_strpos($childContent, $commentPattern)) !== false && (
108+
($end = mb_strpos($childContent, '?>')) === false ||
109+
$pos < $end
110+
) &&
111+
preg_match('/\\}\\s*$/', $content)
112+
) {
113+
$content = preg_replace(
114+
'/\\}\\s*$/',
115+
preg_replace('/\\?><\\?php(?:php)?(\s+\\?><\\?php(?:php)?)*/', '\\\\0', $childContent, 1),
116+
$content
117+
);
118+
$childContent = '';
119+
}
120+
// @codeCoverageIgnoreEnd
121+
}
122+
101123
protected function formatTwigChildElement($child, $previous, &$content, $commentPattern)
102124
{
103125
$childContent = $this->formatter->format($child);
@@ -106,22 +128,7 @@ protected function formatTwigChildElement($child, $previous, &$content, $comment
106128
$previous instanceof CodeElement &&
107129
$previous->isCodeBlock()
108130
) {
109-
$content = preg_replace('/\\s\\?>$/', '', $content);
110-
$childContent = preg_replace('/^<\\?(?:php)?\\s/', '', $childContent);
111-
if ($commentPattern &&
112-
($pos = mb_strpos($childContent, $commentPattern)) !== false && (
113-
($end = mb_strpos($childContent, '?>')) === false ||
114-
$pos < $end
115-
) &&
116-
preg_match('/\\}\\s*$/', $content)
117-
) {
118-
$content = preg_replace(
119-
'/\\}\\s*$/',
120-
preg_replace('/\\?><\\?php(?:php)?(\s+\\?><\\?php(?:php)?)*/', '\\\\0', $childContent, 1),
121-
$content
122-
);
123-
$childContent = '';
124-
}
131+
$this->deduplicatePhpTags($commentPattern, $content, $childContent);
125132
}
126133

127134
if (preg_match('/^\{% else/', $childContent)) {

tests/PugToTwigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function testConvert()
8989
' block',
9090
'section',
9191
' div: +foo()',
92-
' span bar'
92+
' span bar',
9393
]));
9494

9595
self::assertSame('<section><div><p>{{ myVar | e }}<span>bar</span></p></div></section>', $html);
@@ -102,7 +102,7 @@ public static function testConvert()
102102
' div',
103103
' block',
104104
'+foo()',
105-
' span bar'
105+
' span bar',
106106
]));
107107

108108
self::assertSame('{% if (true) %}<span>bar</span>{% else %}<div><span>bar</span></div>{% endif %}', $html);

0 commit comments

Comments
 (0)