doc = $doc; $this->modern = $modern; } public static function supports_html5() { /** * Filters whether to use PHP 8.4's HTML5 parser (when available). * * @param bool $use */ return class_exists( '\Dom\HTMLDocument' ) && apply_filters( 'langferry_html5_parser', true ); } /** * Parse a full HTML document. Returns null if parsing is unsafe/failed. */ public static function parse_document( $html ) { if ( self::supports_html5() ) { try { $doc = \Dom\HTMLDocument::createFromString( $html, LIBXML_NOERROR, 'UTF-8' ); return new self( $doc, true ); } catch ( \Throwable $e ) { return null; } } if ( ! class_exists( '\DOMDocument' ) ) { return null; } $doc = new \DOMDocument( '1.0', 'UTF-8' ); $previous = libxml_use_internal_errors( true ); $ok = $doc->loadHTML( '' . $html, LIBXML_HTML_NODEFDTD | LIBXML_NONET | LIBXML_PARSEHUGE | LIBXML_COMPACT ); libxml_clear_errors(); libxml_use_internal_errors( $previous ); if ( ! $ok ) { return null; } foreach ( iterator_to_array( $doc->childNodes ) as $child ) { if ( XML_PI_NODE === $child->nodeType ) { $doc->removeChild( $child ); } } return new self( $doc, false ); } public function serialize() { if ( $this->modern ) { return $this->doc->saveHtml(); } // libxml's whole-document saveHTML() writes every non-ASCII character as an // entity; serializing node by node keeps UTF-8 intact. $out = ''; foreach ( $this->doc->childNodes as $child ) { if ( XML_DOCUMENT_TYPE_NODE === $child->nodeType ) { $out .= 'name . ( $child->publicId ? ' PUBLIC "' . $child->publicId . '"' : '' ) . ( $child->systemId ? ( $child->publicId ? '' : ' SYSTEM' ) . ' "' . $child->systemId . '"' : '' ) . ">\n"; continue; } $out .= $this->doc->saveHTML( $child ); } return $out; } public function root() { return $this->doc->documentElement; } public function create_text( $text ) { return $this->doc->createTextNode( $text ); } public function create_element( $tag ) { return $this->doc->createElement( $tag ); } /** * Parse an HTML fragment into nodes owned by this document. * * @return array|null Node list, or null if the fragment could not be parsed. */ public function fragment_nodes( $html ) { if ( $this->modern ) { try { $container = $this->doc->createElement( 'div' ); $container->innerHTML = $html; return iterator_to_array( $container->childNodes ); } catch ( \Throwable $e ) { return null; } } $tmp = new \DOMDocument( '1.0', 'UTF-8' ); $previous = libxml_use_internal_errors( true ); $ok = $tmp->loadHTML( '
' . $html . '
', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NONET ); libxml_clear_errors(); libxml_use_internal_errors( $previous ); if ( ! $ok ) { return null; } $root = null; foreach ( $tmp->childNodes as $child ) { if ( XML_ELEMENT_NODE === $child->nodeType ) { $root = $child; break; } } if ( ! $root ) { return null; } $nodes = array(); foreach ( $root->childNodes as $child ) { $nodes[] = $this->doc->importNode( $child, true ); } return $nodes; } /** * A template whose contents hold a script's own placeholders, which translation would break: * {{ }} and {% %} (Mustache, Twig), <% %> (Underscore, EJS), <# #> (WordPress's wp.template), ${ }. */ const SCRIPT_TEMPLATE = '/\{\{|\{%|<%|<%|<#|<#|\$\{/'; /** * The markup inside an element, as the browser would see it. */ public static function inner_html( $el ) { if ( property_exists( $el, 'innerHTML' ) || $el instanceof \Dom\Element ) { return (string) $el->innerHTML; } $out = ''; foreach ( $el->childNodes as $child ) { $out .= $el->ownerDocument->saveHTML( $child ); } return $out; } /** * The markup inside . */ public function body_inner_html() { $body = $this->modern ? $this->doc->body : $this->doc->getElementsByTagName( 'body' )->item( 0 ); return $body ? self::inner_html( $body ) : ''; } /** * Visit each