Completed
Push — master ( afd04b...3b4c22 )
by Colin
10s
created

src/Util/RegexHelper.php (30 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    /** @deprecated Use PARTIAL_ESCAPABLE instead */
25
    const ESCAPABLE = 0;
26
27
    /** @deprecated Use PARTIAL_ESCAPED_CHAR instead */
28
    const ESCAPED_CHAR = 1;
29
30
    /** @deprecated Use PARTIAL_IN_DOUBLE_QUOTES instead */
31
    const IN_DOUBLE_QUOTES = 2;
32
33
    /** @deprecated Use PARTIAL_IN_SINGLE_QUOTES instead */
34
    const IN_SINGLE_QUOTES = 3;
35
36
    /** @deprecated Use PARTIAL_IN_PARENS instead */
37
    const IN_PARENS = 4;
38
39
    /** @deprecated Use PARTIAL_REG_CHAR instead */
40
    const REG_CHAR = 5;
41
42
    /** @deprecated Use PARTIAL_IN_PARENS_NOSP instead */
43
    const IN_PARENS_NOSP = 6;
44
45
    /** @deprecated Use PARTIAL_TAGNAME instead */
46
    const TAGNAME = 7;
47
48
    /** @deprecated Use PARTIAL_BLOCKTAGNAME instead */
49
    const BLOCKTAGNAME = 8;
50
51
    /** @deprecated Use PARTIAL_ATTRIBUTENAME instead */
52
    const ATTRIBUTENAME = 9;
53
54
    /** @deprecated Use PARTIAL_UNQUOTEDVALUE instead */
55
    const UNQUOTEDVALUE = 10;
56
57
    /** @deprecated Use PARTIAL_SINGLEQUOTEDVALUE instead */
58
    const SINGLEQUOTEDVALUE = 11;
59
60
    /** @deprecated Use PARTIAL_DOUBLEQUOTEDVALUE instead */
61
    const DOUBLEQUOTEDVALUE = 12;
62
63
    /** @deprecated Use PARTIAL_ATTRIBUTEVALUE instead */
64
    const ATTRIBUTEVALUE = 13;
65
66
    /** @deprecated Use PARTIAL_ATTRIBUTEVALUESPEC instead */
67
    const ATTRIBUTEVALUESPEC = 14;
68
69
    /** @deprecated Use PARTIAL_ATTRIBUTE instead */
70
    const ATTRIBUTE = 15;
71
72
    /** @deprecated Use PARTIAL_OPENTAG instead */
73
    const OPENTAG = 16;
74
75
    /** @deprecated Use PARTIAL_CLOSETAG instead */
76
    const CLOSETAG = 17;
77
78
    /** @deprecated Use PARTIAL_OPENBLOCKTAG instead */
79
    const OPENBLOCKTAG = 18;
80
81
    /** @deprecated Use PARTIAL_CLOSEBLOCKTAG instead */
82
    const CLOSEBLOCKTAG = 19;
83
84
    /** @deprecated Use PARTIAL_HTMLCOMMENT instead */
85
    const HTMLCOMMENT = 20;
86
87
    /** @deprecated Use PARTIAL_PROCESSINGINSTRUCTION instead */
88
    const PROCESSINGINSTRUCTION = 21;
89
90
    /** @deprecated Use PARTIAL_DECLARATION instead */
91
    const DECLARATION = 22;
92
93
    /** @deprecated Use PARTIAL_CDATA instead */
94
    const CDATA = 23;
95
96
    /** @deprecated Use PARTIAL_HTMLTAG instead */
97
    const HTMLTAG = 24;
98
99
    /** @deprecated Use PARTIAL_HTMLBLOCKOPEN instead */
100
    const HTMLBLOCKOPEN = 25;
101
102
    /** @deprecated Use PARTIAL_LINK_TITLE instead */
103
    const LINK_TITLE = 26;
104
105
    // Partial regular expressions (wrap with `/` on each side before use)
106
    const PARTIAL_ENTITY = '&(?:#x[a-f0-9]{1,8}|#[0-9]{1,8}|[a-z][a-z0-9]{1,31});';
107
    const PARTIAL_ESCAPABLE = '[!"#$%&\'()*+,.\/:;<=>?@[\\\\\]^_`{|}~-]';
108
    const PARTIAL_ESCAPED_CHAR = '\\\\' . self::PARTIAL_ESCAPABLE;
109
    const PARTIAL_IN_DOUBLE_QUOTES = '"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"';
110
    const PARTIAL_IN_SINGLE_QUOTES = '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\'';
111
    const PARTIAL_IN_PARENS = '\\((' . self::PARTIAL_ESCAPED_CHAR . '|[^)\x00])*\\)';
112
    const PARTIAL_REG_CHAR = '[^\\\\()\x00-\x20]';
113
    const PARTIAL_IN_PARENS_NOSP = '\((' . self::PARTIAL_REG_CHAR . '|' . self::PARTIAL_ESCAPED_CHAR . '|\\\\)*\)';
114
    const PARTIAL_TAGNAME = '[A-Za-z][A-Za-z0-9-]*';
115
    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|meta|nav|noframes|ol|optgroup|option|p|param|section|source|title|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)';
