Passed
Push — latest ( ba39d8...ca9086 )
by Colin
08:23
created

RegexHelper::matchAt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
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\Extension\CommonMark\Node\Block\HtmlBlock;
20
21
/**
22
 * Provides regular expressions and utilities for parsing Markdown
23
 *
24
 * @phpcs:disable Generic.Strings.UnnecessaryStringConcat.Found
25
 *
26
 * @psalm-immutable
27
 */
28
final class RegexHelper
29
{
30
    // Partial regular expressions (wrap with `/` on each side before use)
31
    public const PARTIAL_ENTITY                = '&(?:#x[a-f0-9]{1,6}|#[0-9]{1,7}|[a-z][a-z0-9]{1,31});';
32
    public const PARTIAL_ESCAPABLE             = '[!"#$%&\'()*+,.\/:;<=>?@[\\\\\]^_`{|}~-]';
33
    public const PARTIAL_ESCAPED_CHAR          = '\\\\' . self::PARTIAL_ESCAPABLE;
34
    public const PARTIAL_IN_DOUBLE_QUOTES      = '"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"';
35
    public const PARTIAL_IN_SINGLE_QUOTES      = '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\'';
36
    public const PARTIAL_IN_PARENS             = '\\((' . self::PARTIAL_ESCAPED_CHAR . '|[^)\x00])*\\)';
37
    public const PARTIAL_REG_CHAR              = '[^\\\\()\x00-\x20]';
38
    public const PARTIAL_IN_PARENS_NOSP        = '\((' . self::PARTIAL_REG_CHAR . '|' . self::PARTIAL_ESCAPED_CHAR . '|\\\\)*\)';
39
    public const PARTIAL_TAGNAME               = '[A-Za-z][A-Za-z0-9-]*';
40
    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|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)';
41
    public const PARTIAL_ATTRIBUTENAME         = '[a-zA-Z_:][a-zA-Z0-9:._-]*';
42
    public const PARTIAL_UNQUOTEDVALUE         = '[^"\'=<>`\x00-\x20]+';
43
    public const PARTIAL_SINGLEQUOTEDVALUE     = '\'[^\']*\'';
44
    public const PARTIAL_DOUBLEQUOTEDVALUE     = '"[^"]*"';
45
    public const PARTIAL_ATTRIBUTEVALUE        = '(?:' . self::PARTIAL_UNQUOTEDVALUE . '|' . self::PARTIAL_SINGLEQUOTEDVALUE . '|' . self::PARTIAL_DOUBLEQUOTEDVALUE . ')';
46
    public const PARTIAL_ATTRIBUTEVALUESPEC    = '(?:' . '\s*=' . '\s*' . self::PARTIAL_ATTRIBUTEVALUE . ')';
47
    public const PARTIAL_ATTRIBUTE             = '(?:' . '\s+' . self::PARTIAL_ATTRIBUTENAME . self::PARTIAL_ATTRIBUTEVALUESPEC . '?)';
48
    public const PARTIAL_OPENTAG               = '<' . self::PARTIAL_TAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>';
49
    public const PARTIAL_CLOSETAG              = '<\/' . self::PARTIAL_TAGNAME . '\s*[>]';
50
    public const PARTIAL_OPENBLOCKTAG          = '<' . self::PARTIAL_BLOCKTAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>';
51
    public const PARTIAL_CLOSEBLOCKTAG         = '<\/' . self::PARTIAL_BLOCKTAGNAME . '\s*[>]';
52
    public const PARTIAL_HTMLCOMMENT           = '<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->';
53
    public const PARTIAL_PROCESSINGINSTRUCTION = '[<][?].*?[?][>]';
54
    public const PARTIAL_DECLARATION           = '<![A-Z]+' . '\s+[^>]*>';
55
    public const PARTIAL_CDATA                 = '<!\[CDATA\[[\s\S]*?]\]>';
