*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Tests\Unit\Util;
use League\CommonMark\Exception\InvalidArgumentException;
use League\CommonMark\Extension\CommonMark\Node\Block\HtmlBlock;
use League\CommonMark\Util\RegexHelper;
use PHPUnit\Framework\TestCase;
/**
* Tests the different regular expressions
*/
final class RegexHelperTest extends TestCase
{
public function testEscapable(): void
{
$regex = '/^' . RegexHelper::PARTIAL_ESCAPABLE . '$/';
$this->assertRegexMatches($regex, '&');
$this->assertRegexMatches($regex, '/');
$this->assertRegexMatches($regex, '\\');
$this->assertRegexMatches($regex, '(');
$this->assertRegexMatches($regex, ')');
}
public function testEscapedChar(): void
{
$regex = '/^' . RegexHelper::PARTIAL_ESCAPED_CHAR . '$/';
$this->assertRegexMatches($regex, '\\&');
$this->assertRegexMatches($regex, '\\/');
$this->assertRegexMatches($regex, '\\\\');
$this->assertRegexMatches($regex, '\)');
$this->assertRegexMatches($regex, '\(');
}
public function testInDoubleQuotes(): void
{
$regex = '/^' . RegexHelper::PARTIAL_IN_DOUBLE_QUOTES . '$/';
$this->assertRegexMatches($regex, '"\\&"');
$this->assertRegexMatches($regex, '"\\/"');
$this->assertRegexMatches($regex, '"\\\\"');
}
public function testInSingleQuotes(): void
{
$regex = '/^' . RegexHelper::PARTIAL_IN_SINGLE_QUOTES . '$/';
$this->assertRegexMatches($regex, '\'\\&\'');
$this->assertRegexMatches($regex, '\'\\/\'');
$this->assertRegexMatches($regex, '\'\\\\\'');
}
public function testInParens(): void
{
$regex = '/^' . RegexHelper::PARTIAL_IN_PARENS . '$/';
$this->assertRegexMatches($regex, '(\\&)');
$this->assertRegexMatches($regex, '(\\/)');
$this->assertRegexMatches($regex, '(\\\\)');
}
public function testRegChar(): void
{
$regex = '/^' . RegexHelper::PARTIAL_REG_CHAR . '$/';
$this->assertRegexMatches($regex, 'a');
$this->assertRegexMatches($regex, 'A');
$this->assertRegexMatches($regex, '!');
$this->assertRegexDoesNotMatch($regex, ' ');
}
public function testInParensNoSp(): void
{
$regex = '/^' . RegexHelper::PARTIAL_IN_PARENS_NOSP . '$/';
$this->assertRegexMatches($regex, '(a)');
$this->assertRegexMatches($regex, '(A)');
$this->assertRegexMatches($regex, '(!)');
$this->assertRegexDoesNotMatch($regex, '(a )');
}
public function testTagname(): void
{
$regex = '/^' . RegexHelper::PARTIAL_TAGNAME . '$/';
$this->assertRegexMatches($regex, 'a');
$this->assertRegexMatches($regex, 'img');
$this->assertRegexMatches($regex, 'h1');
$this->assertRegexDoesNotMatch($regex, '11');
}
public function testBlockTagName(): void
{
$regex = '/^' . RegexHelper::PARTIAL_BLOCKTAGNAME . '$/';
$this->assertRegexMatches($regex, 'p');
$this->assertRegexMatches($regex, 'div');
$this->assertRegexMatches($regex, 'h1');
$this->assertRegexDoesNotMatch($regex, 'a');
$this->assertRegexDoesNotMatch($regex, 'h7');
}
public function testAttributeName(): void
{
$regex = '/^' . RegexHelper::PARTIAL_ATTRIBUTENAME . '$/';
$this->assertRegexMatches($regex, 'href');
$this->assertRegexMatches($regex, 'class');
$this->assertRegexMatches($regex, 'data-src');
$this->assertRegexDoesNotMatch($regex, '-key');
}
public function testUnquotedValue(): void
{
$regex = '/^' . RegexHelper::PARTIAL_UNQUOTEDVALUE . '$/';
$this->assertRegexMatches($regex, 'foo');
$this->assertRegexMatches($regex, 'bar');
$this->assertRegexDoesNotMatch($regex, '"baz"');
}
public function testSingleQuotedValue(): void
{
$regex = '/^' . RegexHelper::PARTIAL_SINGLEQUOTEDVALUE . '$/';
$this->assertRegexMatches($regex, '\'foo\'');
$this->assertRegexMatches($regex, '\'bar\'');
$this->assertRegexDoesNotMatch($regex, '"baz"');
}
public function testDoubleQuotedValue(): void
{
$regex = '/^' . RegexHelper::PARTIAL_DOUBLEQUOTEDVALUE . '$/';
$this->assertRegexMatches($regex, '"foo"');
$this->assertRegexMatches($regex, '"bar"');
$this->assertRegexDoesNotMatch($regex, '\'baz\'');
}
public function testAttributeValue(): void
{
$regex = '/^' . RegexHelper::PARTIAL_ATTRIBUTEVALUE . '$/';
$this->assertRegexMatches($regex, 'foo');
$this->assertRegexMatches($regex, '\'bar\'');
$this->assertRegexMatches($regex, '"baz"');
}
public function testAttributeValueSpec(): void
{
$regex = '/^' . RegexHelper::PARTIAL_ATTRIBUTEVALUESPEC . '$/';
$this->assertRegexMatches($regex, '=foo');
$this->assertRegexMatches($regex, '= foo');
$this->assertRegexMatches($regex, ' =foo');
$this->assertRegexMatches($regex, ' = foo');
$this->assertRegexMatches($regex, '=\'bar\'');
$this->assertRegexMatches($regex, '= \'bar\'');
$this->assertRegexMatches($regex, ' =\'bar\'');
$this->assertRegexMatches($regex, ' = \'bar\'');
$this->assertRegexMatches($regex, '="baz"');
$this->assertRegexMatches($regex, '= "baz"');
$this->assertRegexMatches($regex, ' ="baz"');
$this->assertRegexMatches($regex, ' = "baz"');
}
public function testAttribute(): void
{
$regex = '/^' . RegexHelper::PARTIAL_ATTRIBUTE . '$/';
$this->assertRegexMatches($regex, ' disabled');
$this->assertRegexMatches($regex, ' disabled="disabled"');
$this->assertRegexMatches($regex, ' href="http://www.google.com"');
$this->assertRegexDoesNotMatch($regex, 'disabled', 'There must be at least one space at the start');
}
public function testOpenTag(): void
{
$regex = '/^' . RegexHelper::PARTIAL_OPENTAG . '$/';
$this->assertRegexMatches($regex, '
');
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '
');
$this->assertRegexDoesNotMatch($regex, '');
}
public function testCloseTag(): void
{
$regex = '/^' . RegexHelper::PARTIAL_CLOSETAG . '$/';
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
$this->assertRegexDoesNotMatch($regex, '
');
$this->assertRegexDoesNotMatch($regex, '
');
}
public function testOpenBlockTag(): void
{
$regex = '/^' . RegexHelper::PARTIAL_OPENBLOCKTAG . '$/';
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '
');
$this->assertRegexMatches($regex, '
');
$this->assertRegexMatches($regex, '');
$this->assertRegexDoesNotMatch($regex, '', 'This is not a block element');
$this->assertRegexDoesNotMatch($regex, '
', 'This is not an opening tag');
}
public function testCloseBlockTag(): void
{
$regex = '/^' . RegexHelper::PARTIAL_CLOSEBLOCKTAG . '$/';
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
$this->assertRegexDoesNotMatch($regex, '', 'This is not a block element');
$this->assertRegexDoesNotMatch($regex, '
', 'This is not a closing tag');
}
public function testHtmlComment(): void
{
$regex = '/^' . RegexHelper::PARTIAL_HTMLCOMMENT . '$/';
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
$this->assertRegexDoesNotMatch($regex, '');
$this->assertRegexDoesNotMatch($regex, '');
$this->assertRegexDoesNotMatch($regex, '');
$this->assertRegexDoesNotMatch($regex, '');
}
public function testProcessingInstruction(): void
{
$regex = '/^' . RegexHelper::PARTIAL_PROCESSINGINSTRUCTION . '$/';
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
}
public function testDeclaration(): void
{
$regex = '/^' . RegexHelper::PARTIAL_DECLARATION . '$/';
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
}
public function testCDATA(): void
{
$regex = '/^' . RegexHelper::PARTIAL_CDATA . '$/';
$this->assertRegexMatches($regex, 'John Smith]]>');
$this->assertRegexMatches($regex, ']]>');
}
public function testHtmlTag(): void
{
$regex = '/^' . RegexHelper::PARTIAL_HTMLTAG . '$/';
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, 'John Smith]]>');
$this->assertRegexDoesNotMatch($regex, 'John Smith]]>');
}
public function testHtmlBlockOpen(): void
{
$regex = '/^' . RegexHelper::PARTIAL_HTMLBLOCKOPEN . '$/';
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
}
public function testLinkTitle(): void
{
$regex = '/^' . RegexHelper::PARTIAL_HTMLBLOCKOPEN . '$/';
$this->assertRegexMatches($regex, '');
$this->assertRegexMatches($regex, '');
}
public function testUnescape(): void
{
$this->assertEquals('foo(and(bar))', RegexHelper::unescape('foo(and\\(bar\\))'));
}
public function testIsEscapable(): void
{
$this->assertFalse(RegexHelper::isEscapable(''));
$this->assertFalse(RegexHelper::isEscapable('A'));
$this->assertTrue(RegexHelper::isEscapable('\\'));
}
/**
* @dataProvider dataForTestMatchAt
*/
public function testMatchAt(string $regex, string $string, ?int $offset, int $expectedResult): void
{
if ($offset === null) {
$this->assertEquals($expectedResult, RegexHelper::matchAt($regex, $string));
} else {
$this->assertEquals($expectedResult, RegexHelper::matchAt($regex, $string, $offset));
}
}
/**
* @return iterable>
*/
public function dataForTestMatchAt(): iterable
{
return [
['/ /', 'foo bar', null, 3],
['/ /', 'foo bar', 0, 3],
['/ /', 'foo bar', 1, 3],
['/ /', 'это тест', null, 3],
['/ /', 'это тест', 0, 3],
['/ /', 'это тест', 1, 3],
];
}
public function testMatchFirst(): void
{
$this->assertSame(null, RegexHelper::matchFirst('/^bar/', 'foobar'));
$this->assertSame(['bar'], RegexHelper::matchFirst('/^bar/', 'foobar', 3));
$this->assertSame(['bar', 'bar'], RegexHelper::matchFirst('/^(bar)/', 'foobar', 3));
$this->assertSame(['bar', 'ar'], RegexHelper::matchFirst('/^b(.+)/', 'foobar', 3));
$this->assertSame(['brown fox', 'brown', 'fox'], RegexHelper::matchFirst('/(quick|brown|lazy) (fox|dog)/', 'The quick brown fox jumps over the lazy dog'));
}
/**
* @dataProvider blockTypesWithValidOpenerRegexes
*/
public function testValidHtmlBlockOpenRegex(int $type): void
{
$this->assertNotEmpty(RegexHelper::getHtmlBlockOpenRegex($type));
}
/**
* @return iterable
*/
public function blockTypesWithValidOpenerRegexes(): iterable
{
yield [HtmlBlock::TYPE_1_CODE_CONTAINER];
yield [HtmlBlock::TYPE_2_COMMENT];
yield [HtmlBlock::TYPE_3];
yield [HtmlBlock::TYPE_4];
yield [HtmlBlock::TYPE_5_CDATA];
yield [HtmlBlock::TYPE_6_BLOCK_ELEMENT];
yield [HtmlBlock::TYPE_7_MISC_ELEMENT];
}
public function testInvalidHtmlBlockOpenRegex(): void
{
$this->expectException(InvalidArgumentException::class);
RegexHelper::getHtmlBlockOpenRegex(8);
}
/**
* @dataProvider blockTypesWithValidCloserRegexes
*/
public function testValidHtmlBlockCloseRegex(int $type): void
{
$this->assertNotEmpty(RegexHelper::getHtmlBlockOpenRegex($type));
}
/**
* @return iterable
*/
public function blockTypesWithValidCloserRegexes(): iterable
{
yield [HtmlBlock::TYPE_1_CODE_CONTAINER];
yield [HtmlBlock::TYPE_2_COMMENT];
yield [HtmlBlock::TYPE_3];
yield [HtmlBlock::TYPE_4];
yield [HtmlBlock::TYPE_5_CDATA];
}
/**
* @dataProvider blockTypesWithInvalidCloserRegexes
*/
public function testInvalidHtmlBlockCloseRegex(int $type): void
{
$this->expectException(InvalidArgumentException::class);
RegexHelper::getHtmlBlockCloseRegex($type);
}
/**
* @return iterable
*/
public function blockTypesWithInvalidCloserRegexes(): iterable
{
yield [HtmlBlock::TYPE_6_BLOCK_ELEMENT];
yield [HtmlBlock::TYPE_7_MISC_ELEMENT];
yield [8];
}
private function assertRegexMatches(string $pattern, string $string, string $message = ''): void
{
if (\method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression($pattern, $string, $message);
} else {
$this->assertRegExp($pattern, $string, $message);
}
}
private function assertRegexDoesNotMatch(string $pattern, string $string, string $message = ''): void
{
if (\method_exists($this, 'assertDoesNotMatchRegularExpression')) {
$this->assertDoesNotMatchRegularExpression($pattern, $string, $message);
} else {
$this->assertNotRegExp($pattern, $string, $message);
}
}
}