116
    const PARTIAL_ATTRIBUTENAME = '[a-zA-Z_:][a-zA-Z0-9:._-]*';
117
    const PARTIAL_UNQUOTEDVALUE = '[^"\'=<>`\x00-\x20]+';
118
    const PARTIAL_SINGLEQUOTEDVALUE = '\'[^\']*\'';
119
    const PARTIAL_DOUBLEQUOTEDVALUE = '"[^"]*"';
120
    const PARTIAL_ATTRIBUTEVALUE = '(?:' . self::PARTIAL_UNQUOTEDVALUE . '|' . self::PARTIAL_SINGLEQUOTEDVALUE . '|' . self::PARTIAL_DOUBLEQUOTEDVALUE . ')';
121
    const PARTIAL_ATTRIBUTEVALUESPEC = '(?:' . '\s*=' . '\s*' . self::PARTIAL_ATTRIBUTEVALUE . ')';
122
    const PARTIAL_ATTRIBUTE = '(?:' . '\s+' . self::PARTIAL_ATTRIBUTENAME . self::PARTIAL_ATTRIBUTEVALUESPEC . '?)';
123
    const PARTIAL_OPENTAG = '<' . self::PARTIAL_TAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>';
124
    const PARTIAL_CLOSETAG = '<\/' . self::PARTIAL_TAGNAME . '\s*[>]';
125
    const PARTIAL_OPENBLOCKTAG = '<' . self::PARTIAL_BLOCKTAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>';
126
    const PARTIAL_CLOSEBLOCKTAG = '<\/' . self::PARTIAL_BLOCKTAGNAME . '\s*[>]';
127
    const PARTIAL_HTMLCOMMENT = '<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->';
128
    const PARTIAL_PROCESSINGINSTRUCTION = '[<][?].*?[?][>]';
129
    const PARTIAL_DECLARATION = '<![A-Z]+' . '\s+[^>]*>';
130
    const PARTIAL_CDATA = '<!\[CDATA\[[\s\S]*?]\]>';
131
    const PARTIAL_HTMLTAG = '(?:' . self::PARTIAL_OPENTAG . '|' . self::PARTIAL_CLOSETAG . '|' . self::PARTIAL_HTMLCOMMENT . '|' .
132
        self::PARTIAL_PROCESSINGINSTRUCTION . '|' . self::PARTIAL_DECLARATION . '|' . self::PARTIAL_CDATA . ')';
133
    const PARTIAL_HTMLBLOCKOPEN = '<(?:' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s\/>]|$)' . '|' .
134
        '\/' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s>]|$)' . '|' . '[?!])';
135
    const PARTIAL_LINK_TITLE = '^(?:"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"' .
136
        '|' . '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\'' .
137
        '|' . '\((' . self::PARTIAL_ESCAPED_CHAR . '|[^)\x00])*\))';
138
139
    /** @deprecated Use PARTIAL_ESCAPABLE instead */
140
    const REGEX_ESCAPABLE = self::PARTIAL_ESCAPABLE;
