1 | <?php |
||
3 | namespace SilverStripe\HTML5; |
||
4 | |||
5 | use Exception; |
||
6 | use SilverStripe\View\Parsers\HTMLValue; |
||
7 | |||
8 | class HTML5Value extends HTMLValue |
||
9 | { |
||
10 | public function setContent($content) |
||
11 | { |
||
12 | require_once(HTML5LIB_PATH.'/HTML5/Parser.php'); |
||
|
|||
13 | |||
14 | // Convert any errors to exceptions |
||
15 | set_error_handler( |
||
16 | function ($no, $str) { |
||
17 | throw new Exception("HTML Parse Error: ".$str); |
||
18 | }, |
||
19 | error_reporting() |
||
20 | ); |
||
21 | |||
22 | // Use HTML5lib to parse the HTML fragment |
||
23 | try { |
||
24 | $document = \HTML5_Parser::parse( |
||
25 | '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>'. |
||
26 | "<body>$content</body></html>" |
||
27 | ); |
||
28 | } catch (Exception $e) { |
||
29 | $document = false; |
||
30 | } |
||
31 | |||
32 | // Disable our error handler (restoring to previous value) |
||
33 | restore_error_handler(); |
||
34 | |||
35 | // If we couldn't parse the HTML, set the error state |
||
36 | if ($document) { |
||
37 | $this->setDocument($document); |
||
38 | } else { |
||
43 |