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\Parser; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Cursor; |
18
|
|
|
use League\CommonMark\Util\RegexHelper; |
19
|
|
|
|
20
|
|
|
trait AttributesParserTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param Cursor $cursor |
24
|
|
|
* |
25
|
|
|
* @return array<string, mixed> |
|
|
|
|
26
|
|
|
*/ |
27
|
18 |
|
private function parseAttributes(Cursor $cursor): array |
28
|
|
|
{ |
29
|
18 |
|
$state = $cursor->saveState(); |
30
|
18 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
31
|
18 |
|
if ($cursor->getCharacter() !== '{') { |
32
|
15 |
|
$cursor->restoreState($state); |
33
|
|
|
|
34
|
15 |
|
return []; |
35
|
|
|
} |
36
|
|
|
|
37
|
18 |
|
$cursor->advanceBy(1); |
38
|
18 |
|
if ($cursor->getCharacter() === ':') { |
39
|
|
|
$cursor->advanceBy(1); |
40
|
|
|
} |
41
|
|
|
|
42
|
18 |
|
$attributes = []; |
43
|
18 |
|
$regex = '/^\s*([.#][_a-z0-9-]+|' . RegexHelper::PARTIAL_ATTRIBUTENAME . RegexHelper::PARTIAL_ATTRIBUTEVALUESPEC . ')(?<!})\s*/i'; |
44
|
18 |
|
while ($attribute = \trim((string) $cursor->match($regex))) { |
45
|
18 |
|
if ($attribute[0] === '#') { |
46
|
12 |
|
$attributes['id'] = \substr($attribute, 1); |
47
|
|
|
|
48
|
12 |
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
18 |
|
if ($attribute[0] === '.') { |
52
|
18 |
|
$attributes['class'][] = \substr($attribute, 1); |
53
|
|
|
|
54
|
18 |
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
[$name, $value] = \explode('=', $attribute, 2); |
|
|
|
|
58
|
6 |
|
$first = $value[0]; |
59
|
6 |
|
$last = \substr($value, -1); |
60
|
6 |
|
if ((($first === '"' && $last === '"') || ($first === "'" && $last === "'")) && \strlen($value) > 1) { |
61
|
6 |
|
$value = \substr($value, 1, -1); |
62
|
|
|
} |
63
|
|
|
|
64
|
6 |
|
if (\strtolower(\trim($name)) === 'class') { |
65
|
|
|
foreach (\array_filter(\explode(' ', \trim($value))) as $class) { |
66
|
|
|
$attributes['class'][] = $class; |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
6 |
|
$attributes[trim($name)] = trim($value); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
18 |
|
if ($cursor->match('/}/') === null) { |
74
|
3 |
|
$cursor->restoreState($state); |
75
|
|
|
|
76
|
3 |
|
return []; |
77
|
|
|
} |
78
|
|
|
|
79
|
18 |
|
if ($attributes === []) { |
80
|
3 |
|
$cursor->restoreState($state); |
81
|
|
|
|
82
|
3 |
|
return []; |
83
|
|
|
} |
84
|
|
|
|
85
|
18 |
|
if (isset($attributes['class'])) { |
86
|
18 |
|
$attributes['class'] = \implode(' ', (array) $attributes['class']); |
87
|
|
|
} |
88
|
|
|
|
89
|
18 |
|
return $attributes; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.