From 73227dda28c6e5cd395c7fdf0feb9b78e9fb6b59 Mon Sep 17 00:00:00 2001 From: Wilhelm Behncke <2522299+grebaldi@users.noreply.github.com> Date: Thu, 8 Mar 2018 18:28:07 +0100 Subject: [PATCH 1/3] TASK: Write tests for props containing tags --- tests/ParserTest.php | 78 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/tests/ParserTest.php b/tests/ParserTest.php index eaa21de..bce8aae 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -642,6 +642,84 @@ public function shouldHandleWhitespace() ); } + /** + * @test + */ + public function shouldParsePropsContainingTags() + { + $parser = new Parser('Hello World!}/>'); + + $this->assertEquals( + [ + [ + 'type' => 'node', + 'payload' => [ + 'identifier' => 'collection', + 'props' => [ + 'itemRenderer' => [ + 'type' => 'node', + 'payload' => [ + 'identifier' => 'li', + 'props' => [], + 'children' => [ + [ + 'type' => 'text', + 'payload' => 'Hello World!' + ] + ], + 'selfClosing' => false + ] + ] + ], + 'children' => [], + 'selfClosing' => true + ] + ] + ], + $parser->parse() + ); + } + + /** + * @test + */ + public function shouldParsePropsContainingTagsWithLineBreaks() + { + $parser = new Parser('Foo Bar + }/>'); + + $this->assertEquals( + [ + [ + 'type' => 'node', + 'payload' => [ + 'identifier' => 'collection', + 'props' => [ + 'itemRenderer' => [ + 'type' => 'node', + 'payload' => [ + 'identifier' => 'div', + 'props' => [], + 'children' => [ + [ + 'type' => 'text', + 'payload' => 'Foo Bar' + ] + ], + 'selfClosing' => false + ] + ] + ], + 'children' => [], + 'selfClosing' => true + ] + ] + ], + $parser->parse() + ); + } + /** * @test * @expectedException \PackageFactory\Afx\Exception From db422a4b78a7b7f26ff4c53a2b91ba487e59b297 Mon Sep 17 00:00:00 2001 From: Wilhelm Behncke <2522299+grebaldi@users.noreply.github.com> Date: Thu, 8 Mar 2018 18:52:47 +0100 Subject: [PATCH 2/3] FEATURE: Parse tags inside of props --- src/Expression/Expression.php | 15 ++++++++++++++- src/Expression/Prop.php | 13 +++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Expression/Expression.php b/src/Expression/Expression.php index ec65a78..930f9bf 100644 --- a/src/Expression/Expression.php +++ b/src/Expression/Expression.php @@ -3,6 +3,7 @@ use PackageFactory\Afx\Exception; use PackageFactory\Afx\Lexer; +use PackageFactory\Afx\Parser; class Expression { @@ -29,7 +30,7 @@ public static function parse(Lexer $lexer) if ($lexer->isClosingBrace()) { if ($braceCount === 0) { $lexer->consume(); - return $contents; + return self::postProcessContents($contents); } $braceCount--; @@ -38,4 +39,16 @@ public static function parse(Lexer $lexer) $contents .= $lexer->consume(); } } + + protected static function postProcessContents($contents) + { + $trimmedContents = trim($contents); + + if ($trimmedContents{0} === '<') { + $parser = new Parser($trimmedContents); + return $parser->parse()[0]; + } + + return $contents; + } } diff --git a/src/Expression/Prop.php b/src/Expression/Prop.php index b90cd75..7ed0baa 100644 --- a/src/Expression/Prop.php +++ b/src/Expression/Prop.php @@ -26,10 +26,15 @@ public static function parse(Lexer $lexer) break; case $lexer->isOpeningBrace(): - $value = [ - 'type' => 'expression', - 'payload' => Expression::parse($lexer) - ]; + $expression = Expression::parse($lexer); + if (is_string($expression)) { + $value = [ + 'type' => 'expression', + 'payload' => $expression + ]; + } else { + $value = $expression; + } break; default: throw new Exception(sprintf( From 9c0fe313557fbe09a301713ecfb266dcfd54cc53 Mon Sep 17 00:00:00 2001 From: Wilhelm Behncke <2522299+grebaldi@users.noreply.github.com> Date: Thu, 8 Mar 2018 18:56:40 +0100 Subject: [PATCH 3/3] TASK: Fix whitespaces --- src/Expression/Expression.php | 18 +++++++++--------- src/Expression/Prop.php | 16 ++++++++-------- tests/ParserTest.php | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Expression/Expression.php b/src/Expression/Expression.php index 930f9bf..49cddbd 100644 --- a/src/Expression/Expression.php +++ b/src/Expression/Expression.php @@ -40,15 +40,15 @@ public static function parse(Lexer $lexer) } } - protected static function postProcessContents($contents) - { - $trimmedContents = trim($contents); + protected static function postProcessContents($contents) + { + $trimmedContents = trim($contents); - if ($trimmedContents{0} === '<') { - $parser = new Parser($trimmedContents); - return $parser->parse()[0]; - } + if ($trimmedContents{0} === '<') { + $parser = new Parser($trimmedContents); + return $parser->parse()[0]; + } - return $contents; - } + return $contents; + } } diff --git a/src/Expression/Prop.php b/src/Expression/Prop.php index 7ed0baa..ad3ae3a 100644 --- a/src/Expression/Prop.php +++ b/src/Expression/Prop.php @@ -27,14 +27,14 @@ public static function parse(Lexer $lexer) case $lexer->isOpeningBrace(): $expression = Expression::parse($lexer); - if (is_string($expression)) { - $value = [ - 'type' => 'expression', - 'payload' => $expression - ]; - } else { - $value = $expression; - } + if (is_string($expression)) { + $value = [ + 'type' => 'expression', + 'payload' => $expression + ]; + } else { + $value = $expression; + } break; default: throw new Exception(sprintf( diff --git a/tests/ParserTest.php b/tests/ParserTest.php index bce8aae..152872d 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -686,8 +686,8 @@ public function shouldParsePropsContainingTags() public function shouldParsePropsContainingTagsWithLineBreaks() { $parser = new Parser('Foo Bar - }/>'); +
Foo Bar
+ }/>'); $this->assertEquals( [