| 148 |
lars |
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 <colinodell@gmail.com>
|
|
|
9 |
*
|
|
|
10 |
* For the full copyright and license information, please view the LICENSE
|
|
|
11 |
* file that was distributed with this source code.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
namespace League\CommonMark\Extension\CommonMark\Parser\Block;
|
|
|
15 |
|
|
|
16 |
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
|
|
|
17 |
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
|
|
|
18 |
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
|
|
|
19 |
use League\CommonMark\Node\Block\AbstractBlock;
|
|
|
20 |
use League\CommonMark\Node\Block\Paragraph;
|
|
|
21 |
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
|
|
|
22 |
use League\CommonMark\Parser\Block\BlockContinue;
|
|
|
23 |
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
|
|
|
24 |
use League\CommonMark\Parser\Cursor;
|
|
|
25 |
|
|
|
26 |
final class ListItemParser extends AbstractBlockContinueParser
|
|
|
27 |
{
|
|
|
28 |
/** @psalm-readonly */
|
|
|
29 |
private ListItem $block;
|
|
|
30 |
|
|
|
31 |
private bool $hadBlankLine = false;
|
|
|
32 |
|
|
|
33 |
public function __construct(ListData $listData)
|
|
|
34 |
{
|
|
|
35 |
$this->block = new ListItem($listData);
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function getBlock(): ListItem
|
|
|
39 |
{
|
|
|
40 |
return $this->block;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public function isContainer(): bool
|
|
|
44 |
{
|
|
|
45 |
return true;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public function canContain(AbstractBlock $childBlock): bool
|
|
|
49 |
{
|
|
|
50 |
if ($this->hadBlankLine) {
|
|
|
51 |
// We saw a blank line in this list item, that means the list block is loose.
|
|
|
52 |
//
|
|
|
53 |
// spec: if any of its constituent list items directly contain two block-level elements with a blank line
|
|
|
54 |
// between them
|
|
|
55 |
$parent = $this->block->parent();
|
|
|
56 |
if ($parent instanceof ListBlock) {
|
|
|
57 |
$parent->setTight(false);
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
return true;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
|
|
|
65 |
{
|
|
|
66 |
if ($cursor->isBlank()) {
|
|
|
67 |
if ($this->block->firstChild() === null) {
|
|
|
68 |
// Blank line after empty list item
|
|
|
69 |
return BlockContinue::none();
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$activeBlock = $activeBlockParser->getBlock();
|
|
|
73 |
// If the active block is a code block, blank lines in it should not affect if the list is tight.
|
|
|
74 |
$this->hadBlankLine = $activeBlock instanceof Paragraph || $activeBlock instanceof ListItem;
|
|
|
75 |
$cursor->advanceToNextNonSpaceOrTab();
|
|
|
76 |
|
|
|
77 |
return BlockContinue::at($cursor);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$contentIndent = $this->block->getListData()->markerOffset + $this->getBlock()->getListData()->padding;
|
|
|
81 |
if ($cursor->getIndent() >= $contentIndent) {
|
|
|
82 |
$cursor->advanceBy($contentIndent, true);
|
|
|
83 |
|
|
|
84 |
return BlockContinue::at($cursor);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// Note: We'll hit this case for lazy continuation lines, they will get added later.
|
|
|
88 |
return BlockContinue::none();
|
|
|
89 |
}
|
|
|
90 |
}
|