141
142
    /** @deprecated Use PARTIAL_ENTITY instead */
143
    const REGEX_ENTITY = self::PARTIAL_ENTITY;
144
145
    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';
146
    const REGEX_UNSAFE_PROTOCOL = '/^javascript:|vbscript:|file:|data:/i';
147
    const REGEX_SAFE_DATA_PROTOCOL = '/^data:image\/(?:png|gif|jpeg|webp)/i';
148
    const REGEX_NON_SPACE = '/[^ \t\f\v\r\n]/';
149
150
    const REGEX_WHITESPACE_CHAR = '/^[ \t\n\x0b\x0c\x0d]/';
151
    const REGEX_WHITESPACE = '/[ \t\n\x0b\x0c\x0d]+/';
152
    const REGEX_UNICODE_WHITESPACE_CHAR = '/^\pZ|\s/u';
153
    const REGEX_THEMATIC_BREAK = '/^(?:(?:\*[ \t]*){3,}|(?:_[ \t]*){3,}|(?:-[ \t]*){3,})[ \t]*$/';
154
    const REGEX_LINK_DESTINATION_BRACES = '/^(?:' . '[<](?:[^ <>\\t\\n\\\\\\x00]' . '|' . self::PARTIAL_ESCAPED_CHAR . '|' . '\\\\)*[>]' . ')/';
155
156
    /**
157
     * @deprecated Instance methods will be removed in 0.18 or 1.0 (whichever comes first)
158
     */
159
    protected static $instance;
160
161
    /**
162
     * @return RegexHelper
163
     *
164
     * @deprecated Instances are no longer needed and will be removed in 0.18 or 1.0
165
     */
166 93
    public static function getInstance()
167
    {
168 93
        @trigger_error('RegexHelper no longer uses the singleton pattern. Directly grab the REGEX_ or PARTIAL_ constant you need instead.', E_USER_DEPRECATED);
169
170 93
        if (self::$instance === null) {
0 ignored issues
show
Deprecated Code introduced by
The property League\CommonMark\Util\RegexHelper::$instance has been deprecated with message: Instance methods will be removed in 0.18 or 1.0 (whichever comes first)

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
171 3
            self::$instance = new self();
0 ignored issues
show
Deprecated Code introduced by
The property League\CommonMark\Util\RegexHelper::$instance has been deprecated with message: Instance methods will be removed in 0.18 or 1.0 (whichever comes first)

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
172 1
        }
173
174 93
        return self::$instance;
0 ignored issues
show
Deprecated Code introduced by
The property League\CommonMark\Util\RegexHelper::$instance has been deprecated with message: Instance methods will be removed in 0.18 or 1.0 (whichever comes first)

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
175
    }
176
177
    /**
178
     * Returns a partial regex
179
     *
180
     * It'll need to be wrapped with /.../ before use
181
     *
182
     * @param int $const
183
     *
184
     * @return string
185
     *
186
     * @deprecated Just grab the constant directly
187
     */
188 81
    public function getPartialRegex($const)
