1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* (c) 2015 Martin Hasoň <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace League\CommonMark\Extension\Attributes\Util; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Node\Node; |
18
|
|
|
use League\CommonMark\Parser\Cursor; |
19
|
|
|
use League\CommonMark\Util\RegexHelper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
final class AttributesHelper |
25
|
|
|
{ |
26
|
|
|
private const SINGLE_ATTRIBUTE = '\s*([.]-?[_a-z][^\s.}]*|[#][^\s}]+|' . RegexHelper::PARTIAL_ATTRIBUTENAME . RegexHelper::PARTIAL_ATTRIBUTEVALUESPEC . ')\s*'; |
27
|
|
|
private const ATTRIBUTE_LIST = '/^{:?(' . self::SINGLE_ATTRIBUTE . ')+}/i'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return array<string, mixed> |
31
|
|
|
*/ |
32
|
120 |
|
public static function parseAttributes(Cursor $cursor): array |
33
|
|
|
{ |
34
|
120 |
|
$state = $cursor->saveState(); |
35
|
120 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
36
|
|
|
|
37
|
|
|
// Quick check to see if we might have attributes |
38
|
120 |
|
if ($cursor->getCharacter() !== '{') { |
39
|
24 |
|
$cursor->restoreState($state); |
40
|
|
|
|
41
|
24 |
|
return []; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Attempt to match the entire attribute list expression |
45
|
|
|
// While this is less performant than checking for '{' now and '}' later, it simplifies |
46
|
|
|
// matching individual attributes since they won't need to look ahead for the closing '}' |
47
|
|
|
// while dealing with the fact that attributes can technically contain curly braces. |
48
|
|
|
// So we'll just match the start and end braces up front. |
49
|
118 |
|
$attributeExpression = $cursor->match(self::ATTRIBUTE_LIST); |
50
|
118 |
|
if ($attributeExpression === null) { |
51
|
14 |
|
$cursor->restoreState($state); |
52
|
|
|
|
53
|
14 |
|
return []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Trim the leading '{' or '{:' and the trailing '}' |
57
|
106 |
|
$attributeExpression = \ltrim(\substr($attributeExpression, 1, -1), ':'); |
58
|
106 |
|
$attributeCursor = new Cursor($attributeExpression); |
59
|
|
|
|
60
|
|
|
/** @var array<string, mixed> $attributes */ |
61
|
106 |
|
$attributes = []; |
62
|
106 |
|
while ($attribute = \trim((string) $attributeCursor->match('/^' . self::SINGLE_ATTRIBUTE . '/i'))) { |
63
|
106 |
|
if ($attribute[0] === '#') { |
64
|
44 |
|
$attributes['id'] = \substr($attribute, 1); |
65
|
|
|
|
66
|
44 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
80 |
|
if ($attribute[0] === '.') { |
70
|
28 |
|
$attributes['class'][] = \substr($attribute, 1); |
71
|
|
|
|
72
|
28 |
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @psalm-suppress PossiblyUndefinedArrayOffset */ |
76
|
66 |
|
[$name, $value] = \explode('=', $attribute, 2); |
77
|
|
|
|
78
|
66 |
|
if ($value === 'true') { |
79
|
6 |
|
$attributes[$name] = true; |
80
|
6 |
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
66 |
|
$first = $value[0]; |
84
|
66 |
|
$last = \substr($value, -1); |
85
|
66 |
|
if (($first === '"' && $last === '"') || ($first === "'" && $last === "'") && \strlen($value) > 1) { |
86
|
42 |
|
$value = \substr($value, 1, -1); |
87
|
|
|
} |
88
|
|
|
|
89
|
66 |
|
if (\strtolower(\trim($name)) === 'class') { |
90
|
6 |
|
foreach (\array_filter(\explode(' ', \trim($value))) as $class) { |
91
|
6 |
|
$attributes['class'][] = $class; |
92
|
|
|
} |
93
|
|
|
} else { |
94
|
66 |
|
$attributes[\trim($name)] = \trim($value); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
106 |
|
if (isset($attributes['class'])) { |
99
|
32 |
|
$attributes['class'] = \implode(' ', (array) $attributes['class']); |
100
|
|
|
} |
101
|
|
|
|
102
|
106 |
|
return $attributes; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Node|array<string, mixed> $attributes1 |
107
|
|
|
* @param Node|array<string, mixed> $attributes2 |
108
|
|
|
* |
109
|
|
|
* @return array<string, mixed> |
110
|
|
|
*/ |
111
|
58 |
|
public static function mergeAttributes($attributes1, $attributes2): array |
112
|
|
|
{ |
113
|
58 |
|
$attributes = []; |
114
|
58 |
|
foreach ([$attributes1, $attributes2] as $arg) { |
115
|
58 |
|
if ($arg instanceof Node) { |
116
|
38 |
|
$arg = $arg->data->get('attributes'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** @var array<string, mixed> $arg */ |
120
|
58 |
|
$arg = (array) $arg; |
121
|
58 |
|
if (isset($arg['class'])) { |
122
|
40 |
|
if (\is_string($arg['class'])) { |
123
|
40 |
|
$arg['class'] = \array_filter(\explode(' ', \trim($arg['class']))); |
124
|
|
|
} |
125
|
|
|
|
126
|
40 |
|
foreach ($arg['class'] as $class) { |
127
|
40 |
|
$attributes['class'][] = $class; |
128
|
|
|
} |
129
|
|
|
|
130
|
40 |
|
unset($arg['class']); |
131
|
|
|
} |
132
|
|
|
|
133
|
58 |
|
$attributes = \array_merge($attributes, $arg); |
134
|
|
|
} |
135
|
|
|
|
136
|
58 |
|
if (isset($attributes['class'])) { |
137
|
40 |
|
$attributes['class'] = \implode(' ', $attributes['class']); |
138
|
|
|
} |
139
|
|
|
|
140
|
58 |
|
return $attributes; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param array<string, mixed> $attributes |
145
|
|
|
* @param list<string> $allowList |
|
|
|
|
146
|
|
|
* |
147
|
|
|
* @return array<string, mixed> |
148
|
|
|
*/ |
149
|
34 |
|
public static function filterAttributes(array $attributes, array $allowList, bool $allowUnsafeLinks): array |
150
|
|
|
{ |
151
|
34 |
|
$allowList = \array_fill_keys($allowList, true); |
152
|
|
|
|
153
|
34 |
|
foreach ($attributes as $name => $value) { |
154
|
34 |
|
$attrNameLower = \strtolower($name); |
155
|
|
|
|
156
|
|
|
// Remove any unsafe links |
157
|
34 |
|
if (! $allowUnsafeLinks && ($attrNameLower === 'href' || $attrNameLower === 'src') && \is_string($value) && RegexHelper::isLinkPotentiallyUnsafe($value)) { |
158
|
6 |
|
unset($attributes[$name]); |
159
|
6 |
|
continue; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// No allowlist? |
163
|
32 |
|
if ($allowList === []) { |
164
|
|
|
// Just remove JS event handlers |
165
|
22 |
|
if (\str_starts_with($attrNameLower, 'on')) { |
166
|
6 |
|
unset($attributes[$name]); |
167
|
|
|
} |
168
|
|
|
|
169
|
22 |
|
continue; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Remove any attributes not in that allowlist (case-sensitive) |
173
|
10 |
|
if (! isset($allowList[$name])) { |
174
|
10 |
|
unset($attributes[$name]); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
34 |
|
return $attributes; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths