|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the league/commonmark package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
|
9
|
|
|
* - (c) John MacFarlane |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace League\CommonMark\Extension\CommonMark\Node\Block; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
|
18
|
|
|
use League\CommonMark\Node\Block\AbstractStringContainerBlock; |
|
19
|
|
|
use League\CommonMark\Parser\ContextInterface; |
|
20
|
|
|
use League\CommonMark\Parser\Cursor; |
|
21
|
|
|
use League\CommonMark\Util\RegexHelper; |
|
22
|
|
|
|
|
23
|
|
|
class HtmlBlock extends AbstractStringContainerBlock |
|
24
|
|
|
{ |
|
25
|
|
|
// Any changes to these constants should be reflected in .phpstorm.meta.php |
|
26
|
|
|
const TYPE_1_CODE_CONTAINER = 1; |
|
27
|
|
|
const TYPE_2_COMMENT = 2; |
|
28
|
|
|
const TYPE_3 = 3; |
|
29
|
|
|
const TYPE_4 = 4; |
|
30
|
|
|
const TYPE_5_CDATA = 5; |
|
31
|
|
|
const TYPE_6_BLOCK_ELEMENT = 6; |
|
32
|
|
|
const TYPE_7_MISC_ELEMENT = 7; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $type; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param int $type |
|
41
|
|
|
*/ |
|
42
|
192 |
|
public function __construct(int $type) |
|
43
|
|
|
{ |
|
44
|
192 |
|
parent::__construct(); |
|
45
|
|
|
|
|
46
|
192 |
|
$this->type = $type; |
|
47
|
192 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return int |
|
51
|
|
|
*/ |
|
52
|
6 |
|
public function getType(): int |
|
53
|
|
|
{ |
|
54
|
6 |
|
return $this->type; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
public function setType(int $type): void |
|
58
|
|
|
{ |
|
59
|
3 |
|
$this->type = $type; |
|
60
|
3 |
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
public function canContain(AbstractBlock $block): bool |
|
63
|
|
|
{ |
|
64
|
3 |
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
120 |
|
public function isCode(): bool |
|
68
|
|
|
{ |
|
69
|
120 |
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
138 |
|
public function matchesNextLine(Cursor $cursor): bool |
|
73
|
|
|
{ |
|
74
|
138 |
|
if ($cursor->isBlank() && ($this->type === self::TYPE_6_BLOCK_ELEMENT || $this->type === self::TYPE_7_MISC_ELEMENT)) { |
|
75
|
48 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
117 |
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
168 |
|
public function finalize(ContextInterface $context, int $endLineNumber): void |
|
82
|
|
|
{ |
|
83
|
168 |
|
parent::finalize($context, $endLineNumber); |
|
84
|
|
|
|
|
85
|
168 |
|
$this->finalStringContents = \implode("\n", $this->strings->toArray()); |
|
86
|
168 |
|
} |
|
87
|
|
|
|
|
88
|
168 |
|
public function handleRemainingContents(ContextInterface $context, Cursor $cursor): void |
|
89
|
|
|
{ |
|
90
|
|
|
/** @var self $tip */ |
|
91
|
168 |
|
$tip = $context->getTip(); |
|
92
|
168 |
|
$tip->addLine($cursor->getRemainder()); |
|
93
|
|
|
|
|
94
|
|
|
// Check for end condition |
|
95
|
168 |
|
if ($this->type >= self::TYPE_1_CODE_CONTAINER && $this->type <= self::TYPE_5_CDATA) { |
|
96
|
57 |
|
if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->type)) !== null) { |
|
97
|
54 |
|
$this->finalize($context, $context->getLineNumber()); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
168 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|