GithubHelp home page GithubHelp logo

jasonll / html5-php Goto Github PK

View Code? Open in Web Editor NEW

This project forked from masterminds/html5-php

0.0 2.0 0.0 1.97 MB

An HTML5 parser and serializer for PHP.

Home Page: http://masterminds.github.io/html5-php/

License: Other

html5-php's Introduction

HTML5-PHP

This is a highly experimental HTML5 Parser.

The need for an HTML5 parser in PHP is clear. This project initially began with the seemingly abandoned html5lib project original source. But after some initial refactoring work, we began a new parser.

  • An HTML5 serializer [feature complete]
  • Support for PHP namespace [done]
  • Composer support [done]
  • Event-based (SAX-like) parser [feature complete]
  • DOM tree builder [feature complete]
  • Interoperability with QueryPath [in progress]

Build Status

Installation

Install HTML5-PHP using composer.

To install, add masterminds/html5 to your composer.json file:

{
  "require" : {
    "masterminds/html5": "dev-master"
  },
}

(You may substitute dev-master for a more stable release tag, of course.)

From there, use the composer install or composer update commands to install.

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";


// 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.
$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.

The Low-Level API

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

  • An InputStream abstraction that can work with different kinds of input source (not just files and strings).
  • 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 InputStream portion handles direct I/O.
  • 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 used are configurable with the OutputRules being the default. An option can be set by default or at call time to use a different ruleset that implements RulesInterface.
  • 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.
  • The Serializer 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. The parser does not support XML style namespaces via :.
  • 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.
  • 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)

Thanks to...

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

While not much of the orignal 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.

html5-php's People

Contributors

technosophos avatar mattfarina avatar vasiliicuhar avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.