]> git.mxchange.org Git - friendica.git/blob - library/HTML5/Parser.php
Moving the clean up code to a central place.
[friendica.git] / library / HTML5 / Parser.php
1 <?php
2
3 require_once dirname(__FILE__) . '/Data.php';
4 require_once dirname(__FILE__) . '/InputStream.php';
5 require_once dirname(__FILE__) . '/TreeBuilder.php';
6 require_once dirname(__FILE__) . '/Tokenizer.php';
7
8 /**
9  * Outwards facing interface for HTML5.
10  */
11 class HTML5_Parser
12 {
13     /**
14      * Parses a full HTML document.
15      * @param $text HTML text to parse
16      * @param $builder Custom builder implementation
17      * @return Parsed HTML as DOMDocument
18      */
19     static public function parse($text, $builder = null) {
20
21         // Cleanup invalid HTML
22         $doc = new DOMDocument();
23         @$doc->loadHTML($text);
24         $text = $doc->saveHTML();
25
26         $tokenizer = new HTML5_Tokenizer($text, $builder);
27         $tokenizer->parse();
28         return $tokenizer->save();
29     }
30     /**
31      * Parses an HTML fragment.
32      * @param $text HTML text to parse
33      * @param $context String name of context element to pretend parsing is in.
34      * @param $builder Custom builder implementation
35      * @return Parsed HTML as DOMDocument
36      */
37     static public function parseFragment($text, $context = null, $builder = null) {
38         $tokenizer = new HTML5_Tokenizer($text, $builder);
39         $tokenizer->parseFragment($context);
40         return $tokenizer->save();
41     }
42 }