From 4671f08fe05521d8f3d3d843f30576cb651ccd1b Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 1 May 2020 21:58:22 +0200 Subject: Fix cutting off whitespace in tag content --- src/ste/Parser.php | 77 +++++++++++----------- tests/test_short_for_additional_attribs/.gitignore | 3 + tests/test_short_for_additional_attribs/code.php | 12 ++++ tests/test_short_for_additional_attribs/test.tpl | 5 ++ tests/test_short_for_additional_attribs/want | 5 ++ 5 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 tests/test_short_for_additional_attribs/.gitignore create mode 100644 tests/test_short_for_additional_attribs/code.php create mode 100644 tests/test_short_for_additional_attribs/test.tpl create mode 100644 tests/test_short_for_additional_attribs/want 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 @@ +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 @@ + \ 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 @@ + -- cgit v1.2.3-70-g09d2