diff --git a/src/Expression/Expression.php b/src/Expression/Expression.php index ec65a78..49cddbd 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..ad3ae3a 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( diff --git a/tests/ParserTest.php b/tests/ParserTest.php index eaa21de..152872d 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