PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.17
Independent Analytics – WordPress Analytics Plugin v1.17
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / masterminds / html5 / src / HTML5.php
independent-analytics / vendor / masterminds / html5 / src Last commit date
HTML5 3 years ago HTML5.php 3 years ago
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