189
    {
190 81
        @trigger_error('RegexHelper no longer supports the getPartialRegex() function. Directly grab the PARTIAL_ constant you need instead.', E_USER_DEPRECATED);
191
192
        switch ($const) {
193 81
            case self::ESCAPABLE: return self::PARTIAL_ESCAPABLE;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::ESCAPABLE has been deprecated with message: Use PARTIAL_ESCAPABLE instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
194 78
            case self::ESCAPED_CHAR: return self::PARTIAL_ESCAPED_CHAR;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::ESCAPED_CHAR has been deprecated with message: Use PARTIAL_ESCAPED_CHAR instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
195 75
            case self::IN_DOUBLE_QUOTES: return self::PARTIAL_IN_DOUBLE_QUOTES;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\R...elper::IN_DOUBLE_QUOTES has been deprecated with message: Use PARTIAL_IN_DOUBLE_QUOTES instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
196 72
            case self::IN_SINGLE_QUOTES: return self::PARTIAL_IN_SINGLE_QUOTES;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\R...elper::IN_SINGLE_QUOTES has been deprecated with message: Use PARTIAL_IN_SINGLE_QUOTES instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
197 69
            case self::IN_PARENS: return self::PARTIAL_IN_PARENS;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::IN_PARENS has been deprecated with message: Use PARTIAL_IN_PARENS instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
198 66
            case self::REG_CHAR: return self::PARTIAL_REG_CHAR;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::REG_CHAR has been deprecated with message: Use PARTIAL_REG_CHAR instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
199 63
            case self::IN_PARENS_NOSP: return self::PARTIAL_IN_PARENS_NOSP;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::IN_PARENS_NOSP has been deprecated with message: Use PARTIAL_IN_PARENS_NOSP instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
200 60
            case self::TAGNAME: return self::PARTIAL_TAGNAME;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::TAGNAME has been deprecated with message: Use PARTIAL_TAGNAME instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
201 57
            case self::BLOCKTAGNAME: return self::PARTIAL_BLOCKTAGNAME;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::BLOCKTAGNAME has been deprecated with message: Use PARTIAL_BLOCKTAGNAME instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
202 54
            case self::ATTRIBUTENAME: return self::PARTIAL_ATTRIBUTENAME;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::ATTRIBUTENAME has been deprecated with message: Use PARTIAL_ATTRIBUTENAME instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
203 51
            case self::UNQUOTEDVALUE: return self::PARTIAL_UNQUOTEDVALUE;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::UNQUOTEDVALUE has been deprecated with message: Use PARTIAL_UNQUOTEDVALUE instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
204 48
            case self::SINGLEQUOTEDVALUE: return self::PARTIAL_SINGLEQUOTEDVALUE;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\R...lper::SINGLEQUOTEDVALUE has been deprecated with message: Use PARTIAL_SINGLEQUOTEDVALUE instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
205 45
            case self::DOUBLEQUOTEDVALUE: return self::PARTIAL_DOUBLEQUOTEDVALUE;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\R...lper::DOUBLEQUOTEDVALUE has been deprecated with message: Use PARTIAL_DOUBLEQUOTEDVALUE instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
206 42
            case self::ATTRIBUTEVALUE: return self::PARTIAL_ATTRIBUTEVALUE;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::ATTRIBUTEVALUE has been deprecated with message: Use PARTIAL_ATTRIBUTEVALUE instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
207 39
            case self::ATTRIBUTEVALUESPEC: return self::PARTIAL_ATTRIBUTEVALUESPEC;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\R...per::ATTRIBUTEVALUESPEC has been deprecated with message: Use PARTIAL_ATTRIBUTEVALUESPEC instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
208 36
            case self::ATTRIBUTE: return self::PARTIAL_ATTRIBUTE;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::ATTRIBUTE has been deprecated with message: Use PARTIAL_ATTRIBUTE instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
209 33
            case self::OPENTAG: return self::PARTIAL_OPENTAG;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::OPENTAG has been deprecated with message: Use PARTIAL_OPENTAG instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
210 30
            case self::CLOSETAG: return self::PARTIAL_CLOSETAG;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::CLOSETAG has been deprecated with message: Use PARTIAL_CLOSETAG instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
211 27
            case self::OPENBLOCKTAG: return self::PARTIAL_OPENBLOCKTAG;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::OPENBLOCKTAG has been deprecated with message: Use PARTIAL_OPENBLOCKTAG instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
212 24
            case self::CLOSEBLOCKTAG: return self::PARTIAL_CLOSEBLOCKTAG;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::CLOSEBLOCKTAG has been deprecated with message: Use PARTIAL_CLOSEBLOCKTAG instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
213 21
            case self::HTMLCOMMENT: return self::PARTIAL_HTMLCOMMENT;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::HTMLCOMMENT has been deprecated with message: Use PARTIAL_HTMLCOMMENT instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
214 18
            case self::PROCESSINGINSTRUCTION: return self::PARTIAL_PROCESSINGINSTRUCTION;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\R...::PROCESSINGINSTRUCTION has been deprecated with message: Use PARTIAL_PROCESSINGINSTRUCTION instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
215 15
            case self::DECLARATION: return self::PARTIAL_DECLARATION;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::DECLARATION has been deprecated with message: Use PARTIAL_DECLARATION instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
216 12
            case self::CDATA: return self::PARTIAL_CDATA;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::CDATA has been deprecated with message: Use PARTIAL_CDATA instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
217 9
            case self::HTMLTAG: return self::PARTIAL_HTMLTAG;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::HTMLTAG has been deprecated with message: Use PARTIAL_HTMLTAG instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
218 6
            case self::HTMLBLOCKOPEN: return self::PARTIAL_HTMLBLOCKOPEN;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::HTMLBLOCKOPEN has been deprecated with message: Use PARTIAL_HTMLBLOCKOPEN instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
219 3
            case self::LINK_TITLE: return self::PARTIAL_LINK_TITLE;
1 ignored issue
show
Deprecated Code introduced by
The constant League\CommonMark\Util\RegexHelper::LINK_TITLE has been deprecated with message: Use PARTIAL_LINK_TITLE instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
220
        }
221
    }
