1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This is part of the webuni/commonmark-attributes-extension package. |
5
|
|
|
* |
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
7
|
|
|
* (c) Webuni s.r.o. <[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
|
|
|
namespace Webuni\CommonMark\AttributesExtension; |
14
|
|
|
|
15
|
|
|
use League\CommonMark\Block\Element\AbstractBlock; |
16
|
|
|
use League\CommonMark\Cursor; |
17
|
|
|
use League\CommonMark\Inline\Element\AbstractInline; |
18
|
|
|
use League\CommonMark\Util\RegexHelper; |
19
|
|
|
|
20
|
|
|
class AttributesUtils |
21
|
|
|
{ |
22
|
|
|
private static $regexp; |
23
|
|
|
|
24
|
5 |
|
public static function parse(Cursor $cursor) |
25
|
|
|
{ |
26
|
5 |
|
if (null === self::$regexp) { |
27
|
1 |
|
$regex = RegexHelper::getInstance(); |
|
|
|
|
28
|
|
|
|
29
|
1 |
|
self::$regexp = sprintf( |
30
|
1 |
|
'/^\s*([.#][_a-z0-9-]+|%s%s)(?<!})\s*/i', |
31
|
1 |
|
$regex->getPartialRegex(RegexHelper::ATTRIBUTENAME), |
|
|
|
|
32
|
1 |
|
$regex->getPartialRegex(RegexHelper::ATTRIBUTEVALUESPEC) |
|
|
|
|
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
5 |
|
$state = $cursor->saveState(); |
37
|
5 |
|
$cursor->advanceToNextNonSpaceOrNewline(); |
38
|
5 |
|
if ('{' !== $cursor->getCharacter()) { |
39
|
4 |
|
$cursor->restoreState($state); |
40
|
|
|
|
41
|
4 |
|
return []; |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
$cursor->advanceBy(1); |
45
|
5 |
|
if (':' === $cursor->getCharacter()) { |
46
|
|
|
$cursor->advanceBy(1); |
47
|
|
|
} |
48
|
|
|
|
49
|
5 |
|
$attributes = []; |
50
|
5 |
|
while ($attribute = trim($cursor->match(self::$regexp))) { |
51
|
5 |
|
if ('#' === $attribute[0]) { |
52
|
3 |
|
$attributes['id'] = substr($attribute, 1); |
53
|
|
|
|
54
|
3 |
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
5 |
|
if ('.' === $attribute[0]) { |
58
|
5 |
|
$attributes['class'][] = substr($attribute, 1); |
59
|
|
|
|
60
|
5 |
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
list($name, $value) = explode('=', $attribute, 2); |
64
|
1 |
|
$first = $value[0]; |
65
|
1 |
|
$last = substr($value, -1); |
66
|
1 |
|
if ((('"' === $first && '"' === $last) || ("'" === $first && "'" === $last)) && strlen($value) > 1) { |
67
|
1 |
|
$value = substr($value, 1, -1); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
if ('class' === strtolower(trim($name))) { |
71
|
|
View Code Duplication |
foreach (array_filter(explode(' ', trim($value))) as $class) { |
|
|
|
|
72
|
|
|
$attributes['class'][] = $class; |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
1 |
|
$attributes[trim($name)] = trim($value); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
if (null === $cursor->match('/}/')) { |
80
|
1 |
|
$cursor->restoreState($state); |
81
|
|
|
|
82
|
1 |
|
return []; |
83
|
|
|
} |
84
|
|
|
|
85
|
5 |
|
if (!count($attributes)) { |
86
|
1 |
|
$cursor->restoreState($state); |
87
|
|
|
|
88
|
1 |
|
return []; |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
if (isset($attributes['class'])) { |
92
|
5 |
|
$attributes['class'] = implode(' ', $attributes['class']); |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
return $attributes; |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
public static function merge($attributes1, $attributes2) |
99
|
|
|
{ |
100
|
5 |
|
$attributes = []; |
101
|
5 |
|
foreach ([$attributes1, $attributes2] as $arg) { |
102
|
5 |
|
if ($arg instanceof AbstractBlock || $arg instanceof AbstractInline) { |
103
|
5 |
|
$arg = isset($arg->data['attributes']) ? $arg->data['attributes'] : []; |
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
|
$arg = (array) $arg; |
107
|
5 |
|
if (isset($arg['class'])) { |
108
|
5 |
View Code Duplication |
foreach (array_filter(explode(' ', trim($arg['class']))) as $class) { |
|
|
|
|
109
|
5 |
|
$attributes['class'][] = $class; |
110
|
|
|
} |
111
|
5 |
|
unset($arg['class']); |
112
|
|
|
} |
113
|
5 |
|
$attributes = array_merge($attributes, $arg); |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
if (isset($attributes['class'])) { |
117
|
5 |
|
$attributes['class'] = implode(' ', $attributes['class']); |
118
|
|
|
} |
119
|
|
|
|
120
|
5 |
|
return $attributes; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This method 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 method will be removed from the class and what other method or class to use instead.