PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.2.88
WindPress – Tailwind CSS integration for WordPress v3.2.88
3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 143 releases
windpress / vendor / masterminds / html5 / src / HTML5.php

HTML5.php in WindPress – Tailwind CSS integration for WordPress 3.2.88, at vendor/masterminds/html5/src/HTML5.php

222 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WindPressDeps\Masterminds;
4
5 use WindPressDeps\Masterminds\HTML5\Parser\DOMTreeBuilder;
6 use WindPressDeps\Masterminds\HTML5\Parser\Scanner;
7 use WindPressDeps\Masterminds\HTML5\Parser\Tokenizer;
8 use WindPressDeps\Masterminds\HTML5\Serializer\OutputRules;
9 use WindPressDeps\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 *
137 * @return \DOMDocument
138 */
139 public function parse($input, array $options = array())
140 {
141 $this->errors = array();
142 $options = array_merge($this->defaultOptions, $options);
143 $events = new DOMTreeBuilder(\false, $options);
144 $scanner = new Scanner($input, !empty($options['encoding']) ? $options['encoding'] : 'UTF-8');
145 $parser = new Tokenizer($scanner, $events, !empty($options['xmlNamespaces']) ? Tokenizer::CONFORMANT_XML : Tokenizer::CONFORMANT_HTML);
146 $parser->parse();
147 $this->errors = $events->getErrors();
148 return $events->document();
149 }
150 /**
151 * Parse an input stream where the stream is a fragment.
152 *
153 * Lower-level loading function. This requires an input stream instead
154 * of a string, file, or resource.
155 *
156 * @param string $input The input data to parse in the form of a string.
157 * @param array $options An array of options.
158 *
159 * @return \DOMDocumentFragment
160 */
161 public function parseFragment($input, array $options = array())
162 {
163 $options = array_merge($this->defaultOptions, $options);
164 $events = new DOMTreeBuilder(\true, $options);
165 $scanner = new Scanner($input, !empty($options['encoding']) ? $options['encoding'] : 'UTF-8');
166 $parser = new Tokenizer($scanner, $events, !empty($options['xmlNamespaces']) ? Tokenizer::CONFORMANT_XML : Tokenizer::CONFORMANT_HTML);
167 $parser->parse();
168 $this->errors = $events->getErrors();
169 return $events->fragment();
170 }
171 /**
172 * Save a DOM into a given file as HTML5.
173 *
174 * @param mixed $dom The DOM to be serialized.
175 * @param string|resource $file The filename to be written or resource to write to.
176 * @param array $options Configuration options when serializing the DOM. These include:
177 * - encode_entities: Text written to the output is escaped by default and not all
178 * entities are encoded. If this is set to true all entities will be encoded.
179 * Defaults to false.
180 */
181 public function save($dom, $file, $options = array())
182 {
183 $close = \true;
184 if (is_resource($file)) {
185 $stream = $file;
186 $close = \false;
187 } else {
188 $stream = fopen($file, 'wb');
189 }
190 $options = array_merge($this->defaultOptions, $options);
191 $rules = new OutputRules($stream, $options);
192 $trav = new Traverser($dom, $stream, $rules, $options);
193 $trav->walk();
194 /*
195 * release the traverser to avoid cyclic references and allow PHP to free memory without waiting for gc_collect_cycles
196 */
197 $rules->unsetTraverser();
198 if ($close) {
199 fclose($stream);
200 }
201 }
202 /**
203 * Convert a DOM into an HTML5 string.
204 *
205 * @param mixed $dom The DOM to be serialized.
206 * @param array $options Configuration options when serializing the DOM. These include:
207 * - encode_entities: Text written to the output is escaped by default and not all
208 * entities are encoded. If this is set to true all entities will be encoded.
209 * Defaults to false.
210 *
211 * @return string A HTML5 documented generated from the DOM.
212 */
213 public function saveHTML($dom, $options = array())
214 {
215 $stream = fopen('php://temp', 'wb');
216 $this->save($dom, $stream, array_merge($this->defaultOptions, $options));
217 $html = stream_get_contents($stream, -1, 0);
218 fclose($stream);
219 return $html;
220 }
221 }
222