RegexHelper::getHtmlBlockOpenRegex()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 13.3593

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 8
nop 1
dl 0
loc 19
ccs 9
cts 16
cp 0.5625
crap 13.3593
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Util;
18
19
use League\CommonMark\Exception\InvalidArgumentException;
20
use League\CommonMark\Extension\CommonMark\Node\Block\HtmlBlock;
21
22
/**
23
 * Provides regular expressions and utilities for parsing Markdown
24
 *
25
 * All of the PARTIAL_ regex constants assume that they'll be used in case-insensitive searches
26
 * All other complete regexes provided by this class (either via constants or methods) will have case-insensitivity enabled.
27
 *
28
 * @phpcs:disable Generic.Strings.UnnecessaryStringConcat.Found
29
 *
30
 * @psalm-immutable
31
 */
32
final class RegexHelper
33
{
34
    // Partial regular expressions (wrap with `/` on each side and add the case-insensitive `i` flag before use)
35
    public const PARTIAL_ENTITY                = '&(?:#x[a-f0-9]{1,6}|#[0-9]{1,7}|[a-z][a-z0-9]{1,31});';
36
    public const PARTIAL_ESCAPABLE             = '[!"#$%&\'()*+,.\/:;<=>?@[\\\\\]^_`{|}~-]';
37
    public const PARTIAL_ESCAPED_CHAR          = '\\\\' . self::PARTIAL_ESCAPABLE;
38
    public const PARTIAL_IN_DOUBLE_QUOTES      = '"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"';
39
    public const PARTIAL_IN_SINGLE_QUOTES      = '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\'';
40
    public const PARTIAL_IN_PARENS             = '\\((' . self::PARTIAL_ESCAPED_CHAR . '|[^)\x00])*\\)';
41
    public const PARTIAL_REG_CHAR              = '[^\\\\()\x00-\x20]';
42
    public const PARTIAL_IN_PARENS_NOSP        = '\((' . self::PARTIAL_REG_CHAR . '|' . self::PARTIAL_ESCAPED_CHAR . '|\\\\)*\)';
43
    public const PARTIAL_TAGNAME               = '[a-z][a-z0-9-]*';
44
    public const PARTIAL_BLOCKTAGNAME          = '(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h1|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)';
45
    public const PARTIAL_ATTRIBUTENAME         = '[a-z_:][a-z0-9:._-]*';
46
    public const PARTIAL_UNQUOTEDVALUE         = '[^"\'=<>`\x00-\x20]+';
47
    public const PARTIAL_SINGLEQUOTEDVALUE     = '\'[^\']*\'';
48
    public const PARTIAL_DOUBLEQUOTEDVALUE     = '"[^"]*"';
49
    public const PARTIAL_ATTRIBUTEVALUE        = '(?:' . self::PARTIAL_UNQUOTEDVALUE . '|' . self::PARTIAL_SINGLEQUOTEDVALUE . '|' . self::PARTIAL_DOUBLEQUOTEDVALUE . ')';
50
    public const PARTIAL_ATTRIBUTEVALUESPEC    = '(?:' . '\s*=' . '\s*' . self::PARTIAL_ATTRIBUTEVALUE . ')';
51
    public const PARTIAL_ATTRIBUTE             = '(?:' . '\s+' . self::PARTIAL_ATTRIBUTENAME . self::PARTIAL_ATTRIBUTEVALUESPEC . '?)';
52
    public const PARTIAL_OPENTAG               = '<' . self::PARTIAL_TAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>';
53
    public const PARTIAL_CLOSETAG              = '<\/' . self::PARTIAL_TAGNAME . '\s*[>]';
54
    public const PARTIAL_OPENBLOCKTAG          = '<' . self::PARTIAL_BLOCKTAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>';
55
    public const PARTIAL_CLOSEBLOCKTAG         = '<\/' . self::PARTIAL_BLOCKTAGNAME . '\s*[>]';
56
    public const PARTIAL_HTMLCOMMENT           = '<!-->|<!--->|<!--[\s\S]*?-->';
57
    public const PARTIAL_PROCESSINGINSTRUCTION = '[<][?][\s\S]*?[?][>]';
58
    public const PARTIAL_DECLARATION           = '<![A-Za-z]+' . '[^>]*>';
59
    public const PARTIAL_CDATA                 = '<!\[CDATA\[[\s\S]*?]\]>';
60
    public const PARTIAL_HTMLTAG               = '(?:' . self::PARTIAL_OPENTAG . '|' . self::PARTIAL_CLOSETAG . '|' . self::PARTIAL_HTMLCOMMENT . '|' .
61
        self::PARTIAL_PROCESSINGINSTRUCTION . '|' . self::PARTIAL_DECLARATION . '|' . self::PARTIAL_CDATA . ')';
62
    public const PARTIAL_HTMLBLOCKOPEN         = '<(?:' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s\/>]|$)' . '|' .
