An HTML5 parser and serializer for PHP.

Overview

HTML5-PHP

HTML5 is a standards-compliant HTML5 parser and writer written entirely in PHP. It is stable and used in many production websites, and has well over five million downloads.

HTML5 provides the following features.

  • An HTML5 serializer
  • Support for PHP namespaces
  • Composer support
  • Event-based (SAX-like) parser
  • A DOM tree builder
  • Interoperability with QueryPath
  • Runs on PHP 5.3.0 or newer

Build Status Latest Stable Version Code Coverage Scrutinizer Code Quality Stability: Sustained

Installation

Install HTML5-PHP using composer.

By adding the masterminds/html5 dependency to your composer.json file:

{
  "require" : {
    "masterminds/html5": "^2.0"
  },
}

By invoking require command via composer executable:

composer require masterminds/html5

Basic Usage

HTML5-PHP has a high-level API and a low-level API.

Here is how you use the high-level HTML5 library API:

<?php
// Assuming you installed from Composer:
require "vendor/autoload.php";

use Masterminds\HTML5;

// An example HTML document:
$html = <<< 'HERE'
  <html>
  <head>
    <title>TEST</title>
  </head>
  <body id='foo'>
    <h1>Hello World</h1>
    <p>This is a test of the HTML5 parser.</p>
  </body>
  </html>
HERE;

// Parse the document. $dom is a DOMDocument.
$html5 = new HTML5();
$dom = $html5->loadHTML($html);

// Render it as HTML5:
print $html5->saveHTML($dom);

// Or save it to a file:
$html5->save($dom, 'out.html');

The $dom created by the parser is a full DOMDocument object. And the save() and saveHTML() methods will take any DOMDocument.

Options

It is possible to pass in an array of configuration options when loading an HTML5 document.

// An associative array of options
$options = array(
  'option_name' => 'option_value',
);

// Provide the options to the constructor
$html5 = new HTML5($options);

$dom = $html5->loadHTML($html);

The following options are supported:

  • encode_entities (boolean): Indicates that the serializer should aggressively encode characters as entities. Without this, it only encodes the bare minimum.
  • disable_html_ns (boolean): Prevents the parser from automatically assigning the HTML5 namespace to the DOM document. This is for non-namespace aware DOM tools.
  • target_document (\DOMDocument): A DOM document that will be used as the destination for the parsed nodes.
  • implicit_namespaces (array): An assoc array of namespaces that should be used by the parser. Name is tag prefix, value is NS URI.

The Low-Level API

This library provides the following low-level APIs that you can use to create more customized HTML5 tools:

  • A SAX-like event-based parser that you can hook into for special kinds of parsing.
  • A flexible error-reporting mechanism that can be tuned to document syntax checking.
  • A DOM implementation that uses PHP's built-in DOM library.

The unit tests exercise each piece of the API, and every public function is well-documented.

Parser Design

The parser is designed as follows:

  • The Scanner handles scanning on behalf of the parser.
  • The Tokenizer requests data off of the scanner, parses it, clasifies it, and sends it to an EventHandler. It is a recursive descent parser.
  • The EventHandler receives notifications and data for each specific semantic event that occurs during tokenization.
  • The DOMBuilder is an EventHandler that listens for tokenizing events and builds a document tree (DOMDocument) based on the events.

Serializer Design

The serializer takes a data structure (the DOMDocument) and transforms it into a character representation -- an HTML5 document.

The serializer is broken into three parts:

  • The OutputRules contain the rules to turn DOM elements into strings. The rules are an implementation of the interface RulesInterface allowing for different rule sets to be used.
  • The Traverser, which is a special-purpose tree walker. It visits each node node in the tree and uses the OutputRules to transform the node into a string.
  • HTML5 manages the Traverser and stores the resultant data in the correct place.

The serializer (save(), saveHTML()) follows the section 8.9 of the HTML 5.0 spec. So tags are serialized according to these rules:

  • A tag with children: <foo>CHILDREN</foo>
  • A tag that cannot have content: <foo> (no closing tag)
  • A tag that could have content, but doesn't: <foo></foo>

Known Issues (Or, Things We Designed Against the Spec)