222
223
    /**
224
     * @return string
225
     *
226
     * @deprecated Use PARTIAL_HTMLTAG and wrap it yourself instead
227
     */
228 3
    public function getHtmlTagRegex()
229
    {
230 3
        @trigger_error('RegexHelper::getHtmlTagRegex() has been deprecated. Use the RegexHelper::PARTIAL_HTMLTAG constant instead.', E_USER_DEPRECATED);
231
232 3
        return '/^' . self::PARTIAL_HTMLTAG . '/i';
233
    }
234
235
    /**
236
     * @return string
237
     *
238
     * @deprecated Use PARTIAL_LINK_TITLE and wrap it yourself instead
239
     */
240 3
    public function getLinkTitleRegex()
241
    {
242 3
        @trigger_error('RegexHelper::getLinkTitleRegex() has been deprecated. Use the RegexHelper::PARTIAL_LINK_TITLE constant instead.', E_USER_DEPRECATED);
243
244 3
        return '/' . self::PARTIAL_LINK_TITLE . '/';
245
    }
246
247
    /**
248
     * @return string
249
     *
250
     * @deprecated Use REGEX_LINK_DESTINATION_BRACES instead
251
     */
252 3
    public function getLinkDestinationBracesRegex()
253
    {
254 3
        @trigger_error('RegexHelper::getLinkDestinationBracesRegex() has been deprecated. Use the RegexHelper::REGEX_LINK_DESTINATION_BRACES constant instead.', E_USER_DEPRECATED);
255
256 3
        return self::REGEX_LINK_DESTINATION_BRACES;
257
    }
258
259
    /**
260
     * @return string
261
     *
262
     * @deprecated Use the REGEX_THEMATIC_BREAK constant directly
263
     */
264 3
    public function getThematicBreakRegex()
265
    {
266 3
        @trigger_error('RegexHelper::getThematicBreakRegex() has been deprecated. Use the RegexHelper::REGEX_THEMATIC_BREAK constant instead.', E_USER_DEPRECATED);
267
268 3
        return self::REGEX_THEMATIC_BREAK;
269
    }
270
271
    /**
272
     * Attempt to match a regex in string s at offset offset
273
     *
274
     * @param string $regex
275
     * @param string $string
276
     * @param int    $offset
277
     *
278
     * @return int|null Index of match, or null
279
     */
280 1776
    public static function matchAt($regex, $string, $offset = 0)
281
    {
282 1776
        $matches = [];
283 1776
        $string = mb_substr($string, $offset, null, 'utf-8');
284 1776
        if (!preg_match($regex, $string, $matches, PREG_OFFSET_CAPTURE)) {
285 1716
            return;
286
        }
287
288
        // PREG_OFFSET_CAPTURE always returns the byte offset, not the char offset, which is annoying
289 294
        $charPos = mb_strlen(mb_strcut($string, 0, $matches[0][1], 'utf-8'), 'utf-8');
290
291 294
        return $offset + $charPos;
292
    }
