|
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\Event; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\Event\DocumentParsedEvent; |
|
18
|
|
|
use League\CommonMark\Extension\Attributes\Node\Attributes; |
|
19
|
|
|
use League\CommonMark\Extension\Attributes\Node\AttributesInline; |
|
20
|
|
|
use League\CommonMark\Extension\Attributes\Util\AttributesHelper; |
|
21
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; |
|
22
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock; |
|
23
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem; |
|
24
|
|
|
use League\CommonMark\Node\Inline\AbstractInline; |
|
25
|
|
|
use League\CommonMark\Node\Node; |
|
26
|
|
|
|
|
27
|
|
|
final class AttributesListener |
|
28
|
|
|
{ |
|
29
|
|
|
private const DIRECTION_PREFIX = 'prefix'; |
|
30
|
|
|
private const DIRECTION_SUFFIX = 'suffix'; |
|
31
|
|
|
|
|
32
|
|
|
/** @var list<string> */ |
|
|
|
|
|
|
33
|
|
|
private array $allowList; |
|
34
|
|
|
private bool $allowUnsafeLinks; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param list<string> $allowList |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(array $allowList = [], bool $allowUnsafeLinks = true) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->allowList = $allowList; |
|
|
|
|
|
|
42
|
|
|
$this->allowUnsafeLinks = $allowUnsafeLinks; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function processDocument(DocumentParsedEvent $event): void |
|
46
|
|
|
{ |
|
47
|
|
|
foreach ($event->getDocument()->iterator() as $node) { |
|
48
|
|
|
if (! ($node instanceof Attributes || $node instanceof AttributesInline)) { |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
[$target, $direction] = self::findTargetAndDirection($node); |
|
53
|
|
|
|
|
54
|
|
|
if ($target instanceof Node) { |
|
55
|
|
|
$parent = $target->parent(); |
|
56
|
|
|
if ($parent instanceof ListItem && $parent->parent() instanceof ListBlock && $parent->parent()->isTight()) { |
|
57
|
|
|
$target = $parent; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($direction === self::DIRECTION_SUFFIX) { |
|
61
|
|
|
$attributes = AttributesHelper::mergeAttributes($target, $node->getAttributes()); |
|
62
|
|
|
} else { |
|
63
|
|
|
$attributes = AttributesHelper::mergeAttributes($node->getAttributes(), $target); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$target->data->set('attributes', AttributesHelper::filterAttributes($attributes, $this->allowList, $this->allowUnsafeLinks)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$node->detach(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Attributes|AttributesInline $node |
|
75
|
|
|
* |
|
76
|
|
|
* @return array<Node|string|null> |
|
77
|
|
|
*/ |
|
78
|
|
|
private static function findTargetAndDirection($node): array |
|
79
|
|
|
{ |
|
80
|
|
|
$target = null; |
|
81
|
|
|
$direction = null; |
|
82
|
|
|
$previous = $next = $node; |
|
83
|
|
|
while (true) { |
|
84
|
|
|
$previous = self::getPrevious($previous); |
|
85
|
|
|
$next = self::getNext($next); |
|
86
|
|
|
|
|
87
|
|
|
if ($previous === null && $next === null) { |
|
88
|
|
|
if (! $node->parent() instanceof FencedCode) { |
|
89
|
|
|
$target = $node->parent(); |
|
90
|
|
|
$direction = self::DIRECTION_SUFFIX; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
break; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($node instanceof AttributesInline && ($previous === null || ($previous instanceof AbstractInline && $node->isBlock()))) { |
|
97
|
|
|
continue; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($previous !== null && ! self::isAttributesNode($previous)) { |
|
101
|
|
|
$target = $previous; |
|
102
|
|
|
$direction = self::DIRECTION_SUFFIX; |
|
103
|
|
|
|
|
104
|
|
|
break; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($next !== null && ! self::isAttributesNode($next)) { |
|
108
|
|
|
$target = $next; |
|
109
|
|
|
$direction = self::DIRECTION_PREFIX; |
|
110
|
|
|
|
|
111
|
|
|
break; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return [$target, $direction]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get any previous block (sibling or parent) this might apply to |
|
120
|
|
|
*/ |
|
121
|
|
|
private static function getPrevious(?Node $node = null): ?Node |
|
122
|
|
|
{ |
|
123
|
|
|
if ($node instanceof Attributes) { |
|
124
|
|
|
if ($node->getTarget() === Attributes::TARGET_NEXT) { |
|
125
|
|
|
return null; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if ($node->getTarget() === Attributes::TARGET_PARENT) { |
|
129
|
|
|
return $node->parent(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $node instanceof Node ? $node->previous() : null; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get any previous block (sibling or parent) this might apply to |
|
138
|
|
|
*/ |
|
139
|
|
|
private static function getNext(?Node $node = null): ?Node |
|
140
|
|
|
{ |
|
141
|
|
|
if ($node instanceof Attributes && $node->getTarget() !== Attributes::TARGET_NEXT) { |
|
142
|
|
|
return null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $node instanceof Node ? $node->next() : null; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
private static function isAttributesNode(Node $node): bool |
|
149
|
|
|
{ |
|
150
|
|
|
return $node instanceof Attributes || $node instanceof AttributesInline; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
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