56
    public const PARTIAL_HTMLTAG               = '(?:' . self::PARTIAL_OPENTAG . '|' . self::PARTIAL_CLOSETAG . '|' . self::PARTIAL_HTMLCOMMENT . '|' .
57
        self::PARTIAL_PROCESSINGINSTRUCTION . '|' . self::PARTIAL_DECLARATION . '|' . self::PARTIAL_CDATA . ')';
58
    public const PARTIAL_HTMLBLOCKOPEN         = '<(?:' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s\/>]|$)' . '|' .
59
        '\/' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s>]|$)' . '|' . '[?!])';
60
    public const PARTIAL_LINK_TITLE            = '^(?:"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"' .
61
        '|' . '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\'' .
62
        '|' . '\((' . self::PARTIAL_ESCAPED_CHAR . '|[^()\x00])*\))';
63
64
    public const REGEX_PUNCTUATION        = '/^[\x{2000}-\x{206F}\x{2E00}-\x{2E7F}\p{Pc}\p{Pd}\p{Pe}\p{Pf}\p{Pi}\p{Po}\p{Ps}\\\\\'!"#\$%&\(\)\*\+,\-\.\\/:;<=>\?@\[\]\^_`\{\|\}~]/u';
65
    public const REGEX_UNSAFE_PROTOCOL    = '/^javascript:|vbscript:|file:|data:/i';
66
    public const REGEX_SAFE_DATA_PROTOCOL = '/^data:image\/(?:png|gif|jpeg|webp)/i';
67
    public const REGEX_NON_SPACE          = '/[^ \t\f\v\r\n]/';
68
69
    public const REGEX_WHITESPACE_CHAR         = '/^[ \t\n\x0b\x0c\x0d]/';
70
    public const REGEX_UNICODE_WHITESPACE_CHAR = '/^\pZ|\s/u';
71
    public const REGEX_THEMATIC_BREAK          = '/^(?:(?:\*[ \t]*){3,}|(?:_[ \t]*){3,}|(?:-[ \t]*){3,})[ \t]*$/';
72
    public const REGEX_LINK_DESTINATION_BRACES = '/^(?:<(?:[^<>\\n\\\\\\x00]|\\\\.)*>)/';
73
74
    /**
75
     * @psalm-pure
76
     */
77 123
    public static function isEscapable(string $character): bool
78
    {
79 123
        return \preg_match('/' . self::PARTIAL_ESCAPABLE . '/', $character) === 1;
80
    }
81
82
    /**
83
     * @psalm-pure
84
     */
85 2784
    public static function isLetter(?string $character): bool
86
    {
87 2784
        if ($character === null) {
88
            return false;
89
        }
90
91 2784
        return \preg_match('/[\pL]/u', $character) === 1;
92
    }
93
94
    /**
95
     * Attempt to match a regex in string s at offset offset
96
     *
97
     * @return int|null Index of match, or null
98
     *
99
     * @psalm-pure
100
     */
101 1761
    public static function matchAt(string $regex, string $string, int $offset = 0): ?int
102
    {
103 1761
        $matches = [];
104 1761
        $string  = \mb_substr($string, $offset, null, 'utf-8');
105 1761
        if (! \preg_match($regex, $string, $matches, \PREG_OFFSET_CAPTURE)) {
106 1665
            return null;
107
        }
108
109
        // PREG_OFFSET_CAPTURE always returns the byte offset, not the char offset, which is annoying
110 318
        $charPos = \mb_strlen(\mb_strcut($string, 0, $matches[0][1], 'utf-8'), 'utf-8');
111
112 318
        return $offset + $charPos;
113
    }
114
115
    /**
116
     * Functional wrapper around preg_match_all
117
     *
118
     * @return array<string>|null
119
     *
120
     * @psalm-pure
121
     */
122 2067
    public static function matchAll(string $pattern, string $subject, int $offset = 0): ?array
