1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
9
|
|
|
* |
10
|
|
|
* Additional emphasis processing code based on commonmark-java (https://github.com/atlassian/commonmark-java) |
11
|
|
|
* - (c) Atlassian Pty Ltd |
12
|
|
|
* |
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
14
|
|
|
* file that was distributed with this source code. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace League\CommonMark\Node\Inline; |
18
|
|
|
|
19
|
|
|
use League\CommonMark\Node\Node; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
final class AdjacentTextMerger |
25
|
|
|
{ |
26
|
2490 |
|
public static function mergeChildNodes(Node $node): void |
27
|
|
|
{ |
28
|
|
|
// No children or just one child node, no need for merging |
29
|
2490 |
|
if ($node->firstChild() === $node->lastChild() || $node->firstChild() === null || $node->lastChild() === null) { |
30
|
1455 |
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @psalm-suppress PossiblyNullArgument */ |
34
|
1296 |
|
self::mergeTextNodesInclusive($node->firstChild(), $node->lastChild()); |
35
|
1296 |
|
} |
36
|
|
|
|
37
|
483 |
|
public static function mergeTextNodesBetweenExclusive(Node $fromNode, Node $toNode): void |
38
|
|
|
{ |
39
|
|
|
// No nodes between them |
40
|
483 |
|
if ($fromNode === $toNode || $fromNode->next() === $toNode || $fromNode->next() === null || $toNode->previous() === null) { |
41
|
3 |
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @psalm-suppress PossiblyNullArgument */ |
45
|
483 |
|
self::mergeTextNodesInclusive($fromNode->next(), $toNode->previous()); |
46
|
483 |
|
} |
47
|
|
|
|
48
|
1485 |
|
private static function mergeTextNodesInclusive(Node $fromNode, Node $toNode): void |
49
|
|
|
{ |
50
|
1485 |
|
$first = null; |
51
|
1485 |
|
$last = null; |
52
|
|
|
|
53
|
1485 |
|
$node = $fromNode; |
54
|
1485 |
|
while ($node !== null) { |
55
|
1485 |
|
if ($node instanceof Text) { |
56
|
1461 |
|
if ($first === null) { |
57
|
1461 |
|
$first = $node; |
58
|
|
|
} |
59
|
|
|
|
60
|
1461 |
|
$last = $node; |
61
|
|
|
} else { |
62
|
1110 |
|
self::mergeIfNeeded($first, $last); |
63
|
1110 |
|
$first = null; |
64
|
1110 |
|
$last = null; |
65
|
|
|
} |
66
|
|
|
|
67
|
1485 |
|
if ($node === $toNode) { |
68
|
1485 |
|
break; |
69
|
|
|
} |
70
|
|
|
|
71
|
1422 |
|
$node = $node->next(); |
72
|
|
|
} |
73
|
|
|
|
74
|
1485 |
|
self::mergeIfNeeded($first, $last); |
75
|
1485 |
|
} |
76
|
|
|
|
77
|
1485 |
|
private static function mergeIfNeeded(?Text $first, ?Text $last): void |
78
|
|
|
{ |
79
|
1485 |
|
if ($first === null || $last === null || $first === $last) { |
80
|
|
|
// No merging needed |
81
|
1140 |
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
516 |
|
$s = $first->getLiteral(); |
85
|
|
|
|
86
|
516 |
|
$node = $first->next(); |
87
|
516 |
|
$stop = $last->next(); |
88
|
516 |
|
while ($node !== $stop && $node instanceof Text) { |
89
|
516 |
|
$s .= $node->getLiteral(); |
90
|
516 |
|
$unlink = $node; |
91
|
516 |
|
$node = $node->next(); |
92
|
516 |
|
$unlink->detach(); |
93
|
|
|
} |
94
|
|
|
|
95
|
516 |
|
$first->setLiteral($s); |
96
|
516 |
|
} |
97
|
|
|
} |
98
|
|
|
|