Skip to content

Commit

Permalink
WIP standard / aggressive value matching modes
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderer committed Dec 9, 2018
1 parent f1660ae commit 9351dfa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Parser/RegularParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9351dfa

Please sign in to comment.