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
|
96 |
|
public static function parseAttributes(Cursor $cursor): array |
33
|
|
|
{ |
34
|
96 |
|
$state = $cursor->saveState(); |
35
|
96 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
36
|
|
|
|
37
|
|
|
// Quick check to see if we might have attributes |
38
|
96 |
|
if ($cursor->getCharacter() !== '{') { |
39
|
16 |
|
$cursor->restoreState($state); |
40
|
|
|
|
41
|
16 |
|
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
|
94 |
|
$attributeExpression = $cursor->match(self::ATTRIBUTE_LIST); |
50
|
94 |
|
if ($attributeExpression === null) { |
51
|
8 |
|
$cursor->restoreState($state); |
52
|
|
|
|
53
|
8 |
|
return []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Trim the leading '{' or '{:' and the trailing '}' |
57
|
88 |
|
$attributeExpression = \ltrim(\substr($attributeExpression, 1, -1), ':'); |
58
|
88 |
|
$attributeCursor = new Cursor($attributeExpression); |
59
|
|
|
|
60
|
|
|
/** @var array<string, mixed> $attributes */ |
61
|
88 |
|
$attributes = []; |
62
|
88 |
|
while ($attribute = \trim((string) $attributeCursor->match('/^' . self::SINGLE_ATTRIBUTE . '/i'))) { |
63
|
88 |
|
if ($attribute[0] === '#') { |
64
|
38 |
|
$attributes['id'] = \substr($attribute, 1); |
65
|
|
|
|
66
|
38 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
62 |
|
if ($attribute[0] === '.') { |
70
|
22 |
|
$attributes['class'][] = \substr($attribute, 1); |
71
|
|
|
|
72
|
22 |
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @psalm-suppress PossiblyUndefinedArrayOffset */ |
76
|
50 |
|
[$name, $value] = \explode('=', $attribute, 2); |
77
|
|
|
|
78
|
50 |
|
$first = $value[0]; |
79
|
50 |
|
$last = \substr($value, -1); |
80
|
50 |
|
if (($first === '"' && $last === '"') || ($first === "'" && $last === "'") && \strlen($value) > 1) { |
81
|
32 |
|
$value = \substr($value, 1, -1); |
82
|
|
|
} |
83
|
|
|
|
84
|
50 |
|
if (\strtolower(\trim($name)) === 'class') { |
85
|
2 |
|
foreach (\array_filter(\explode(' ', \trim($value))) as $class) { |
86
|
2 |
|
$attributes['class'][] = $class; |
87
|
|
|
} |
88
|
|
|
} else { |
89
|
50 |
|
$attributes[\trim($name)] = \trim($value); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
88 |
|
if (isset($attributes['class'])) { |
94
|
22 |
|
$attributes['class'] = \implode(' ', (array) $attributes['class']); |
95
|
|
|
} |
96
|
|
|
|
97
|
88 |
|
return $attributes; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Node|array<string, mixed> $attributes1 |
102
|
|
|
* @param Node|array<string, mixed> $attributes2 |
103
|
|
|
* |
104
|
|
|
* @return array<string, mixed> |
105
|
|
|
*/ |
106
|
50 |
|
public static function mergeAttributes($attributes1, $attributes2): array |
107
|
|
|
{ |
108
|
50 |
|
$attributes = []; |
109
|
50 |
|
foreach ([$attributes1, $attributes2] as $arg) { |
110
|
50 |
|
if ($arg instanceof Node) { |
111
|
30 |
|
$arg = $arg->data->get('attributes'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** @var array<string, mixed> $arg */ |
115
|
50 |
|
$arg = (array) $arg; |
116
|
50 |
|
if (isset($arg['class'])) { |
117
|
36 |
|
if (\is_string($arg['class'])) { |
118
|
36 |
|
$arg['class'] = \array_filter(\explode(' ', \trim($arg['class']))); |
119
|
|
|
} |
120
|
|
|
|
121
|
36 |
|
foreach ($arg['class'] as $class) { |
122
|
36 |
|
$attributes['class'][] = $class; |
123
|
|
|
} |
124
|
|
|
|
125
|
36 |
|
unset($arg['class']); |
126
|
|
|
} |
127
|
|
|
|
128
|
50 |
|
$attributes = \array_merge($attributes, $arg); |
129
|
|
|
} |
130
|
|
|
|
131
|
50 |
|
if (isset($attributes['class'])) { |
132
|
36 |
|
$attributes['class'] = \implode(' ', $attributes['class']); |
133
|
|
|
} |
134
|
|
|
|
135
|
50 |
|
return $attributes; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|