RegexHelper   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 26
lcom 0
cbo 1
dl 0
loc 194
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isEscapable() 0 4 1
A matchAt() 0 13 2
B matchAll() 0 21 6
A unescape() 0 11 1
A isLinkPotentiallyUnsafe() 0 4 2
B getHtmlBlockOpenRegex() 0 21 8
A getHtmlBlockCloseRegex() 0 17 6
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Util;
16
17
use League\CommonMark\Block\Element\HtmlBlock;
18
19
/**
20
 * Provides regular expressions and utilities for parsing Markdown
21
 */
22
final class RegexHelper
23
{
24
    // Partial regular expressions (wrap with `/` on each side before use)
25
    const PARTIAL_ENTITY = '&(?:#x[a-f0-9]{1,6}|#[0-9]{1,7}|[a-z][a-z0-9]{1,31});';
26
    const PARTIAL_ESCAPABLE = '[!"#$%&\'()*+,.\/:;<=>[email protected][\\\\\]^_`{|}~-]';
27
    const PARTIAL_ESCAPED_CHAR = '\\\\' . self::PARTIAL_ESCAPABLE;
28
    const PARTIAL_IN_DOUBLE_QUOTES = '"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"';
29
    const PARTIAL_IN_SINGLE_QUOTES = '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\'';
30
    const PARTIAL_IN_PARENS = '\\((' . self::PARTIAL_ESCAPED_CHAR . '|[^)\x00])*\\)';
31
    const PARTIAL_REG_CHAR = '[^\\\\()\x00-\x20]';
32
    const PARTIAL_IN_PARENS_NOSP = '\((' . self::PARTIAL_REG_CHAR . '|' . self::PARTIAL_ESCAPED_CHAR . '|\\\\)*\)';
33
    const PARTIAL_TAGNAME = '[A-Za-z][A-Za-z0-9-]*';
34
    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|title|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)';
35
    const PARTIAL_ATTRIBUTENAME = '[a-zA-Z_:][a-zA-Z0-9:._-]*';
36
    const PARTIAL_UNQUOTEDVALUE = '[^"\'=<>`\x00-\x20]+';
37
    const PARTIAL_SINGLEQUOTEDVALUE = '\'[^\']*\'';
38
    const PARTIAL_DOUBLEQUOTEDVALUE = '"[^"]*"';
39
    const PARTIAL_ATTRIBUTEVALUE = '(?:' . self::PARTIAL_UNQUOTEDVALUE . '|' . self::PARTIAL_SINGLEQUOTEDVALUE . '|' . self::PARTIAL_DOUBLEQUOTEDVALUE . ')';
40
    const PARTIAL_ATTRIBUTEVALUESPEC = '(?:' . '\s*=' . '\s*' . self::PARTIAL_ATTRIBUTEVALUE . ')';
41
    const PARTIAL_ATTRIBUTE = '(?:' . '\s+' . self::PARTIAL_ATTRIBUTENAME . self::PARTIAL_ATTRIBUTEVALUESPEC . '?)';
42
    const PARTIAL_OPENTAG = '<' . self::PARTIAL_TAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>';
43
    const PARTIAL_CLOSETAG = '<\/' . self::PARTIAL_TAGNAME . '\s*[>]';
44
    const PARTIAL_OPENBLOCKTAG = '<' . self::PARTIAL_BLOCKTAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>';
45
    const PARTIAL_CLOSEBLOCKTAG = '<\/' . self::PARTIAL_BLOCKTAGNAME . '\s*[>]';
46
    const PARTIAL_HTMLCOMMENT = '<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->';
47
    const PARTIAL_PROCESSINGINSTRUCTION = '[<][?].*?[?][>]';
48
    const PARTIAL_DECLARATION = '<![A-Z]+' . '\s+[^>]*>';
49
    const PARTIAL_CDATA = '<!\[CDATA\[[\s\S]*?]\]>';
50
    const PARTIAL_HTMLTAG = '(?:' . self::PARTIAL_OPENTAG . '|' . self::PARTIAL_CLOSETAG . '|' . self::PARTIAL_HTMLCOMMENT . '|' .
51
        self::PARTIAL_PROCESSINGINSTRUCTION . '|' . self::PARTIAL_DECLARATION . '|' . self::PARTIAL_CDATA . ')';
52
    const PARTIAL_HTMLBLOCKOPEN = '<(?:' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s\/>]|$)' . '|' .
53
        '\/' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s>]|$)' . '|' . '[?!])';
54
    const PARTIAL_LINK_TITLE = '^(?:"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"' .
55
        '|' . '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\'' .
56
        '|' . '\((' . self::PARTIAL_ESCAPED_CHAR . '|[^()\x00])*\))';
57
58
    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}\\\\\'!"#\$%&\(\)\*\+,\-\.\\/:;<=>\[email protected]\[\]\^_`\{\|\}~]/u';
59
    const REGEX_UNSAFE_PROTOCOL = '/^javascript:|vbscript:|file:|data:/i';
60
    const REGEX_SAFE_DATA_PROTOCOL = '/^data:image\/(?:png|gif|jpeg|webp)/i';
61
    const REGEX_NON_SPACE = '/[^ \t\f\v\r\n]/';
62
63
    const REGEX_WHITESPACE_CHAR = '/^[ \t\n\x0b\x0c\x0d]/';
64
    const REGEX_WHITESPACE = '/[ \t\n\x0b\x0c\x0d]+/';
65
    const REGEX_UNICODE_WHITESPACE_CHAR = '/^\pZ|\s/u';
66
    const REGEX_THEMATIC_BREAK = '/^(?:(?:\*[ \t]*){3,}|(?:_[ \t]*){3,}|(?:-[ \t]*){3,})[ \t]*$/';
