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 REGEX = '/^\s*([.#][_a-z0-9-]+|' . RegexHelper::PARTIAL_ATTRIBUTENAME . RegexHelper::PARTIAL_ATTRIBUTEVALUESPEC . ')(?<!})\s*/i'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return array<string, mixed> |
30
|
|
|
*/ |
31
|
111 |
|
public static function parseAttributes(Cursor $cursor): array |
32
|
|
|
{ |
33
|
111 |
|
$state = $cursor->saveState(); |
34
|
111 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
35
|
111 |
|
if ($cursor->getCharacter() !== '{') { |
36
|
21 |
|
$cursor->restoreState($state); |
37
|
|
|
|
38
|
21 |
|
return []; |
39
|
|
|
} |
40
|
|
|
|
41
|
108 |
|
$cursor->advanceBy(1); |
42
|
108 |
|
if ($cursor->getCharacter() === ':') { |
43
|
57 |
|
$cursor->advanceBy(1); |
44
|
|
|
} |
45
|
|
|
|
46
|
108 |
|
$attributes = []; |
47
|
108 |
|
while ($attribute = \trim((string) $cursor->match(self::REGEX))) { |
48
|
102 |
|
if ($attribute[0] === '#') { |
49
|
54 |
|
$attributes['id'] = \substr($attribute, 1); |
50
|
|
|
|
51
|
54 |
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
63 |
|
if ($attribute[0] === '.') { |
55
|
30 |
|
$attributes['class'][] = \substr($attribute, 1); |
56
|
|
|
|
57
|
30 |
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
45 |
|
[$name, $value] = \explode('=', $attribute, 2); |
61
|
|
|
|
62
|
45 |
|
$first = $value[0]; |
63
|
45 |
|
$last = \substr($value, -1); |
64
|
45 |
|
if (($first === '"' && $last === '"') || ($first === "'" && $last === "'") && \strlen($value) > 1) { |
65
|
45 |
|
$value = \substr($value, 1, -1); |
66
|
|
|
} |
67
|
|
|
|
68
|
45 |
|
if (\strtolower(\trim($name)) === 'class') { |
69
|
|
|
foreach (\array_filter(\explode(' ', \trim($value))) as $class) { |
70
|
|
|
$attributes['class'][] = $class; |
71
|
|
|
} |
72
|
|
|
} else { |
73
|
45 |
|
$attributes[\trim($name)] = \trim($value); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
108 |
|
if ($cursor->match('/}/') === null) { |
78
|
6 |
|
$cursor->restoreState($state); |
79
|
|
|
|
80
|
6 |
|
return []; |
81
|
|
|
} |
82
|
|
|
|
83
|
105 |
|
if ($attributes === []) { |
84
|
9 |
|
$cursor->restoreState($state); |
85
|
|
|
|
86
|
9 |
|
return []; |
87
|
|
|
} |
88
|
|
|
|
89
|
99 |
|
if (isset($attributes['class'])) { |
90
|
30 |
|
$attributes['class'] = \implode(' ', (array) $attributes['class']); |
91
|
|
|
} |
92
|
|
|
|
93
|
99 |
|
return $attributes; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Node|array<string, mixed> $attributes1 |
98
|
|
|
* @param Node|array<string, mixed> $attributes2 |
99
|
|
|
* |
100
|
|
|
* @return array<string, mixed> |
101
|
|
|
*/ |
102
|
51 |
|
public static function mergeAttributes($attributes1, $attributes2): array |
103
|
|
|
{ |
104
|
51 |
|
$attributes = []; |
105
|
51 |
|
foreach ([$attributes1, $attributes2] as $arg) { |
106
|
51 |
|
if ($arg instanceof Node) { |
107
|
42 |
|
$arg = $arg->data->get('attributes'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** @var array<string, mixed> $arg */ |
111
|
51 |
|
$arg = (array) $arg; |
112
|
51 |
|
if (isset($arg['class'])) { |
113
|
45 |
|
if (\is_string($arg['class'])) { |
114
|
45 |
|
$arg['class'] = \array_filter(\explode(' ', \trim($arg['class']))); |
115
|
|
|
} |
116
|
|
|
|
117
|
45 |
|
foreach ($arg['class'] as $class) { |
118
|
45 |
|
$attributes['class'][] = $class; |
119
|
|
|
} |
120
|
|
|
|
121
|
45 |
|
unset($arg['class']); |
122
|
|
|
} |
123
|
|
|
|
124
|
51 |
|
$attributes = \array_merge($attributes, $arg); |
125
|
|
|
} |
126
|
|
|
|
127
|
51 |
|
if (isset($attributes['class'])) { |
128
|
45 |
|
$attributes['class'] = \implode(' ', $attributes['class']); |
129
|
|
|
} |
130
|
|
|
|
131
|
51 |
|
return $attributes; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|