summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ste/Parser.php77
-rw-r--r--tests/test_short_for_additional_attribs/.gitignore3
-rw-r--r--tests/test_short_for_additional_attribs/code.php12
-rw-r--r--tests/test_short_for_additional_attribs/test.tpl5
-rw-r--r--tests/test_short_for_additional_attribs/want5
5 files changed, 64 insertions, 38 deletions
diff --git a/src/ste/Parser.php b/src/ste/Parser.php
index 6e7bf62..e786396 100644
--- a/src/ste/Parser.php
+++ b/src/ste/Parser.php
@@ -198,60 +198,61 @@ class Parser
* @param ASTNode[] $ast
* @return ASTNode[]
*/
- private static function tidyup_ast($ast)
+ private static function combine_consecutive_text(array $ast)
{
$out = array();
- /** @var TextNode|null $prevtext */
- $prevtext = null;
- $first = true;
+ /** @var TextNode|null $last_text */
+ $last_text = null;
+
+ $put_last_text = static function () use (&$out, &$last_text) {
+ if ($last_text === null) {
+ return;
+ }
+
+ if ($last_text->text !== "") {
+ $out[] = $last_text;
+ }
+
+ $last_text = null;
+ };
foreach ($ast as $node) {
if ($node instanceof TextNode) {
- if ($prevtext === null) {
- $prevtext = $node;
+ if ($last_text !== null) {
+ $last_text->text .= $node->text;
} else {
- $prevtext->text .= $node->text;
+ $last_text = $node;
}
} else {
- if ($prevtext !== null) {
- if ($first) {
- $prevtext->text = ltrim($prevtext->text);
- }
- if ($prevtext->text != "") {
- $out[] = $prevtext;
- }
- }
- $prevtext = null;
- $first = false;
-
- if ($node instanceof TagNode) {
- $node->sub = self::tidyup_ast($node->sub);
- foreach ($node->params as $k => &$v) {
- $v = self::tidyup_ast($v);
- }
- unset($v);
- } else { /* VariableNode */
- foreach ($node->arrayfields as &$v) {
- $v = self::tidyup_ast($v);
- }
- unset($v);
- }
-
+ $put_last_text();
$out[] = $node;
}
}
- if ($prevtext !== null) {
- if ($first) {
- $prevtext->text = ltrim($prevtext->text);
- }
- if ($prevtext->text != "") {
- $out[] = $prevtext;
+ $put_last_text();
+
+ return $out;
+ }
+
+ /**
+ * @param ASTNode[] $ast
+ * @return ASTNode[]
+ */
+ private static function tidyup_ast(array $ast)
+ {
+ foreach ($ast as $node) {
+ if ($node instanceof TagNode) {
+ $node->sub = self::tidyup_ast($node->sub);
+ $node->params = array_map(array(__CLASS__, 'tidyup_ast'), $node->params);
+ } elseif ($node instanceof VariableNode) {
+ $node->arrayfields = array_map(array(__CLASS__, 'tidyup_ast'), $node->arrayfields);
}
}
- return $out;
+ $ast = self::combine_consecutive_text($ast);
+
+ return $ast;
}
/**
diff --git a/tests/test_short_for_additional_attribs/.gitignore b/tests/test_short_for_additional_attribs/.gitignore
new file mode 100644
index 0000000..de2a41b
--- /dev/null
+++ b/tests/test_short_for_additional_attribs/.gitignore
@@ -0,0 +1,3 @@
+have
+*.ast
+*.transc.php
diff --git a/tests/test_short_for_additional_attribs/code.php b/tests/test_short_for_additional_attribs/code.php
new file mode 100644
index 0000000..5d75d34
--- /dev/null
+++ b/tests/test_short_for_additional_attribs/code.php
@@ -0,0 +1,12 @@
+<?php
+
+use kch42\ste\STECore;
+
+function test_func(STECore $ste)
+{
+ $ste->set_var_by_name("data", array(
+ array('content' => 'foo', 'foo' => true),
+ array('content' => 'bar', 'foo' => false),
+ array('content' => 'baz', 'foo' => false),
+ ));
+}
diff --git a/tests/test_short_for_additional_attribs/test.tpl b/tests/test_short_for_additional_attribs/test.tpl
new file mode 100644
index 0000000..e6b30e4
--- /dev/null
+++ b/tests/test_short_for_additional_attribs/test.tpl
@@ -0,0 +1,5 @@
+<ul>
+ <ste:foreach array="data" value="x">
+ <li?{$x[foo]| class="foo"|}>$x[content]</li>
+ </ste:foreach>
+</ul> \ No newline at end of file
diff --git a/tests/test_short_for_additional_attribs/want b/tests/test_short_for_additional_attribs/want
new file mode 100644
index 0000000..c46d63f
--- /dev/null
+++ b/tests/test_short_for_additional_attribs/want
@@ -0,0 +1,5 @@
+<ul>
+<li class="foo">foo</li>
+<li>bar</li>
+<li>baz</li>
+</ul>