File: CommonMarkTestWeak.php

package info (click to toggle)
php-parsedown 2.0.0~beta-1-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 7,036 kB
  • sloc: php: 4,260; xml: 38; makefile: 14
file content (84 lines) | stat: -rw-r--r-- 2,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

namespace Erusev\Parsedown\Tests;

use Erusev\Parsedown\Html\Renderables\Element;
use PHPUnit\Framework\Attributes\RequiresPhpunit;

/**
 * Test Parsedown against the CommonMark spec, but less aggressive
 *
 * The resulting HTML markup is cleaned up before comparison, so examples
 * which would normally fail due to actually invisible differences (e.g.
 * superfluous whitespaces), don't fail. However, cleanup relies on block
 * element detection. The detection doesn't work correctly when a element's
 * `display` CSS property is manipulated. According to that this test is only
 * a interim solution on Parsedown's way to full CommonMark compatibility.
 *
 * @link http://commonmark.org/ CommonMark
 */
class CommonMarkTestWeak extends CommonMarkTestStrict
{
    /** @var string */
    protected $textLevelElementRegex;

    /**
     * @param string|null $name
     * @param array $data
     * @param string $dataName
     */
    public function __construct($name = null, array $data = [], $dataName = '')
    {
        $textLevelElements = \array_keys(Element::TEXT_LEVEL_ELEMENTS);

        $textLevelElements = \array_map(
            function ($e) { return \preg_quote($e, '/'); },
            $textLevelElements
        );

        $this->textLevelElementRegex = '\b(?:' . \implode('|', $textLevelElements) . ')\b';

        parent::__construct($name, $data, $dataName);
    }

    /**
     * @dataProvider data
     * @param int $_
     * @param string $__
     * @param string $markdown
     * @param string $expectedHtml
     * @return void
     * @throws \PHPUnit\Framework\AssertionFailedError
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
     */
    #[RequiresPhpunit('< 11')]
    public function testExample($_, $__, $markdown, $expectedHtml)
    {
        $expectedHtml = $this->cleanupHtml($expectedHtml);

        $actualHtml = $this->Parsedown->toHtml($markdown);
        $actualHtml = $this->cleanupHtml($actualHtml);

        $this->assertEquals($expectedHtml, $actualHtml);
    }

    /**
     * @param string $markup
     * @return string
     */
    protected function cleanupHtml($markup)
    {
        // invisible whitespaces at the beginning and end of block elements
        // however, whitespaces at the beginning of <pre> elements do matter
        $markup = \preg_replace(
            [
                '/(<(?!(?:' . $this->textLevelElementRegex . '|\bpre\b))\w+\b[^>]*>(?:<' . $this->textLevelElementRegex . '[^>]*>)*)\s+/s',
                '/\s+((?:<\/' . $this->textLevelElementRegex . '>)*<\/(?!' . $this->textLevelElementRegex . ')\w+\b>)/s'
            ],
            '$1',
            $markup
        );

        return $markup;
    }
}