|
| 1 | +<?php |
| 2 | + |
| 3 | +declare( strict_types = 1 ); |
| 4 | + |
| 5 | +namespace WMDE\VueJsTemplating; |
| 6 | + |
| 7 | +use DOMDocument; |
| 8 | +use DOMElement; |
| 9 | +use Exception; |
| 10 | +use LibXMLError; |
| 11 | + |
| 12 | +/** |
| 13 | + * Methods for parsing HTML strings and extracting elements from them. |
| 14 | + */ |
| 15 | +class HtmlParser { |
| 16 | + |
| 17 | + /** |
| 18 | + * Parse the given HTML string into a DOM document. |
| 19 | + */ |
| 20 | + public function parseHtml( string $html ): DOMDocument { |
| 21 | + if ( LIBXML_VERSION < 20900 ) { |
| 22 | + $entityLoaderDisabled = libxml_disable_entity_loader( true ); |
| 23 | + } |
| 24 | + $internalErrors = libxml_use_internal_errors( true ); |
| 25 | + $document = new DOMDocument( '1.0', 'UTF-8' ); |
| 26 | + |
| 27 | + // Ensure $html is treated as UTF-8, see https://stackoverflow.com/a/8218649 |
| 28 | + // LIBXML_NOBLANKS Constant excludes "ghost nodes" to avoid violating |
| 29 | + // vue's single root node constraint |
| 30 | + if ( !$document->loadHTML( '<?xml encoding="utf-8" ?>' . $html, LIBXML_NOBLANKS ) ) { |
| 31 | + //TODO Test failure |
| 32 | + } |
| 33 | + |
| 34 | + /** @var LibXMLError[] $errors */ |
| 35 | + $errors = libxml_get_errors(); |
| 36 | + libxml_clear_errors(); |
| 37 | + |
| 38 | + // Restore previous state |
| 39 | + libxml_use_internal_errors( $internalErrors ); |
| 40 | + if ( LIBXML_VERSION < 20900 ) { |
| 41 | + libxml_disable_entity_loader( $entityLoaderDisabled ); |
| 42 | + } |
| 43 | + |
| 44 | + foreach ( $errors as $error ) { |
| 45 | + //TODO html5 tags can fail parsing |
| 46 | + //TODO Throw an exception |
| 47 | + } |
| 48 | + |
| 49 | + return $document; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the root node of the template represented by the given document. |
| 54 | + */ |
| 55 | + public function getRootNode( DOMDocument $document ): DOMElement { |
| 56 | + $htmlElement = $this->getHtmlElement( $document ); |
| 57 | + $headOrBody = $this->getSoleHeadOrBody( $htmlElement ); |
| 58 | + $rootNodeParent = $this->getTemplateElement( $headOrBody ) ?? $headOrBody; |
| 59 | + $rootNode = $this->getOnlySubstantialChild( $rootNodeParent ); |
| 60 | + return $rootNode; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the `<html>` element of the given document. |
| 65 | + */ |
| 66 | + public function getHtmlElement( DOMDocument $document ): DOMElement { |
| 67 | + $documentElement = $document->documentElement; |
| 68 | + if ( $documentElement === null ) { |
| 69 | + throw new Exception( 'Empty document' ); |
| 70 | + } |
| 71 | + if ( $documentElement->tagName !== 'html' ) { |
| 72 | + throw new Exception( "Expected <html>, got <{$documentElement->tagName}>" ); |
| 73 | + } |
| 74 | + return $documentElement; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the `<body>` element of the given document. |
| 79 | + */ |
| 80 | + public function getBodyElement( DOMDocument $document ): DOMElement { |
| 81 | + $htmlElement = $this->getHtmlElement( $document ); |
| 82 | + $bodyElement = $htmlElement->childNodes[0]; |
| 83 | + if ( $bodyElement->tagName !== 'body' ) { |
| 84 | + throw new Exception( "Expected <body>, got <{$bodyElement->tagName}>" ); |
| 85 | + } |
| 86 | + return $bodyElement; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get the `<head>` or `<body>` element of the given document, |
| 91 | + * asserting that it is the only child (cannot have both nor any other children). |
| 92 | + */ |
| 93 | + private function getSoleHeadOrBody( DOMElement $htmlElement ): DOMElement { |
| 94 | + $length = $htmlElement->childNodes->length; |
| 95 | + if ( $length !== 1 ) { |
| 96 | + throw new Exception( "Expected exactly 1 <html> child, got $length" ); |
| 97 | + } |
| 98 | + |
| 99 | + $child = $htmlElement->childNodes[0]; |
| 100 | + $tagName = $child->tagName; |
| 101 | + if ( $tagName !== 'head' && $tagName !== 'body' ) { |
| 102 | + throw new Exception( "Expected <head> or <body>, got <$tagName>" ); |
| 103 | + } |
| 104 | + |
| 105 | + return $child; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Get the `<template>` element of the given `<head>` or `<body>` element, |
| 110 | + * discarding any adjacent `<script>` or `<style>` elements |
| 111 | + * if the input is in Single-File Component (SFC) syntax. |
| 112 | + */ |
| 113 | + private function getTemplateElement( DOMElement $rootElement ): ?DOMElement { |
| 114 | + $onlyTemplateElement = null; |
| 115 | + foreach ( $rootElement->childNodes as $node ) { |
| 116 | + if ( $node->nodeType === XML_COMMENT_NODE ) { |
| 117 | + // comment node, ignore |
| 118 | + continue; |
| 119 | + } elseif ( $node->nodeType === XML_TEXT_NODE ) { |
| 120 | + if ( trim( $node->textContent ) === '' ) { |
| 121 | + // whitespace-only text node, ignore |
| 122 | + continue; |
| 123 | + } else { |
| 124 | + // not SFC |
| 125 | + $onlyTemplateElement = null; |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + if ( $node->tagName === 'template' ) { |
| 130 | + if ( $onlyTemplateElement === null ) { |
| 131 | + $onlyTemplateElement = $node; |
| 132 | + } else { |
| 133 | + // more than one <template>, handle as non-SFC and throw error below |
| 134 | + $onlyTemplateElement = null; |
| 135 | + break; |
| 136 | + } |
| 137 | + } elseif ( $node->tagName !== 'script' && $node->tagName !== 'style' ) { |
| 138 | + // top-level tag other than <template>, <script> or <style> => not SFC |
| 139 | + $onlyTemplateElement = null; |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + return $onlyTemplateElement; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get the only “substantial” child of the given element. |
| 148 | + * Ignore any adjacent comments or whitespace-only text nodes |
| 149 | + * (such as line breaks or indentation). |
| 150 | + */ |
| 151 | + private function getOnlySubstantialChild( DOMElement $element ): DOMElement { |
| 152 | + $onlySubstantialChild = null; |
| 153 | + foreach ( $element->childNodes as $node ) { |
| 154 | + if ( $node->nodeType === XML_COMMENT_NODE ) { |
| 155 | + // comment node, ignore |
| 156 | + continue; |
| 157 | + } elseif ( $node->nodeType === XML_TEXT_NODE && trim( $node->textContent ) === '' ) { |
| 158 | + // whitespace-only text node, ignore |
| 159 | + continue; |
| 160 | + } |
| 161 | + if ( $onlySubstantialChild === null ) { |
| 162 | + $onlySubstantialChild = $node; |
| 163 | + } else { |
| 164 | + throw new Exception( 'Template should only have one root node' ); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + if ( $onlySubstantialChild !== null ) { |
| 169 | + return $onlySubstantialChild; |
| 170 | + } else { |
| 171 | + throw new Exception( 'Template contained no root node' ); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments