From 9351dfaa2540f286e4cddef16cc22b3c98089922 Mon Sep 17 00:00:00 2001 From: Tomasz Kowalczyk Date: Mon, 10 Dec 2018 00:54:33 +0100 Subject: [PATCH] WIP standard / aggressive value matching modes --- src/Parser/RegularParser.php | 16 +++++++++++++++- tests/ParserTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/Parser/RegularParser.php b/src/Parser/RegularParser.php index f5edbc2..43079f4 100644 --- a/src/Parser/RegularParser.php +++ b/src/Parser/RegularParser.php @@ -29,6 +29,11 @@ final class RegularParser implements ParserInterface const TOKEN_STRING = 6; const TOKEN_WS = 7; + const VALUE_REGULAR = 0x01; + const VALUE_AGGRESSIVE = 0x02; + + public $valueMode = self::VALUE_REGULAR; + public function __construct(SyntaxInterface $syntax = null) { $this->lexerRegex = $this->prepareLexer($syntax ?: new CommonSyntax()); @@ -201,7 +206,16 @@ private function value() } if($this->lookahead(self::TOKEN_STRING) || $this->lookahead(self::TOKEN_MARKER)) { - while(false === ($this->lookahead(self::TOKEN_WS) || $this->lookahead(self::TOKEN_CLOSE) || $this->lookaheadN(array(self::TOKEN_MARKER, self::TOKEN_CLOSE)))) { + while(true) { + if($this->lookahead(self::TOKEN_WS) || $this->lookahead(self::TOKEN_CLOSE)) { + break; + } + if($this->lookaheadN(array(self::TOKEN_MARKER, self::TOKEN_CLOSE))) { + if($this->valueMode === self::VALUE_AGGRESSIVE) { + $value .= $this->match(null, false); + } + break; + } $value .= $this->match(null, false); } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index ea83106..cb000e1 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -260,6 +260,30 @@ public function provideShortcodes() return $result; } + public function testValueModeAggressive() + { + $parser = new RegularParser(new CommonSyntax()); + $parser->valueMode = RegularParser::VALUE_AGGRESSIVE; + $parsed = $parser->parse('[x=/[/] [y a=/"//] [z=http://url/] [a=http://url ]'); + $tested = array( + new ParsedShortcode(new Shortcode('x', array(), null, '/[/'), '[x=/[/]', 0), + new ParsedShortcode(new Shortcode('y', array('a' => '/"//'), null, null), '[y a=/"//]', 8), + new ParsedShortcode(new Shortcode('z', array(), null, 'http://url/'), '[z=http://url/]', 19), + new ParsedShortcode(new Shortcode('a', array(), null, 'http://url'), '[a=http://url ]', 35), + ); + + $count = count($tested); + static::assertCount($count, $parsed, 'counts'); + for ($i = 0; $i < $count; $i++) { + static::assertSame($tested[$i]->getName(), $parsed[$i]->getName(), 'name'); + static::assertSame($tested[$i]->getParameters(), $parsed[$i]->getParameters(), 'parameters'); + static::assertSame($tested[$i]->getContent(), $parsed[$i]->getContent(), 'content'); + static::assertSame($tested[$i]->getText(), $parsed[$i]->getText(), 'text'); + static::assertSame($tested[$i]->getOffset(), $parsed[$i]->getOffset(), 'offset'); + static::assertSame($tested[$i]->getBbCode(), $parsed[$i]->getBbCode(), 'bbCode'); + } + } + public function testWordPress() { $parser = new WordpressParser();