63
        '\/' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s>]|$)' . '|' . '[?!])';
64
    public const PARTIAL_LINK_TITLE            = '^(?:"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*+"' .
65
        '|' . '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*+\'' .
66
        '|' . '\((' . self::PARTIAL_ESCAPED_CHAR . '|[^()\x00])*+\))';
67
68
    public const REGEX_PUNCTUATION        = '/^[!"#$%&\'()*+,\-.\\/:;<=>?@\\[\\]\\\\^_`{|}~\p{P}\p{S}]/u';
69
    public const REGEX_UNSAFE_PROTOCOL    = '/^javascript:|vbscript:|file:|data:/i';
70
    public const REGEX_SAFE_DATA_PROTOCOL = '/^data:image\/(?:png|gif|jpeg|webp)/i';
71
    public const REGEX_NON_SPACE          = '/[^ \t\f\v\r\n]/';
72
73
    public const REGEX_WHITESPACE_CHAR         = '/^[ \t\n\x0b\x0c\x0d]/';
74
    public const REGEX_UNICODE_WHITESPACE_CHAR = '/^\pZ|\s/u';
75
    public const REGEX_THEMATIC_BREAK          = '/^(?:\*[ \t]*){3,}$|^(?:_[ \t]*){3,}$|^(?:-[ \t]*){3,}$/';
76
    public const REGEX_LINK_DESTINATION_BRACES = '/^(?:<(?:[^<>\\n\\\\\\x00]|\\\\.)*>)/';
77
78
    /**
79
     * @psalm-pure
80
     */
81 92
    public static function isEscapable(string $character): bool
82
    {
83 92
        return \preg_match('/' . self::PARTIAL_ESCAPABLE . '/', $character) === 1;
84
    }
85
86 128
    public static function isWhitespace(string $character): bool
87
    {
88
        /** @psalm-suppress InvalidLiteralArgument */
89 128
        return $character !== '' && \strpos(" \t\n\x0b\x0c\x0d", $character) !== false;
90
    }
91
92
    /**
93
     * @psalm-pure
94
     */
95 2268
    public static function isLetter(?string $character): bool
96
    {
97 2268
        if ($character === null) {
98
            return false;
99
        }
100
101 2268
        return \preg_match('/[\pL]/u', $character) === 1;
102
    }
103
104
    /**
105
     * Attempt to match a regex in string s at offset offset
106
     *
107
     * @psalm-param non-empty-string $regex
108
     *
109
     * @return int|null Index of match, or null
110
     *
111
     * @psalm-pure
112
     */
113 1406
    public static function matchAt(string $regex, string $string, int $offset = 0): ?int
114
    {
115 1406
        $matches = [];
116 1406
        $string  = \mb_substr($string, $offset, null, 'UTF-8');
117 1406
        if (! \preg_match($regex, $string, $matches, \PREG_OFFSET_CAPTURE)) {
118 1330
            return null;
119
        }
120
121
        // PREG_OFFSET_CAPTURE always returns the byte offset, not the char offset, which is annoying
122 250
        $charPos = \mb_strlen(\mb_strcut($string, 0, $matches[0][1], 'UTF-8'), 'UTF-8');
123
124 250
        return $offset + $charPos;
125
    }
126
127
    /**
128
     * Functional wrapper around preg_match_all which only returns the first set of matches
129
     *
130
     * @psalm-param non-empty-string $pattern
131
     *
132
     * @return string[]|null
133
     *
134
     * @psalm-pure
135
     */
136 1386
    public static function matchFirst(string $pattern, string $subject, int $offset = 0): ?array
