From 859933d1803ff73ffa85443916b5cb4453c049c8 Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Sun, 25 Mar 2018 20:29:17 -0400 Subject: [PATCH 1/2] Don't allow escaped spaces in link destination. See commonmark/CommonMark#493 and commonmark/commonmark.js@f0da145 --- src/Inline/Parser/EscapableParser.php | 4 +--- src/Util/LinkParserHelper.php | 2 +- src/Util/RegexHelper.php | 14 ++++++++++++++ tests/unit/Util/RegexHelperTest.php | 7 +++++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Inline/Parser/EscapableParser.php b/src/Inline/Parser/EscapableParser.php index bfd8802f07..3f18b04e6e 100644 --- a/src/Inline/Parser/EscapableParser.php +++ b/src/Inline/Parser/EscapableParser.php @@ -48,9 +48,7 @@ public function parse(InlineParserContext $inlineContext) $inlineContext->getContainer()->appendChild(new Newline(Newline::HARDBREAK)); return true; - } elseif ($nextChar !== null && - preg_match('/' . RegexHelper::PARTIAL_ESCAPABLE . '/', $nextChar) - ) { + } elseif (RegexHelper::isEscapable($nextChar)) { $cursor->advanceBy(2); $inlineContext->getContainer()->appendChild(new Text($nextChar)); diff --git a/src/Util/LinkParserHelper.php b/src/Util/LinkParserHelper.php index 116a921e5e..36f9a6bea1 100644 --- a/src/Util/LinkParserHelper.php +++ b/src/Util/LinkParserHelper.php @@ -37,7 +37,7 @@ public static function parseLinkDestination(Cursor $cursor) $oldState = $cursor->saveState(); $openParens = 0; while (($c = $cursor->getCharacter()) !== null) { - if ($c === '\\') { + if ($c === '\\' && RegexHelper::isEscapable($cursor->peek())) { $cursor->advanceBy(2); } elseif ($c === '(') { $cursor->advance(); diff --git a/src/Util/RegexHelper.php b/src/Util/RegexHelper.php index 4a8ea4b81f..43e2a738a7 100644 --- a/src/Util/RegexHelper.php +++ b/src/Util/RegexHelper.php @@ -174,6 +174,20 @@ public static function getInstance() return self::$instance; } + /** + * @param string|null $character + * + * @return bool + */ + public static function isEscapable($character) + { + if ($character === null) { + return false; + } + + return preg_match('/' . self::PARTIAL_ESCAPABLE . '/', $character) === 1; + } + /** * Returns a partial regex * diff --git a/tests/unit/Util/RegexHelperTest.php b/tests/unit/Util/RegexHelperTest.php index 1d3950562a..cda5448106 100644 --- a/tests/unit/Util/RegexHelperTest.php +++ b/tests/unit/Util/RegexHelperTest.php @@ -269,6 +269,13 @@ public function testUnescape() $this->assertEquals('foo(and(bar))', RegexHelper::unescape('foo(and\\(bar\\))')); } + public function testIsEscapable() + { + $this->assertFalse(RegexHelper::isEscapable(null)); + $this->assertFalse(RegexHelper::isEscapable('A')); + $this->assertTrue(RegexHelper::isEscapable('\\')); + } + /** * @param $regex * @param $string From f8d0d708093031d784da7f21dcd5800db474397c Mon Sep 17 00:00:00 2001 From: Colin O'Dell Date: Sun, 25 Mar 2018 21:23:15 -0400 Subject: [PATCH 2/2] Prepare for 0.17.2 release --- CHANGELOG.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 677a2be1a5..84b14c53d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip ## [Unreleased][unreleased] +## [0.17.2] - 2017-03-25 + +### Added + + - Added new `RegexHelper::isEscapable()` method + +### Fixed + + - Fixed spec compliance bug where escaped spaces should not be allowed in link destinations + ## [0.17.1] - 2017-03-18 ### Added @@ -615,7 +625,8 @@ An unused constant and static method were deprecated and will be removed in a fu ### Added - Initial commit (compatible with jgm/stmd:spec.txt @ 0275f34) -[unreleased]: https://github.com/thephpleague/commonmark/compare/0.17.1...HEAD +[unreleased]: https://github.com/thephpleague/commonmark/compare/0.17.2...HEAD +[0.17.2]: https://github.com/thephpleague/commonmark/compare/0.17.1...0.17.2 [0.17.1]: https://github.com/thephpleague/commonmark/compare/0.17.0...0.17.1 [0.17.0]: https://github.com/thephpleague/commonmark/compare/0.16.0...0.17.0 [0.16.0]: https://github.com/thephpleague/commonmark/compare/0.15.7...0.16.0