4 use Masterminds\HTML5\Parser\FileInputStream;
5 use Masterminds\HTML5\Parser\StringInputStream;
6 use Masterminds\HTML5\Parser\DOMTreeBuilder;
7 use Masterminds\HTML5\Parser\Scanner;
8 use Masterminds\HTML5\Parser\Tokenizer;
9 use Masterminds\HTML5\Serializer\OutputRules;
10 use Masterminds\HTML5\Serializer\Traverser;
13 * This class offers convenience methods for parsing and serializing HTML5.
14 * It is roughly designed to mirror the \DOMDocument class that is
15 * provided with most versions of PHP.
17 * EXPERIMENTAL. This may change or be completely replaced.
23 * Global options for the parser and serializer.
27 protected $options = array(
28 // If the serializer should encode all entities.
29 'encode_entities' => false
32 protected $errors = array();
34 public function __construct(array $options = array())
36 $this->options = array_merge($this->options, $options);
40 * Get the default options.
42 * @return array The default options.
44 public function getOptions()
46 return $this->options;
50 * Load and parse an HTML file.
52 * This will apply the HTML5 parser, which is tolerant of many
53 * varieties of HTML, including XHTML 1, HTML 4, and well-formed HTML
54 * 3. Note that in these cases, not all of the old data will be
55 * preserved. For example, XHTML's XML declaration will be removed.
57 * The rules governing parsing are set out in the HTML 5 spec.
60 * The path to the file to parse. If this is a resource, it is
61 * assumed to be an open stream whose pointer is set to the first
63 * @param array $options
64 * Configuration options when parsing the HTML
65 * @return \DOMDocument A DOM document. These object type is defined by the libxml
66 * library, and should have been included with your version of PHP.
68 public function load($file, array $options = array())
70 // Handle the case where file is a resource.
71 if (is_resource($file)) {
72 // FIXME: We need a StreamInputStream class.
73 return $this->loadHTML(stream_get_contents($file), $options);
76 $input = new FileInputStream($file);
78 return $this->parse($input, $options);
82 * Parse a HTML Document from a string.
84 * Take a string of HTML 5 (or earlier) and parse it into a
87 * @param string $string
88 * A html5 document as a string.
89 * @param array $options
90 * Configuration options when parsing the HTML
91 * @return \DOMDocument A DOM document. DOM is part of libxml, which is included with
92 * almost all distribtions of PHP.
94 public function loadHTML($string, array $options = array())
96 $input = new StringInputStream($string);
98 return $this->parse($input, $options);
102 * Convenience function to load an HTML file.
104 * This is here to provide backwards compatibility with the
105 * PHP DOM implementation. It simply calls load().
107 * @param string $file
108 * The path to the file to parse. If this is a resource, it is
109 * assumed to be an open stream whose pointer is set to the first
111 * @param array $options
112 * Configuration options when parsing the HTML
114 * @return \DOMDocument A DOM document. These object type is defined by the libxml
115 * library, and should have been included with your version of PHP.
117 public function loadHTMLFile($file, array $options = array())
119 return $this->load($file, $options);
123 * Parse a HTML fragment from a string.
125 * @param string $string
126 * The html5 fragment as a string.
127 * @param array $options
128 * Configuration options when parsing the HTML
130 * @return \DOMDocumentFragment A DOM fragment. The DOM is part of libxml, which is included with
131 * almost all distributions of PHP.
133 public function loadHTMLFragment($string, array $options = array())
135 $input = new StringInputStream($string);
137 return $this->parseFragment($input, $options);
141 * Return all errors encountered into parsing phase
145 public function getErrors()
147 return $this->errors;
151 * Return true it some errors were encountered into parsing phase
155 public function hasErrors()
157 return count($this->errors) > 0;
161 * Parse an input stream.
163 * Lower-level loading function. This requires an input stream instead
164 * of a string, file, or resource.
166 public function parse(\Masterminds\HTML5\Parser\InputStream $input, array $options = array())
168 $this->errors = array();
169 $options = array_merge($this->getOptions(), $options);
170 $events = new DOMTreeBuilder(false, $options);
171 $scanner = new Scanner($input);
172 $parser = new Tokenizer($scanner, $events, !empty($options['xmlNamespaces']) ? Tokenizer::CONFORMANT_XML: Tokenizer::CONFORMANT_HTML);
175 $this->errors = $events->getErrors();
177 return $events->document();
181 * Parse an input stream where the stream is a fragment.
183 * Lower-level loading function. This requires an input stream instead
184 * of a string, file, or resource.
186 public function parseFragment(\Masterminds\HTML5\Parser\InputStream $input, array $options = array())
188 $options = array_merge($this->getOptions(), $options);
189 $events = new DOMTreeBuilder(true, $options);
190 $scanner = new Scanner($input);
191 $parser = new Tokenizer($scanner, $events, !empty($options['xmlNamespaces']) ? Tokenizer::CONFORMANT_XML: Tokenizer::CONFORMANT_HTML);
194 $this->errors = $events->getErrors();
196 return $events->fragment();
200 * Save a DOM into a given file as HTML5.
203 * The DOM to be serialized.
204 * @param string $file
205 * The filename to be written.
206 * @param array $options
207 * 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.
212 public function save($dom, $file, $options = array())
215 if (is_resource($file)) {
219 $stream = fopen($file, 'w');
221 $options = array_merge($this->getOptions(), $options);
222 $rules = new OutputRules($stream, $options);
223 $trav = new Traverser($dom, $stream, $rules, $options);
233 * Convert a DOM into an HTML5 string.
236 * The DOM to be serialized.
237 * @param array $options
238 * Configuration options when serializing the DOM. These include:
239 * - encode_entities: Text written to the output is escaped by default and not all
240 * entities are encoded. If this is set to true all entities will be encoded.
243 * @return string A HTML5 documented generated from the DOM.
245 public function saveHTML($dom, $options = array())
247 $stream = fopen('php://temp', 'w');
248 $this->save($dom, $stream, array_merge($this->getOptions(), $options));
250 return stream_get_contents($stream, - 1, 0);