Please check the issue queue for a full list, but the following are issues known issues that are not presently on the roadmap:

  • Namespaces: HTML5 only supports a selected list of namespaces and they do not operate in the same way as XML namespaces. A : has no special meaning. By default the parser does not support XML style namespaces via :; to enable the XML namespaces see the XML Namespaces section
  • Scripts: This parser does not contain a JavaScript or a CSS interpreter. While one may be supplied, not all features will be supported.
  • Rentrance: The current parser is not re-entrant. (Thus you can't pause the parser to modify the HTML string mid-parse.)
  • Validation: The current tree builder is not a validating parser. While it will correct some HTML, it does not check that the HTML conforms to the standard. (Should you wish, you can build a validating parser by extending DOMTree or building your own EventHandler implementation.)
    • There is limited support for insertion modes.
    • Some autocorrection is done automatically.
    • Per the spec, many legacy tags are admitted and correctly handled, even though they are technically not part of HTML5.
  • Attribute names and values: Due to the implementation details of the PHP implementation of DOM, attribute names that do not follow the XML 1.0 standard are not inserted into the DOM. (Effectively, they are ignored.) If you've got a clever fix for this, jump in!
  • Processor Instructions: The HTML5 spec does not allow processor instructions. We do. Since this is a server-side library, we think this is useful. And that means, dear reader, that in some cases you can parse the HTML from a mixed PHP/HTML document. This, however, is an incidental feature, not a core feature.
  • HTML manifests: Unsupported.
  • PLAINTEXT: Unsupported.
  • Adoption Agency Algorithm: Not yet implemented. (8.2.5.4.7)

XML Namespaces

To use XML style namespaces you have to configure well the main HTML5 instance.

use Masterminds\HTML5;
$html = new HTML5(array(
    "xmlNamespaces" => true
));

$dom = $html->loadHTML('<t:tag xmlns:t="http://www.example.com"/>');

$dom->documentElement->namespaceURI; // http://www.example.com

You can also add some default prefixes that will not require the namespace declaration, but its elements will be namespaced.

use Masterminds\HTML5;
$html = new HTML5(array(
    "implicitNamespaces"=>array(
        "t"=>"http://www.example.com"
    )
));

$dom = $html->loadHTML('<t:tag/>');

$dom->documentElement->namespaceURI; // http://www.example.com

Thanks to...

The dedicated (and patient) contributors of patches small and large, who have already made this library better.See the CREDITS file for a list of contributors.

We owe a huge debt of gratitude to the original authors of html5lib.

While not much of the original parser remains, we learned a lot from reading the html5lib library. And some pieces remain here. In particular, much of the UTF-8 and Unicode handling is derived from the html5lib project.

License

This software is released under the MIT license. The original html5lib library was also released under the MIT license.

See LICENSE.txt

Certain files contain copyright assertions by specific individuals involved with html5lib. Those have been retained where appropriate.

You might also like...
A New Markdown parser for PHP5.4

Ciconia - A New Markdown Parser for PHP The Markdown parser for PHP5.4, it is fully extensible. Ciconia is the collection of extension, so you can rep

A lightweight lexical string parser for BBCode styled markup.

Decoda A lightweight lexical string parser for BBCode styled markup. Requirements PHP 5.6.0+ Multibyte Composer Contributors "Marten-Plain" emoticons

Simple URL parser

urlparser Simple URL parser This is a simple URL parser, which returns an array of results from url of kind /module/controller/param1:value/param2:val

This is a simple, streaming parser for processing large JSON documents

Streaming JSON parser for PHP This is a simple, streaming parser for processing large JSON documents. Use it for parsing very large JSON documents to

UpToDocs scans a Markdown file for PHP code blocks, and executes each one in a separate process.

UpToDocs UpToDocs scans a Markdown file for PHP code blocks, and executes each one in a separate process. Include this in your CI workflows, to make s

HTML sanitizer, written in PHP, aiming to provide XSS-safe markup based on explicitly allowed tags, attributes and values.

TYPO3 HTML Sanitizer ℹ️ Common safe HTML tags & attributes as given in \TYPO3\HtmlSanitizer\Builder\CommonBuilder still might be adjusted, extended or

A simple PHP scripting application which fetch emails from your Gmail account according to a filter and parses them for information.

A simple PHP scripting application which fetch emails from your Gmail account according to a filter and parses them for information.

Plug and play flat file markdown blog for your Laravel-projects
Plug and play flat file markdown blog for your Laravel-projects

Ampersand Plug-and-play flat file markdown blog tool for your Laravel-project. Create an article or blog-section on your site without the hassle of se

Convert HTML to Markdown with PHP

HTML To Markdown for PHP Library which converts HTML to Markdown for your sanity and convenience. Requires: PHP 7.2+ Lead Developer: @colinodell Origi

Comments
  • Typed property DOMDocument::$documentElement must not be accessed before initialization

    Typed property DOMDocument::$documentElement must not be accessed before initialization

    I'm unable to reproduce, but one of Roundcube users created a ticket for this here.

    This is with PHP 8.1.13:

    PHP Fatal error:  Uncaught Error: Typed property DOMDocument::$documentElement must not be accessed before initialization in /var/www/roundcube/vendor/masterminds/html5/src/HTML5/Parser/DOMTreeBuilder.php:275
    Stack trace:
    #0 /var/www/roundcube/vendor/masterminds/html5/src/HTML5/Parser/Tokenizer.php(399): Masterminds\HTML5\Parser\DOMTreeBuilder->startTag()
    #1 /var/www/roundcube/vendor/masterminds/html5/src/HTML5/Parser/Tokenizer.php(141): Masterminds\HTML5\Parser\Tokenizer->tagName()
    #2 /var/www/roundcube/vendor/masterminds/html5/src/HTML5/Parser/Tokenizer.php(82): Masterminds\HTML5\Parser\Tokenizer->consumeData()
    #3 /var/www/roundcube/vendor/masterminds/html5/src/HTML5.php(161): Masterminds\HTML5\Parser\Tokenizer->parse()
    #4 /var/www/roundcube/vendor/masterminds/html5/src/HTML5.php(89): Masterminds\HTML5->parse()
    #5 /var/www/roundcube/program/lib/Roundcube/rcube_washtml.php(698): Masterminds\HTML5->loadHTML()
    
    opened by alecpl 0
  • Question

    Question

    Hi I come here to ask a, maybe, dumb question. I used a WP plugin that uses this library to make changes to the html. This html contain inlined svg like:

    < svg xmlns= "https://www.w3.org/2000/svg" some stuff here ></svg>

    After get parsed this becomes:

    < svg some stuff here /></svg>

    Is there any way to preserve the xmlns attribute and don´t set the autoclosig / ?

    Thx in advance.

    opened by Lofesa 0
  • Parser returns SVG with image-tag as text-node

    Parser returns SVG with image-tag as text-node

    The current version 2.7.5 still returns svg as text node:

    <ul id="nav1" class="nav1main">
        <li>
            <a href="/" class="logo-image">
                &lt;svg id="logo-mobile"&gt;
                    &lt;image href="logo-mobile.svg" width="100%" height="100%" /&gt;
                &lt;/svg&gt;
            </a>
        </li>
    </ul>
    

    Source:

    <ul id="nav1" class="nav1main">
        <li>
            <a href="/" class="logo-image">
                <svg id="logo-mobile">
                    <image href="logo-mobile.svg" width="100%" height="100%"></image>
                </svg>
            </a>
        </li>
    </ul>
    

    Equal, if image with separate closing tag or self-closing.

    Originally posted by @mat-hew1 in https://github.com/Masterminds/html5-php/issues/129#issuecomment-904601890

    opened by mat-hew1 0
  • is adding html tags and converting > with >

    is adding html tags and converting > with >

    Hi there, thanks for your work with this package, im having some problems with it, but im parsing php files worse than that blade files and i need help starting with 2 situations

    1 - Is any way to stop adding html tags to php files 2 - It is enconding > with &gt with encode option disable

    regards

    opened by mbarreiro85 0
Releases(2.7.6)
  • 2.7.6(Aug 18, 2022)

  • 2.7.5(Jul 1, 2021)

  • 2.7.4(Oct 1, 2020)

  • 2.7.3(Jul 5, 2020)

  • 2.7.2(Jul 1, 2020)

  • 2.7.1(Jun 14, 2020)

  • 2.7.0(Jul 25, 2019)

  • 2.6.0(Mar 10, 2019)

  • 2.5.0(Dec 27, 2018)

    This release brings again big performance improvements (thanks @stof for the excellent work).

    v2.5 is 3.5 times faster than v2.4 and 11 times faster than v2.3!

    Here a list of the changes:

    • #162, #161, #155, #154, #153, #151: performance improvements
    • #156: fixed typos
    • #160: adopt and enforce code style
    • #159: remove deprecated php unit base test case
    • #150: backport changes from old master branch
    Source code(tar.gz)
    Source code(zip)
  • 2.4.0(Nov 17, 2018)

    This release brings big performance improvements (thanks @tgalopin for the excellent work).

    Based on the test/benchmark/run.php benchmark, 2.4.0 is 2x faster than 2.3.1 when parsing HTML5 documents.

    Here a list of the changes:

    • #148: Improve performance by moving sequence matching
    • #147: Improve the Tokenizer performance
    • #146: Improve performance by relying on a native string instead of InputStream
    • #144: Add DOM extension in composer.json
    • #145: Add more extensions on composer.json, improve phpdocs and remove dead code
    • #143: Remove experimental comment
    Source code(tar.gz)
    Source code(zip)
  • 2.3.1(Oct 22, 2018)

    • #121: Audio is not a block tag (fixed by #141)
    • #136: Handle illegal self-closing according to spec (fixed by #137)
    • #141: Minor fixes in the README
    Source code(tar.gz)
    Source code(zip)
  • 2.3.0(Sep 4, 2017)

    • #129: image within inline svg breaks system (fixed by #133)
    • #131: ² does not work (fixed by #132)
    • #134: Improve tokenizer performance by 20% (alternative version of #130 thanks to @MichaelHeerklotz)
    • #135: Raw & in attributes
    Source code(tar.gz)
    Source code(zip)
  • 2.2.2(Sep 22, 2016)

    • #116: In XML mode, tags are case sensitive
    • #115: Fix PHP Notice in OutputRules
    • #112: fix parsing of options of an optgroup
    • #111: Adding test for the address tag
    Source code(tar.gz)
    Source code(zip)
  • 2.2.1(May 10, 2016)

  • 2.2.0(Apr 11, 2016)

    • #105: Enable composer cache (for CI/CD)
    • #100: Use mb_substitute_character inset of ini_set for environments where ini_set is disable (e.g., shared hosting)
    • #98: Allow link, meta, style tags in noscript tags
    • #96: Fixed xml:href on svgs that use the "use" breaking
    • #94: Counting UTF8 characters performance improvement
    • #93: Use newer version of coveralls package
    • #90: Remove duplicate test
    • #87: Allow multiple root nodes
    Source code(tar.gz)
    Source code(zip)
  • 2.1.2(Jun 7, 2015)

  • 2.1.1(Mar 23, 2015)

  • 2.1.0(Feb 9, 2015)

    • #74: Added disable_html_ns and target_doc dom parsing options
    • Unified option names
    • #73: Fixed alphabet, ß now can be detected
    • #75 and #76: Allow whitespace in RCDATA tags
    • #77: Fixed parsing blunder for json embeds
    • #72: Add options to HTML methods
    Source code(tar.gz)
    Source code(zip)
  • 1.0.7(Feb 6, 2015)

  • 2.0.2(Dec 17, 2014)

    • closed #50 (empty document handling)
    • closed #63 (tags with strange capitalization)
    • closed #65 (dashes and underscores as allowed characters in tag names)
    • closed #68 (non inline elements inside inline containers)
    Source code(tar.gz)
    Source code(zip)
  • 2.0.1(Sep 23, 2014)

  • 2.0.0(Jul 28, 2014)

    • #53: Improved boolean attributes handling
    • #52: Facebook HHVM compatibility
    • #48: Adopted PSR-2 as coding standard
    • #47: Moved everything to Masterminds namespace
    • #45: Added custom namespaces
    • #44: Added support to XML-style namespaces
    • #37: Refactored HTML5 class removing static methods
    Source code(tar.gz)
    Source code(zip)
  • 1.0.5(Jun 11, 2014)

    • #38: Set the dev-master branch as the 1.0.x branch for composer (goetas)
    • #34: Tests use PSR-4 for autoloading. (goetas)
    • #40, #41: Fix entity handling in RCDATA sections. (KitaitiMakoto)
    • #32: Fixed issue where wharacter references were being incorrectly encoded in style tags.
    Source code(tar.gz)
    Source code(zip)
  • 1.0.4(Apr 30, 2014)

  • 1.0.3(Feb 28, 2014)

  • 1.0.2(Feb 12, 2014)

    • #23: Handle missing tag close in attribute list.
    • #25: Fixed text escaping in the serializer (HTML% 8.3).
    • #27: Fixed tests on Windows: changed "\n" -> PHP_EOL.
    • #28: Fixed infinite loop for char "&" in unquoted attribute in parser.
    • #26: Updated tag name case handling to deal with uppercase usage.
    • #24: Newlines and tabs are allowed inside quoted attributes (HTML5 8.2.4).
    • Fixed Travis CI testing.
    Source code(tar.gz)
    Source code(zip)
  • 1.0.1(Nov 7, 2013)

    This release includes:

    • CDATA encoding is improved. (Non-standard; Issue #19)
    • Some parser rules were not returning the new current element. (Issue #20)
    • Added, to the README, details on code test coverage and to packagist version.
    • Fixed processor instructions.
    • Improved test coverage and documentation coverage.
    Source code(tar.gz)
    Source code(zip)
Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.

league/commonmark league/commonmark is a highly-extensible PHP Markdown parser created by Colin O'Dell which supports the full CommonMark spec and Git

The League of Extraordinary Packages 2.4k Jan 1, 2023
Advanced shortcode (BBCode) parser and engine for PHP

Shortcode Shortcode is a framework agnostic PHP library allowing to find, extract and process text fragments called "shortcodes" or "BBCodes". Example

Tomasz Kowalczyk 358 Nov 26, 2022
Efficient, easy-to-use, and fast PHP JSON stream parser

JSON Machine Very easy to use and memory efficient drop-in replacement for inefficient iteration of big JSON files or streams for PHP 5.6+. See TL;DR.

Filip Halaxa 801 Dec 28, 2022
Parser for Markdown and Markdown Extra derived from the original Markdown.pl by John Gruber.

PHP Markdown PHP Markdown Lib 1.9.0 - 1 Dec 2019 by Michel Fortin https://michelf.ca/ based on Markdown by John Gruber https://daringfireball.net/ Int

Michel Fortin 3.3k Jan 1, 2023
Better Markdown Parser in PHP

Parsedown Better Markdown Parser in PHP - Demo. Features One File No Dependencies Super Fast Extensible GitHub flavored Tested in 5.3 to 7.3 Markdown

Emanuil Rusev 14.3k Jan 8, 2023
A super fast, highly extensible markdown parser for PHP

A super fast, highly extensible markdown parser for PHP What is this? A set of PHP classes, each representing a Markdown flavor, and a command line to

Carsten Brandt 989 Dec 16, 2022
📜 Modern Simple HTML DOM Parser for PHP

?? Simple Html Dom Parser for PHP A HTML DOM parser written in PHP - let you manipulate HTML in a very easy way! This is a fork of PHP Simple HTML DOM

Lars Moelleken 665 Jan 4, 2023
Parsica - PHP Parser Combinators - The easiest way to build robust parsers.

Parsica The easiest way to build robust parsers in PHP.

null 0 Feb 22, 2022
This is a php parser for plantuml source file.

PlantUML parser for PHP Overview This package builds AST of class definitions from plantuml files. This package works only with php. Installation Via

Tasuku Yamashita 5 May 29, 2022
A PHP hold'em range parser

mattjmattj/holdem-range-parser A PHP hold'em range parser Installation No published package yet, so you'll have to clone the project manually, or add

Matthias Jouan 1 Feb 2, 2022