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\Parser; 16 17use League\CommonMark\Block\Element\HtmlBlock; 18use League\CommonMark\Block\Element\Paragraph; 19use League\CommonMark\ContextInterface; 20use League\CommonMark\Cursor; 21use League\CommonMark\Util\RegexHelper; 22 23final class HtmlBlockParser implements BlockParserInterface 24{ 25 public function parse(ContextInterface $context, Cursor $cursor): bool 26 { 27 if ($cursor->isIndented()) { 28 return false; 29 } 30 31 if ($cursor->getNextNonSpaceCharacter() !== '<') { 32 return false; 33 } 34 35 $savedState = $cursor->saveState(); 36 37 $cursor->advanceToNextNonSpaceOrTab(); 38 $line = $cursor->getRemainder(); 39 40 for ($blockType = 1; $blockType <= 7; $blockType++) { 41 $match = RegexHelper::matchAt( 42 RegexHelper::getHtmlBlockOpenRegex($blockType), 43 $line 44 ); 45 46 if ($match !== null && ($blockType < 7 || !($context->getContainer() instanceof Paragraph))) { 47 $cursor->restoreState($savedState); 48 $context->addBlock(new HtmlBlock($blockType)); 49 $context->setBlocksParsed(true); 50 51 return true; 52 } 53 } 54 55 $cursor->restoreState($savedState); 56 57 return false; 58 } 59} 60