• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..24-Nov-2021-

src/H01-Oct-2020-8,8426,385

CREDITSH A D01-Oct-2020687 1211

README.mdH A D01-Oct-20208.6 KiB255192

RELEASE.mdH A D01-Oct-20204.8 KiB149110

UPGRADING.mdH A D01-Oct-2020496 2212

composer.jsonH A D01-Oct-20201 KiB4342

README.md

1# HTML5-PHP
2
3HTML5 is a standards-compliant HTML5 parser and writer written entirely in PHP.
4It is stable and used in many production websites, and has
5well over [five million downloads](https://packagist.org/packages/masterminds/html5).
6
7HTML5 provides the following features.
8
9- An HTML5 serializer
10- Support for PHP namespaces
11- Composer support
12- Event-based (SAX-like) parser
13- A DOM tree builder
14- Interoperability with [QueryPath](https://github.com/technosophos/querypath)
15- Runs on **PHP** 5.3.0 or newer
16
17[![Build Status](https://travis-ci.org/Masterminds/html5-php.png?branch=master)](https://travis-ci.org/Masterminds/html5-php)
18[![Latest Stable Version](https://poser.pugx.org/masterminds/html5/v/stable.png)](https://packagist.org/packages/masterminds/html5)
19[![Code Coverage](https://scrutinizer-ci.com/g/Masterminds/html5-php/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Masterminds/html5-php/?branch=master)
20[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Masterminds/html5-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Masterminds/html5-php/?branch=master)
21[![Stability: Sustained](https://masterminds.github.io/stability/sustained.svg)](https://masterminds.github.io/stability/sustained.html)
22
23## Installation
24
25Install HTML5-PHP using [composer](http://getcomposer.org/).
26
27By adding the `masterminds/html5` dependency to your `composer.json` file:
28
29```json
30{
31  "require" : {
32    "masterminds/html5": "^2.0"
33  },
34}
35```
36
37By invoking require command via composer executable:
38
39```bash
40composer require masterminds/html5
41```
42
43## Basic Usage
44
45HTML5-PHP has a high-level API and a low-level API.
46
47Here is how you use the high-level `HTML5` library API:
48
49```php
50<?php
51// Assuming you installed from Composer:
52require "vendor/autoload.php";
53
54use Masterminds\HTML5;
55
56// An example HTML document:
57$html = <<< 'HERE'
58  <html>
59  <head>
60    <title>TEST</title>
61  </head>
62  <body id='foo'>
63    <h1>Hello World</h1>
64    <p>This is a test of the HTML5 parser.</p>
65  </body>
66  </html>
67HERE;
68
69// Parse the document. $dom is a DOMDocument.
70$html5 = new HTML5();
71$dom = $html5->loadHTML($html);
72
73// Render it as HTML5:
74print $html5->saveHTML($dom);
75
76// Or save it to a file:
77$html5->save($dom, 'out.html');
78```
79
80The `$dom` created by the parser is a full `DOMDocument` object. And the
81`save()` and `saveHTML()` methods will take any DOMDocument.
82
83### Options
84
85It is possible to pass in an array of configuration options when loading
86an HTML5 document.
87
88```php
89// An associative array of options
90$options = array(
91  'option_name' => 'option_value',
92);
93
94// Provide the options to the constructor
95$html5 = new HTML5($options);
96
97$dom = $html5->loadHTML($html);
98```
99
100The following options are supported:
101
102* `encode_entities` (boolean): Indicates that the serializer should aggressively
103  encode characters as entities. Without this, it only encodes the bare
104  minimum.
105* `disable_html_ns` (boolean): Prevents the parser from automatically
106  assigning the HTML5 namespace to the DOM document. This is for
107  non-namespace aware DOM tools.
108* `target_document` (\DOMDocument): A DOM document that will be used as the
109  destination for the parsed nodes.
110* `implicit_namespaces` (array): An assoc array of namespaces that should be
111  used by the parser. Name is tag prefix, value is NS URI.
112
113## The Low-Level API
114
115This library provides the following low-level APIs that you can use to
116create more customized HTML5 tools:
117
118- A SAX-like event-based parser that you can hook into for special kinds
119of parsing.
120- A flexible error-reporting mechanism that can be tuned to document
121syntax checking.
122- A DOM implementation that uses PHP's built-in DOM library.
123
124The unit tests exercise each piece of the API, and every public function
125is well-documented.
126
127### Parser Design
128
129The parser is designed as follows:
130
131- The `Scanner` handles scanning on behalf of the parser.
132- The `Tokenizer` requests data off of the scanner, parses it, clasifies
133it, and sends it to an `EventHandler`. It is a *recursive descent parser.*
134- The `EventHandler` receives notifications and data for each specific
135semantic event that occurs during tokenization.
136- The `DOMBuilder` is an `EventHandler` that listens for tokenizing
137events and builds a document tree (`DOMDocument`) based on the events.
138
139### Serializer Design
140
141The serializer takes a data structure (the `DOMDocument`) and transforms
142it into a character representation -- an HTML5 document.
143
144The serializer is broken into three parts:
145
146- The `OutputRules` contain the rules to turn DOM elements into strings. The
147rules are an implementation of the interface `RulesInterface` allowing for
148different rule sets to be used.
149- The `Traverser`, which is a special-purpose tree walker. It visits
150each node node in the tree and uses the `OutputRules` to transform the node
151into a string.
152- `HTML5` manages the `Traverser` and stores the resultant data
153in the correct place.
154
155The serializer (`save()`, `saveHTML()`) follows the
156[section 8.9 of the HTML 5.0 spec](http://www.w3.org/TR/2012/CR-html5-20121217/syntax.html#serializing-html-fragments).
157So tags are serialized according to these rules:
158
159- A tag with children: &lt;foo&gt;CHILDREN&lt;/foo&gt;
160- A tag that cannot have content: &lt;foo&gt; (no closing tag)
161- A tag that could have content, but doesn't: &lt;foo&gt;&lt;/foo&gt;
162
163## Known Issues (Or, Things We Designed Against the Spec)
164
165Please check the issue queue for a full list, but the following are
166issues known issues that are not presently on the roadmap:
167
168- Namespaces: HTML5 only [supports a selected list of namespaces](http://www.w3.org/TR/html5/infrastructure.html#namespaces)
169  and they do not operate in the same way as XML namespaces. A `:` has no special
170  meaning.
171  By default the parser does not support XML style namespaces via `:`;
172  to enable the XML namespaces see the  [XML Namespaces section](#xml-namespaces)
173- Scripts: This parser does not contain a JavaScript or a CSS
174  interpreter. While one may be supplied, not all features will be
175  supported.
176- Rentrance: The current parser is not re-entrant. (Thus you can't pause
177  the parser to modify the HTML string mid-parse.)
178- Validation: The current tree builder is **not** a validating parser.
179  While it will correct some HTML, it does not check that the HTML
180  conforms to the standard. (Should you wish, you can build a validating
181  parser by extending DOMTree or building your own EventHandler
182  implementation.)
183  * There is limited support for insertion modes.
184  * Some autocorrection is done automatically.
185  * Per the spec, many legacy tags are admitted and correctly handled,
186    even though they are technically not part of HTML5.
187- Attribute names and values: Due to the implementation details of the
188  PHP implementation of DOM, attribute names that do not follow the
189  XML 1.0 standard are not inserted into the DOM. (Effectively, they
190  are ignored.) If you've got a clever fix for this, jump in!
191- Processor Instructions: The HTML5 spec does not allow processor
192  instructions. We do. Since this is a server-side library, we think
193  this is useful. And that means, dear reader, that in some cases you
194  can parse the HTML from a mixed PHP/HTML document. This, however,
195  is an incidental feature, not a core feature.
196- HTML manifests: Unsupported.
197- PLAINTEXT: Unsupported.
198- Adoption Agency Algorithm: Not yet implemented. (8.2.5.4.7)
199
200## XML Namespaces
201
202To use XML style namespaces you have to configure well the main `HTML5` instance.
203
204```php
205use Masterminds\HTML5;
206$html = new HTML5(array(
207    "xmlNamespaces" => true
208));
209
210$dom = $html->loadHTML('<t:tag xmlns:t="http://www.example.com"/>');
211
212$dom->documentElement->namespaceURI; // http://www.example.com
213
214```
215
216You can also add some default prefixes that will not require the namespace declaration,
217but its elements will be namespaced.
218
219```php
220use Masterminds\HTML5;
221$html = new HTML5(array(
222    "implicitNamespaces"=>array(
223        "t"=>"http://www.example.com"
224    )
225));
226
227$dom = $html->loadHTML('<t:tag/>');
228
229$dom->documentElement->namespaceURI; // http://www.example.com
230
231```
232
233## Thanks to...
234
235The dedicated (and patient) contributors of patches small and large,
236who have already made this library better.See the CREDITS file for
237a list of contributors.
238
239We owe a huge debt of gratitude to the original authors of html5lib.
240
241While not much of the original parser remains, we learned a lot from
242reading the html5lib library. And some pieces remain here. In
243particular, much of the UTF-8 and Unicode handling is derived from the
244html5lib project.
245
246## License
247
248This software is released under the MIT license. The original html5lib
249library was also released under the MIT license.
250
251See LICENSE.txt
252
253Certain files contain copyright assertions by specific individuals
254involved with html5lib. Those have been retained where appropriate.
255