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\Block\Element; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\ContextInterface; |
18
|
|
|
use League\CommonMark\Cursor; |
19
|
|
|
use League\CommonMark\Util\RegexHelper; |
20
|
|
|
|
21
|
|
|
class HtmlBlock extends AbstractStringContainerBlock |
22
|
|
|
{ |
23
|
|
|
// Any changes to these constants should be reflected in .phpstorm.meta.php |
24
|
|
|
const TYPE_1_CODE_CONTAINER = 1; |
25
|
|
|
const TYPE_2_COMMENT = 2; |
26
|
|
|
const TYPE_3 = 3; |
27
|
|
|
const TYPE_4 = 4; |
28
|
|
|
const TYPE_5_CDATA = 5; |
29
|
|
|
const TYPE_6_BLOCK_ELEMENT = 6; |
30
|
|
|
const TYPE_7_MISC_ELEMENT = 7; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $type; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param int $type |
39
|
|
|
*/ |
40
|
189 |
|
public function __construct(int $type) |
41
|
|
|
{ |
42
|
189 |
|
parent::__construct(); |
43
|
|
|
|
44
|
189 |
|
$this->type = $type; |
45
|
189 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
6 |
|
public function getType(): int |
51
|
|
|
{ |
52
|
6 |
|
return $this->type; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param int $type |
57
|
|
|
*/ |
58
|
3 |
|
public function setType(int $type) |
59
|
|
|
{ |
60
|
3 |
|
$this->type = $type; |
61
|
3 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns true if this block can contain the given block as a child node |
65
|
|
|
* |
66
|
|
|
* @param AbstractBlock $block |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
3 |
|
public function canContain(AbstractBlock $block): bool |
71
|
|
|
{ |
72
|
3 |
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Whether this is a code block |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
114 |
|
public function isCode(): bool |
81
|
|
|
{ |
82
|
114 |
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
132 |
|
public function matchesNextLine(Cursor $cursor): bool |
86
|
|
|
{ |
87
|
132 |
|
if ($cursor->isBlank() && ($this->type === self::TYPE_6_BLOCK_ELEMENT || $this->type === self::TYPE_7_MISC_ELEMENT)) { |
88
|
51 |
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
111 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
162 |
|
public function finalize(ContextInterface $context, int $endLineNumber) |
95
|
|
|
{ |
96
|
162 |
|
parent::finalize($context, $endLineNumber); |
97
|
|
|
|
98
|
162 |
|
$this->finalStringContents = \implode("\n", $this->strings->toArray()); |
99
|
162 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param ContextInterface $context |
103
|
|
|
* @param Cursor $cursor |
104
|
|
|
*/ |
105
|
162 |
|
public function handleRemainingContents(ContextInterface $context, Cursor $cursor) |
106
|
|
|
{ |
107
|
|
|
/** @var self $tip */ |
108
|
162 |
|
$tip = $context->getTip(); |
109
|
162 |
|
$tip->addLine($cursor->getRemainder()); |
110
|
|
|
|
111
|
|
|
// Check for end condition |
112
|
162 |
|
if ($this->type >= self::TYPE_1_CODE_CONTAINER && $this->type <= self::TYPE_5_CDATA) { |
113
|
60 |
|
if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->type)) !== null) { |
114
|
57 |
|
$this->finalize($context, $context->getLineNumber()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
162 |
|
} |
118
|
|
|
} |
119
|
|
|
|