67
    const REGEX_LINK_DESTINATION_BRACES = '/^(?:<(?:[^<>\\n\\\\\\x00]|\\\\.)*>)/';
68
69
    /**
70
     * @param string $character
71
     *
72
     * @return bool
73
     */
74 114
    public static function isEscapable(string $character): bool
75
    {
76 114
        return \preg_match('/' . self::PARTIAL_ESCAPABLE . '/', $character) === 1;
77
    }
78
79
    /**
80
     * Attempt to match a regex in string s at offset offset
81
     *
82
     * @param string $regex
83
     * @param string $string
84
     * @param int    $offset
85
     *
86
     * @return int|null Index of match, or null
87
     */
88 1899
    public static function matchAt(string $regex, string $string, int $offset = 0): ?int
89
    {
90 1899
        $matches = [];
91 1899
        $string = \mb_substr($string, $offset, null, 'utf-8');
92 1899
        if (!\preg_match($regex, $string, $matches, PREG_OFFSET_CAPTURE)) {
93 1839
            return null;
94
        }
95
96
        // PREG_OFFSET_CAPTURE always returns the byte offset, not the char offset, which is annoying
97 297
        $charPos = \mb_strlen(\mb_strcut($string, 0, $matches[0][1], 'utf-8'), 'utf-8');
98
99 297
        return $offset + $charPos;
100
    }
101
102
    /**
103
     * Functional wrapper around preg_match_all
104
     *
105
     * @param string $pattern
106
     * @param string $subject
107
     * @param int    $offset
108
     *
109
     * @return array|null
110
     */
111 2001
    public static function matchAll(string $pattern, string $subject, int $offset = 0): ?array
112
    {
113 2001
        if ($offset !== 0) {
114 420
            $subject = \substr($subject, $offset);
115
        }
116
117 2001
        \preg_match_all($pattern, $subject, $matches, PREG_PATTERN_ORDER);
118
119 2001
        $fullMatches = \reset($matches);
120 2001
        if (empty($fullMatches)) {
121 1956
            return null;
122
        }
123
124 303
        if (\count($fullMatches) === 1) {
125 303
            foreach ($matches as &$match) {
126 303
                $match = \reset($match);
127
            }
128
        }
129
130 303
        return $matches ?: null;
131
    }
132
133
    /**
134
     * Replace backslash escapes with literal characters
135
     *
136
     * @param string $string
137
     *
138
     * @return string
139
     */
140 525
    public static function unescape(string $string): string
141
    {
142 525
        $allEscapedChar = '/\\\\(' . self::PARTIAL_ESCAPABLE . ')/';
143
144 525
        $escaped = \preg_replace($allEscapedChar, '$1', $string);
145
        $replaced = \preg_replace_callback('/' . self::PARTIAL_ENTITY . '/i', function ($e) {
146 24
            return Html5Entities::decodeEntity($e[0]);
147 525
        }, $escaped);
148
149 525
        return $replaced;
150
    }
151
152
    /**
153
     * @param int $type HTML block type
154
     *
155
     * @return string
156
     *
157
     * @internal
158
     */
159 318
    public static function getHtmlBlockOpenRegex(int $type): string
160
    {
161 212
        switch ($type) {
162 106
            case HtmlBlock::TYPE_1_CODE_CONTAINER:
163 285
                return '/^<(?:script|pre|style)(?:\s|>|$)/i';
164 98
            case HtmlBlock::TYPE_2_COMMENT:
165 267
                return '/^<!--/';
166 91
            case HtmlBlock::TYPE_3:
167 252
                return '/^<[?]/';
168 88
            case HtmlBlock::TYPE_4:
169 249
                return '/^<![A-Z]/';
170 85
            case HtmlBlock::TYPE_5_CDATA:
171 246
                return '/^<!\[CDATA\[/';
172 82
            case HtmlBlock::TYPE_6_BLOCK_ELEMENT:
173 240
                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|title|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?:\s|[/]?[>]|$)%i';
174 54
            case HtmlBlock::TYPE_7_MISC_ELEMENT:
175 159
                return '/^(?:' . self::PARTIAL_OPENTAG . '|' . self::PARTIAL_CLOSETAG . ')\\s*$/i';
176
        }
177
178 3
        throw new \InvalidArgumentException('Invalid HTML block type');
179
    }
180
181
    /**
182
     * @param int $type HTML block type
183
     *
184
     * @return string
185
     *
186
     * @internal
187
     */
188 63
    public static function getHtmlBlockCloseRegex(int $type): string
189
    {
190 42
        switch ($type) {
191 21
            case HtmlBlock::TYPE_1_CODE_CONTAINER:
192 30
                return '%<\/(?:script|pre|style)>%i';
193 11
            case HtmlBlock::TYPE_2_COMMENT:
194 15
                return '/-->/';
195 6
            case HtmlBlock::TYPE_3:
196 3
                return '/\?>/';
197 5
            case HtmlBlock::TYPE_4:
198 3
                return '/>/';
199 4
            case HtmlBlock::TYPE_5_CDATA:
200 3
                return '/\]\]>/';
201
        }
202
203 9
        throw new \InvalidArgumentException('Invalid HTML block type');
204
    }
205
206
    /**
207
     * @param string $url
208
     *
209
     * @return bool
210
     */
211 24
    public static function isLinkPotentiallyUnsafe(string $url): bool
212
    {
213 24
        return \preg_match(self::REGEX_UNSAFE_PROTOCOL, $url) !== 0 && \preg_match(self::REGEX_SAFE_DATA_PROTOCOL, $url) === 0;
214
    }
215
}
216