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