137
    {
138 1386
        if ($offset !== 0) {
139 28
            $subject = \substr($subject, $offset);
140
        }
141
142 1386
        \preg_match_all($pattern, $subject, $matches, \PREG_SET_ORDER);
143
144 1386
        if ($matches === []) {
145 1072
            return null;
146
        }
147
148 488
        return $matches[0] ?: null;
149
    }
150
151
    /**
152
     * Replace backslash escapes with literal characters
153
     *
154
     * @psalm-pure
155
     */
156 480
    public static function unescape(string $string): string
157
    {
158 480
        $allEscapedChar = '/\\\\(' . self::PARTIAL_ESCAPABLE . ')/';
159
160 480
        $escaped = \preg_replace($allEscapedChar, '$1', $string);
161
        \assert(\is_string($escaped));
162
163 480
        return \preg_replace_callback('/' . self::PARTIAL_ENTITY . '/i', static fn ($e) => Html5EntityDecoder::decode($e[0]), $escaped);
164
    }
165
166
    /**
167
     * @internal
168
     *
169
     * @param int $type HTML block type
170
     *
171
     * @psalm-param HtmlBlock::TYPE_* $type
172
     *
173
     * @phpstan-param HtmlBlock::TYPE_* $type
174
     *
175
     * @psalm-return non-empty-string
176
     *
177
     * @throws InvalidArgumentException if an invalid type is given
178
     *
179
     * @psalm-pure
180
     */
181 226
    public static function getHtmlBlockOpenRegex(int $type): string
182
    {
183
        switch ($type) {
184
            case HtmlBlock::TYPE_1_CODE_CONTAINER:
185 226
                return '/^<(?:script|pre|textarea|style)(?:\s|>|$)/i';
186
            case HtmlBlock::TYPE_2_COMMENT:
187 212
                return '/^<!--/';
188
            case HtmlBlock::TYPE_3:
189 202
                return '/^<[?]/';
190
            case HtmlBlock::TYPE_4:
191 200
                return '/^<![A-Z]/i';
192
            case HtmlBlock::TYPE_5_CDATA:
193 194
                return '/^<!\[CDATA\[/i';
194
            case HtmlBlock::TYPE_6_BLOCK_ELEMENT:
195 190
                return '%^<[/]?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[123456]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?:\s|[/]?[>]|$)%i';
196
            case HtmlBlock::TYPE_7_MISC_ELEMENT:
197 130
                return '/^(?:' . self::PARTIAL_OPENTAG . '|' . self::PARTIAL_CLOSETAG . ')\\s*$/i';
198
            default:
199 2
                throw new InvalidArgumentException('Invalid HTML block type');
200
        }
201
    }
202
203
    /**
204
     * @internal
205
     *
206
     * @param int $type HTML block type
207
     *
208
     * @psalm-param HtmlBlock::TYPE_* $type
209
     *
210
     * @phpstan-param HtmlBlock::TYPE_* $type
211
     *
212
     * @psalm-return non-empty-string
213
     *
214
     * @throws InvalidArgumentException if an invalid type is given
215
     *
216
     * @psalm-pure
217
     */
218 30
    public static function getHtmlBlockCloseRegex(int $type): string
219
    {
220
        switch ($type) {
221
            case HtmlBlock::TYPE_1_CODE_CONTAINER:
222 30
                return '%<\/(?:script|pre|textarea|style)>%i';
223
            case HtmlBlock::TYPE_2_COMMENT:
224 10
                return '/-->/';
225
            case HtmlBlock::TYPE_3:
226 2
                return '/\?>/';
227
            case HtmlBlock::TYPE_4:
228 6
                return '/>/';
229
            case HtmlBlock::TYPE_5_CDATA:
230 2
                return '/\]\]>/';
231
            default:
232 6
                throw new InvalidArgumentException('Invalid HTML block type');
233
        }
234
    }
235
236
    /**
237
     * @psalm-pure
238
     */
239 12
    public static function isLinkPotentiallyUnsafe(string $url): bool
240
    {
241 12
        return \preg_match(self::REGEX_UNSAFE_PROTOCOL, $url) !== 0 && \preg_match(self::REGEX_SAFE_DATA_PROTOCOL, $url) === 0;
242
    }
243
}
244