123
    {
124 2067
        if ($offset !== 0) {
125 240
            $subject = \substr($subject, $offset);
126
        }
127
128 2067
        \preg_match_all($pattern, $subject, $matches, \PREG_PATTERN_ORDER);
129
130 2067
        $fullMatches = \reset($matches);
131 2067
        if ($fullMatches === false || \count($fullMatches) === 0) {
132 1935
            return null;
133
        }
134
135 516
        if (\count($fullMatches) === 1) {
136 516
            foreach ($matches as &$match) {
137 516
                $match = \reset($match);
138
            }
139
        }
140
141 516
        return $matches ?: null;
142
    }
143
144
    /**
145
     * Replace backslash escapes with literal characters
146
     *
147
     * @psalm-pure
148
     */
149 579
    public static function unescape(string $string): string
150
    {
151 579
        $allEscapedChar = '/\\\\(' . self::PARTIAL_ESCAPABLE . ')/';
152
153 579
        $escaped = \preg_replace($allEscapedChar, '$1', $string);
154
        \assert(\is_string($escaped));
155
156 386
        return \preg_replace_callback('/' . self::PARTIAL_ENTITY . '/i', static function ($e) {
157 24
            return Html5EntityDecoder::decode($e[0]);
158 579
        }, $escaped);
159
    }
160
161
    /**
162
     * @internal
163
     *
164
     * @param int $type HTML block type
165
     *
166
     * @psalm-pure
167
     */
168 339
    public static function getHtmlBlockOpenRegex(int $type): string
169
    {
170 339
        switch ($type) {
171
            case HtmlBlock::TYPE_1_CODE_CONTAINER:
172 306
                return '/^<(?:script|pre|style)(?:\s|>|$)/i';
173
            case HtmlBlock::TYPE_2_COMMENT:
174 288
                return '/^<!--/';
175
            case HtmlBlock::TYPE_3:
176 273
                return '/^<[?]/';
177
            case HtmlBlock::TYPE_4:
178 270
                return '/^<![A-Z]/';
179
            case HtmlBlock::TYPE_5_CDATA:
180 267
                return '/^<!\[CDATA\[/';
181
            case HtmlBlock::TYPE_6_BLOCK_ELEMENT:
182 261
                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';
183
            case HtmlBlock::TYPE_7_MISC_ELEMENT:
184 177
                return '/^(?:' . self::PARTIAL_OPENTAG . '|' . self::PARTIAL_CLOSETAG . ')\\s*$/i';
185
            default:
186 3
                throw new \InvalidArgumentException('Invalid HTML block type');
187
        }
188
    }
189
190
    /**
191
     * @internal
192
     *
193
     * @param int $type HTML block type
194
     *
195
     * @psalm-pure
196
     */
197 66
    public static function getHtmlBlockCloseRegex(int $type): string
198
    {
199 66
        switch ($type) {
200
            case HtmlBlock::TYPE_1_CODE_CONTAINER:
201 33
                return '%<\/(?:script|pre|style)>%i';
202
            case HtmlBlock::TYPE_2_COMMENT:
203 15
                return '/-->/';
204
            case HtmlBlock::TYPE_3:
205 3
                return '/\?>/';
206
            case HtmlBlock::TYPE_4:
207 3
                return '/>/';
208
            case HtmlBlock::TYPE_5_CDATA:
209 3
                return '/\]\]>/';
210
            default:
211 9
                throw new \InvalidArgumentException('Invalid HTML block type');
212
        }
213
    }
214
215
    /**
216
     * @psalm-pure
217
     */
218 36
    public static function isLinkPotentiallyUnsafe(string $url): bool
219
    {
220 36
        return \preg_match(self::REGEX_UNSAFE_PROTOCOL, $url) !== 0 && \preg_match(self::REGEX_SAFE_DATA_PROTOCOL, $url) === 0;
221
    }
222
}
223