*/ abstract class AbstractHtmlProcessor { /** * @var string */ protected const DEFAULT_DOCUMENT_TYPE = ''; /** * @var string */ protected const CONTENT_TYPE_META_TAG = ''; /** * @var string Regular expression part to match tag names that PHP's DOMDocument implementation is not aware are * self-closing. These are mostly HTML5 elements, but for completeness (obsolete) and * (deprecated) are also included. * * @see https://bugs.php.net/bug.php?id=73175 */ protected const PHP_UNRECOGNIZED_VOID_TAGNAME_MATCHER = '(?:command|embed|keygen|source|track|wbr)'; /** * Regular expression part to match tag names that may appear before the start of the `` element. A start tag * for any other element would implicitly start the `` element due to tag omission rules. * * @var string */ protected const TAGNAME_ALLOWED_BEFORE_BODY_MATCHER = '(?:html|head|base|command|link|meta|noscript|script|style|template|title)'; /** * regular expression pattern to match an HTML comment, including delimiters and modifiers * * @var string */ protected const HTML_COMMENT_PATTERN = '/|$)/'; /** * regular expression pattern to match an HTML `|$)%i'; /** * @var \DOMDocument|null */ protected $domDocument = null; /** * @var \DOMXPath */ protected $xPath = null; /** * The constructor. * * Please use `::fromHtml` or `::fromDomDocument` instead. */ private function __construct() { } /** * Builds a new instance from the given HTML. * * @param string $unprocessedHtml raw HTML, must be UTF-encoded, must not be empty * * @return static * * @throws \InvalidArgumentException if $unprocessedHtml is anything other than a non-empty string */ public static function fromHtml(string $unprocessedHtml): self { if ($unprocessedHtml === '') { throw new \InvalidArgumentException('The provided HTML must not be empty.', 1515763647); } $instance = new static(); $instance->setHtml($unprocessedHtml); return $instance; } /** * Builds a new instance from the given DOM document. * * @param \DOMDocument $document a DOM document returned by getDomDocument() of another instance * * @return static */ public static function fromDomDocument(\DOMDocument $document): self { $instance = new static(); $instance->setDomDocument($document); return $instance; } /** * Sets the HTML to process. * * @param string $html the HTML to process, must be UTF-8-encoded */ private function setHtml(string $html): void { $this->createUnifiedDomDocument($html); } /** * Provides access to the internal DOMDocument representation of the HTML in its current state. * * @return \DOMDocument * * @throws \UnexpectedValueException */ public function getDomDocument(): \DOMDocument { if ($this->domDocument === null) { throw new \UnexpectedValueException( ( self::class . '::setDomDocument() has not yet been called on ' . static::class ), 1570472239 ); } return $this->domDocument; } /** * @param \DOMDocument $domDocument */ private function setDomDocument(\DOMDocument $domDocument): void { $this->domDocument = $domDocument; $this->xPath = new \DOMXPath($this->domDocument); } /** * Renders the normalized and processed HTML. * * @return string */ public function render(): string { $htmlWithPossibleErroneousClosingTags = $this->getDomDocument()->saveHTML(); return $this->removeSelfClosingTagsClosingTags($htmlWithPossibleErroneousClosingTags); } /** * Renders the content of the BODY element of the normalized and processed HTML. * * @return string */ public function renderBodyContent(): string { $htmlWithPossibleErroneousClosingTags = $this->getDomDocument()->saveHTML($this->getBodyElement()); $bodyNodeHtml = $this->removeSelfClosingTagsClosingTags($htmlWithPossibleErroneousClosingTags); return \preg_replace('%]*+)?+>%', '', $bodyNodeHtml); } /** * Eliminates any invalid closing tags for void elements from the given HTML. * * @param string $html * * @return string */ private function removeSelfClosingTagsClosingTags(string $html): string { return \preg_replace('%%', '', $html); } /** * Returns the BODY element. * * This method assumes that there always is a BODY element. * * @return \DOMElement */ private function getBodyElement(): \DOMElement { return $this->getDomDocument()->getElementsByTagName('body')->item(0); } /** * Creates a DOM document from the given HTML and stores it in $this->domDocument. * * The DOM document will always have a BODY element and a document type. * * @param string $html */ private function createUnifiedDomDocument(string $html): void { $this->createRawDomDocument($html); $this->ensureExistenceOfBodyElement(); } /** * Creates a DOMDocument instance from the given HTML and stores it in $this->domDocument. * * @param string $html */ private function createRawDomDocument(string $html): void { $domDocument = new \DOMDocument(); $domDocument->strictErrorChecking = false; $domDocument->formatOutput = true; $libXmlState = \libxml_use_internal_errors(true); $domDocument->loadHTML($this->prepareHtmlForDomConversion($html)); \libxml_clear_errors(); \libxml_use_internal_errors($libXmlState); $this->setDomDocument($domDocument); } /** * Returns the HTML with added document type, Content-Type meta tag, and self-closing slashes, if needed, * ensuring that the HTML will be good for creating a DOM document from it. * * @param string $html * * @return string the unified HTML */ private function prepareHtmlForDomConversion(string $html): string { $htmlWithSelfClosingSlashes = $this->ensurePhpUnrecognizedSelfClosingTagsAreXml($html); $htmlWithDocumentType = $this->ensureDocumentType($htmlWithSelfClosingSlashes); return $this->addContentTypeMetaTag($htmlWithDocumentType); } /** * Makes sure that the passed HTML has a document type, with lowercase "html". * * @param string $html * * @return string HTML with document type */ private function ensureDocumentType(string $html): string { $hasDocumentType = \stripos($html, 'normalizeDocumentType($html); } return self::DEFAULT_DOCUMENT_TYPE . $html; } /** * Makes sure the document type in the passed HTML has lowercase "html". * * @param string $html * * @return string HTML with normalized document type */ private function normalizeDocumentType(string $html): string { // Limit to replacing the first occurrence: as an optimization; and in case an example exists as unescaped text. return \preg_replace( '/])/i', 'hasContentTypeMetaTagInHead($html)) { return $html; } // We are trying to insert the meta tag to the right spot in the DOM. // If we just prepended it to the HTML, we would lose attributes set to the HTML tag. $hasHeadTag = \preg_match('/]/i', $html); $hasHtmlTag = \stripos($html, '])([^>]*+)>/i', '' . self::CONTENT_TYPE_META_TAG, $html ); } elseif ($hasHtmlTag) { $reworkedHtml = \preg_replace( '//i', '' . self::CONTENT_TYPE_META_TAG . '', $html ); } else { $reworkedHtml = self::CONTENT_TYPE_META_TAG . $html; } return $reworkedHtml; } /** * Tests whether the given HTML has a valid `Content-Type` metadata element within the `` element. Due to tag * omission rules, HTML parsers are expected to end the `` element and start the `` element upon * encountering a start tag for any element which is permitted only within the ``. * * @param string $html * * @return bool */ private function hasContentTypeMetaTagInHead(string $html): bool { \preg_match('%^.*?(?=]*\\shttp-equiv=(["\']?+)Content-Type\\g{-1}[\\s/>])%is', $html, $matches); if (isset($matches[0])) { $htmlBefore = $matches[0]; try { $hasContentTypeMetaTagInHead = !$this->hasEndOfHeadElement($htmlBefore); } catch (\RuntimeException $exception) { // If something unexpected occurs, assume the `Content-Type` that was found is valid. \trigger_error($exception->getMessage()); $hasContentTypeMetaTagInHead = true; } } else { $hasContentTypeMetaTagInHead = false; } return $hasContentTypeMetaTagInHead; } /** * Tests whether the `` element ends within the given HTML. Due to tag omission rules, HTML parsers are * expected to end the `` element and start the `` element upon encountering a start tag for any element * which is permitted only within the ``. * * @param string $html * * @return bool * * @throws \RuntimeException */ private function hasEndOfHeadElement(string $html): bool { $headEndTagMatchCount = \preg_match('%<(?!' . self::TAGNAME_ALLOWED_BEFORE_BODY_MATCHER . '[\\s/>])\\w|%i', $html); if (\is_int($headEndTagMatchCount) && $headEndTagMatchCount > 0) { // An exception to the implicit end of the `` is any content within a `