1<?php 2 3/* 4 * This file is part of the league/commonmark package. 5 * 6 * (c) Colin O'Dell <colinodell@gmail.com> 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 15namespace League\CommonMark\Block\Element; 16 17use League\CommonMark\ContextInterface; 18use League\CommonMark\Cursor; 19use League\CommonMark\Util\RegexHelper; 20 21class 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 public function __construct(int $type) 41 { 42 parent::__construct(); 43 44 $this->type = $type; 45 } 46 47 /** 48 * @return int 49 */ 50 public function getType(): int 51 { 52 return $this->type; 53 } 54 55 /** 56 * @param int $type 57 * 58 * @return void 59 */ 60 public function setType(int $type) 61 { 62 $this->type = $type; 63 } 64 65 public function canContain(AbstractBlock $block): bool 66 { 67 return false; 68 } 69 70 public function isCode(): bool 71 { 72 return true; 73 } 74 75 public function matchesNextLine(Cursor $cursor): bool 76 { 77 if ($cursor->isBlank() && ($this->type === self::TYPE_6_BLOCK_ELEMENT || $this->type === self::TYPE_7_MISC_ELEMENT)) { 78 return false; 79 } 80 81 return true; 82 } 83 84 public function finalize(ContextInterface $context, int $endLineNumber) 85 { 86 parent::finalize($context, $endLineNumber); 87 88 $this->finalStringContents = \implode("\n", $this->strings->toArray()); 89 } 90 91 public function handleRemainingContents(ContextInterface $context, Cursor $cursor) 92 { 93 /** @var self $tip */ 94 $tip = $context->getTip(); 95 $tip->addLine($cursor->getRemainder()); 96 97 // Check for end condition 98 if ($this->type >= self::TYPE_1_CODE_CONTAINER && $this->type <= self::TYPE_5_CDATA) { 99 if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->type)) !== null) { 100 $this->finalize($context, $context->getLineNumber()); 101 } 102 } 103 } 104} 105