293
294
    /**
295
     * Functional wrapper around preg_match_all
296
     *
297
     * @param string $pattern
298
     * @param string $subject
299
     * @param int    $offset
300
     *
301
     * @return array|null
302
     */
303 1875
    public static function matchAll($pattern, $subject, $offset = 0)
304
    {
305 1875
        $matches = [];
306 1875
        $subject = substr($subject, $offset);
307 1875
        preg_match_all($pattern, $subject, $matches, PREG_PATTERN_ORDER);
308
309 1875
        $fullMatches = reset($matches);
310 1875
        if (empty($fullMatches)) {
311 1830
            return;
312
        }
313
314 288
        if (count($fullMatches) === 1) {
315 288
            foreach ($matches as &$match) {
316 288
                $match = reset($match);
317 96
            }
318 96
        }
319
320 288
        if (!empty($matches)) {
321 288
            return $matches;
322
        }
323
    }
324
325
    /**
326
     * Replace backslash escapes with literal characters
327
     *
328
     * @param string $string
329
     *
330
     * @return string
331
     */
332 498
    public static function unescape($string)
333
    {
334 498
        $allEscapedChar = '/\\\\(' . self::PARTIAL_ESCAPABLE . ')/';
335
336 498
        $escaped = preg_replace($allEscapedChar, '$1', $string);
337 498
        $replaced = preg_replace_callback('/' . self::PARTIAL_ENTITY . '/i', function ($e) {
338 15
            return Html5Entities::decodeEntity($e[0]);
339 498
        }, $escaped);
340
341 498
        return $replaced;
342
    }
343
344
    /**
345
     * @param int $type HTML block type
346
     *
347
     * @return string|null
348
     */
349 279
    public static function getHtmlBlockOpenRegex($type)
350
    {
351
        switch ($type) {
352 279
            case HtmlBlock::TYPE_1_CODE_CONTAINER:
353 279
                return '/^<(?:script|pre|style)(?:\s|>|$)/i';
354 261
            case HtmlBlock::TYPE_2_COMMENT:
355 261
                return '/^<!--/';
356 246
            case HtmlBlock::TYPE_3:
357 246
                return '/^<[?]/';
358 243
            case HtmlBlock::TYPE_4:
359 243
                return '/^<![A-Z]/';
360 240
            case HtmlBlock::TYPE_5_CDATA:
361 240
                return '/^<!\[CDATA\[/';
362 237
            case HtmlBlock::TYPE_6_BLOCK_ELEMENT:
363 237
                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|meta|nav|noframes|ol|optgroup|option|p|param|section|source|title|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?:\s|[/]?[>]|$)%i';
364 156
            case HtmlBlock::TYPE_7_MISC_ELEMENT:
365 156
                return '/^(?:' . self::PARTIAL_OPENTAG . '|' . self::PARTIAL_CLOSETAG . ')\\s*$/i';
366
        }
367
    }
368
369
    /**
370
     * @param int $type HTML block type
371
     *
372
     * @return string|null
373
     */
374 60
    public static function getHtmlBlockCloseRegex($type)
375
    {
376
        switch ($type) {
377 60
            case HtmlBlock::TYPE_1_CODE_CONTAINER:
378 36
                return '%<\/(?:script|pre|style)>%i';
379 24
            case HtmlBlock::TYPE_2_COMMENT:
380 15
                return '/-->/';
381 9
            case HtmlBlock::TYPE_3:
382 3
                return '/\?>/';
383 6
            case HtmlBlock::TYPE_4:
384 3
                return '/>/';
385 3
            case HtmlBlock::TYPE_5_CDATA:
386 3
                return '/\]\]>/';
387
        }
388
    }
389
390
    /**
391
     * @param string $url
392
     *
393
     * @return bool
394
     */
395 30
    public static function isLinkPotentiallyUnsafe($url)
396
    {
397 30
        return preg_match(self::REGEX_UNSAFE_PROTOCOL, $url) !== 0 && preg_match(self::REGEX_SAFE_DATA_PROTOCOL, $url) === 0;
398
    }
399
}
400