HTML5.php
223 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Masterminds; |
| 4 | |
| 5 | use IAWP\Masterminds\HTML5\Parser\DOMTreeBuilder; |
| 6 | use IAWP\Masterminds\HTML5\Parser\Scanner; |
| 7 | use IAWP\Masterminds\HTML5\Parser\Tokenizer; |
| 8 | use IAWP\Masterminds\HTML5\Serializer\OutputRules; |
| 9 | use IAWP\Masterminds\HTML5\Serializer\Traverser; |
| 10 | /** |
| 11 | * This class offers convenience methods for parsing and serializing HTML5. |
| 12 | * It is roughly designed to mirror the \DOMDocument native class. |
| 13 | */ |
| 14 | class HTML5 |
| 15 | { |
| 16 | /** |
| 17 | * Global options for the parser and serializer. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | private $defaultOptions = array( |
| 22 | // Whether the serializer should aggressively encode all characters as entities. |
| 23 | 'encode_entities' => \false, |
| 24 | // Prevents the parser from automatically assigning the HTML5 namespace to the DOM document. |
| 25 | 'disable_html_ns' => \false, |
| 26 | ); |
| 27 | protected $errors = array(); |
| 28 | public function __construct(array $defaultOptions = array()) |
| 29 | { |
| 30 | $this->defaultOptions = \array_merge($this->defaultOptions, $defaultOptions); |
| 31 | } |
| 32 | /** |
| 33 | * Get the current default options. |
| 34 | * |
| 35 | * @return array |
| 36 | */ |
| 37 | public function getOptions() |
| 38 | { |
| 39 | return $this->defaultOptions; |
| 40 | } |
| 41 | /** |
| 42 | * Load and parse an HTML file. |
| 43 | * |
| 44 | * This will apply the HTML5 parser, which is tolerant of many |
| 45 | * varieties of HTML, including XHTML 1, HTML 4, and well-formed HTML |
| 46 | * 3. Note that in these cases, not all of the old data will be |
| 47 | * preserved. For example, XHTML's XML declaration will be removed. |
| 48 | * |
| 49 | * The rules governing parsing are set out in the HTML 5 spec. |
| 50 | * |
| 51 | * @param string|resource $file The path to the file to parse. If this is a resource, it is |
| 52 | * assumed to be an open stream whose pointer is set to the first |
| 53 | * byte of input. |
| 54 | * @param array $options Configuration options when parsing the HTML. |
| 55 | * |
| 56 | * @return \DOMDocument A DOM document. These object type is defined by the libxml |
| 57 | * library, and should have been included with your version of PHP. |
| 58 | */ |
| 59 | public function load($file, array $options = array()) |
| 60 | { |
| 61 | // Handle the case where file is a resource. |
| 62 | if (\is_resource($file)) { |
| 63 | return $this->parse(\stream_get_contents($file), $options); |
| 64 | } |
| 65 | return $this->parse(\file_get_contents($file), $options); |
| 66 | } |
| 67 | /** |
| 68 | * Parse a HTML Document from a string. |
| 69 | * |
| 70 | * Take a string of HTML 5 (or earlier) and parse it into a |
| 71 | * DOMDocument. |
| 72 | * |
| 73 | * @param string $string A html5 document as a string. |
| 74 | * @param array $options Configuration options when parsing the HTML. |
| 75 | * |
| 76 | * @return \DOMDocument A DOM document. DOM is part of libxml, which is included with |
| 77 | * almost all distribtions of PHP. |
| 78 | */ |
| 79 | public function loadHTML($string, array $options = array()) |
| 80 | { |
| 81 | return $this->parse($string, $options); |
| 82 | } |
| 83 | /** |
| 84 | * Convenience function to load an HTML file. |
| 85 | * |
| 86 | * This is here to provide backwards compatibility with the |
| 87 | * PHP DOM implementation. It simply calls load(). |
| 88 | * |
| 89 | * @param string $file The path to the file to parse. If this is a resource, it is |
| 90 | * assumed to be an open stream whose pointer is set to the first |
| 91 | * byte of input. |
| 92 | * @param array $options Configuration options when parsing the HTML. |
| 93 | * |
| 94 | * @return \DOMDocument A DOM document. These object type is defined by the libxml |
| 95 | * library, and should have been included with your version of PHP. |
| 96 | */ |
| 97 | public function loadHTMLFile($file, array $options = array()) |
| 98 | { |
| 99 | return $this->load($file, $options); |
| 100 | } |
| 101 | /** |
| 102 | * Parse a HTML fragment from a string. |
| 103 | * |
| 104 | * @param string $string the HTML5 fragment as a string |
| 105 | * @param array $options Configuration options when parsing the HTML |
| 106 | * |
| 107 | * @return \DOMDocumentFragment A DOM fragment. The DOM is part of libxml, which is included with |
| 108 | * almost all distributions of PHP. |
| 109 | */ |
| 110 | public function loadHTMLFragment($string, array $options = array()) |
| 111 | { |
| 112 | return $this->parseFragment($string, $options); |
| 113 | } |
| 114 | /** |
| 115 | * Return all errors encountered into parsing phase. |
| 116 | * |
| 117 | * @return array |
| 118 | */ |
| 119 | public function getErrors() |
| 120 | { |
| 121 | return $this->errors; |
| 122 | } |
| 123 | /** |
| 124 | * Return true it some errors were encountered into parsing phase. |
| 125 | * |
| 126 | * @return bool |
| 127 | */ |
| 128 | public function hasErrors() |
| 129 | { |
| 130 | return \count($this->errors) > 0; |
| 131 | } |
| 132 | /** |
| 133 | * Parse an input string. |
| 134 | * |
| 135 | * @param string $input |
| 136 | * @param array $options |
| 137 | * |
| 138 | * @return \DOMDocument |
| 139 | */ |
| 140 | public function parse($input, array $options = array()) |
| 141 | { |
| 142 | $this->errors = array(); |
| 143 | $options = \array_merge($this->defaultOptions, $options); |
| 144 | $events = new DOMTreeBuilder(\false, $options); |
| 145 | $scanner = new Scanner($input, !empty($options['encoding']) ? $options['encoding'] : 'UTF-8'); |
| 146 | $parser = new Tokenizer($scanner, $events, !empty($options['xmlNamespaces']) ? Tokenizer::CONFORMANT_XML : Tokenizer::CONFORMANT_HTML); |
| 147 | $parser->parse(); |
| 148 | $this->errors = $events->getErrors(); |
| 149 | return $events->document(); |
| 150 | } |
| 151 | /** |
| 152 | * Parse an input stream where the stream is a fragment. |
| 153 | * |
| 154 | * Lower-level loading function. This requires an input stream instead |
| 155 | * of a string, file, or resource. |
| 156 | * |
| 157 | * @param string $input The input data to parse in the form of a string. |
| 158 | * @param array $options An array of options. |
| 159 | * |
| 160 | * @return \DOMDocumentFragment |
| 161 | */ |
| 162 | public function parseFragment($input, array $options = array()) |
| 163 | { |
| 164 | $options = \array_merge($this->defaultOptions, $options); |
| 165 | $events = new DOMTreeBuilder(\true, $options); |
| 166 | $scanner = new Scanner($input, !empty($options['encoding']) ? $options['encoding'] : 'UTF-8'); |
| 167 | $parser = new Tokenizer($scanner, $events, !empty($options['xmlNamespaces']) ? Tokenizer::CONFORMANT_XML : Tokenizer::CONFORMANT_HTML); |
| 168 | $parser->parse(); |
| 169 | $this->errors = $events->getErrors(); |
| 170 | return $events->fragment(); |
| 171 | } |
| 172 | /** |
| 173 | * Save a DOM into a given file as HTML5. |
| 174 | * |
| 175 | * @param mixed $dom The DOM to be serialized. |
| 176 | * @param string|resource $file The filename to be written or resource to write to. |
| 177 | * @param array $options Configuration options when serializing the DOM. These include: |
| 178 | * - encode_entities: Text written to the output is escaped by default and not all |
| 179 | * entities are encoded. If this is set to true all entities will be encoded. |
| 180 | * Defaults to false. |
| 181 | */ |
| 182 | public function save($dom, $file, $options = array()) |
| 183 | { |
| 184 | $close = \true; |
| 185 | if (\is_resource($file)) { |
| 186 | $stream = $file; |
| 187 | $close = \false; |
| 188 | } else { |
| 189 | $stream = \fopen($file, 'wb'); |
| 190 | } |
| 191 | $options = \array_merge($this->defaultOptions, $options); |
| 192 | $rules = new OutputRules($stream, $options); |
| 193 | $trav = new Traverser($dom, $stream, $rules, $options); |
| 194 | $trav->walk(); |
| 195 | /* |
| 196 | * release the traverser to avoid cyclic references and allow PHP to free memory without waiting for gc_collect_cycles |
| 197 | */ |
| 198 | $rules->unsetTraverser(); |
| 199 | if ($close) { |
| 200 | \fclose($stream); |
| 201 | } |
| 202 | } |
| 203 | /** |
| 204 | * Convert a DOM into an HTML5 string. |
| 205 | * |
| 206 | * @param mixed $dom The DOM to be serialized. |
| 207 | * @param array $options Configuration options when serializing the DOM. These include: |
| 208 | * - encode_entities: Text written to the output is escaped by default and not all |
| 209 | * entities are encoded. If this is set to true all entities will be encoded. |
| 210 | * Defaults to false. |
| 211 | * |
| 212 | * @return string A HTML5 documented generated from the DOM. |
| 213 | */ |
| 214 | public function saveHTML($dom, $options = array()) |
| 215 | { |
| 216 | $stream = \fopen('php://temp', 'wb'); |
| 217 | $this->save($dom, $stream, \array_merge($this->defaultOptions, $options)); |
| 218 | $html = \stream_get_contents($stream, -1, 0); |
| 219 | \fclose($stream); |
| 220 | return $html; |
| 221 | } |
| 222 | } |
| 223 |