From f1660ae0ba7205ade2df65d43c1d7ea3321dd52b Mon Sep 17 00:00:00 2001 From: Tomasz Kowalczyk Date: Sun, 25 Feb 2018 03:23:50 +0100 Subject: [PATCH] parameter and bbcode simple values now allow non-conflicting syntax tokens inside --- src/Parser/RegularParser.php | 29 +++++++++++++++++++++++++---- tests/ParserTest.php | 8 +++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Parser/RegularParser.php b/src/Parser/RegularParser.php index 8d799e0..f5edbc2 100644 --- a/src/Parser/RegularParser.php +++ b/src/Parser/RegularParser.php @@ -200,10 +200,9 @@ private function value() return $this->match(self::TOKEN_DELIMITER, false) ? $value : false; } - if($tmp = $this->match(self::TOKEN_STRING, false)) { - $value .= $tmp; - while($tmp = $this->match(self::TOKEN_STRING, false)) { - $value .= $tmp; + 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)))) { + $value .= $this->match(null, false); } return $value; @@ -252,6 +251,28 @@ private function lookahead($type) return $this->position < $this->tokensCount && $this->tokens[$this->position][0] === $type; } + private function lookaheadN(array $types) + { + $count = count($types); + if($this->position + $count > $this->tokensCount) { + return false; + } + + $position = $this->position; + foreach($types as $type) { + // note: automatically skips whitespace tokens + if($this->tokens[$position][0] === self::TOKEN_WS) { + $position++; + } + if($type !== $this->tokens[$position][0]) { + return false; + } + $position++; + } + + return true; + } + private function match($type, $ws) { if($this->position >= $this->tokensCount) { diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 3080aae..ea83106 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -225,6 +225,12 @@ public function provideShortcodes() new ParsedShortcode(new Shortcode('y', array(), ' ] [] [ [z] [/#] [/z] [ [] ] [/] ', null), '[y] ] [] [ [z] [/#] [/z] [ [] ] [/] [/y]', 27), new ParsedShortcode(new Shortcode('z', array(), ' [ [/ [/] /] ] ', null), '[z] [ [/ [/] /] ] [/z]', 70), )), + array($s, '[x=/[/] [y a=/"//] [z=http://url/] [a=http://url ]', 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), + )), ); /** @@ -239,7 +245,7 @@ public function provideShortcodes() * * Tests cases from array above with identifiers in the array below must be skipped. */ - $wordpressSkip = array(3, 6, 16, 21, 22, 23, 25, 32, 33, 34, 46, 47, 49); + $wordpressSkip = array(3, 6, 16, 21, 22, 23, 25, 32, 33, 34, 46, 47, 49, 51); $result = array(); foreach($tests as $key => $test) { $syntax = array_shift($test);