Parser.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. require_once dirname(__FILE__) . '/Data.php';
  3. require_once dirname(__FILE__) . '/InputStream.php';
  4. require_once dirname(__FILE__) . '/TreeBuilder.php';
  5. require_once dirname(__FILE__) . '/Tokenizer.php';
  6. /**
  7. * Outwards facing interface for HTML5.
  8. */
  9. class HTML5_Parser
  10. {
  11. /**
  12. * Parses a full HTML document.
  13. * @param $text HTML text to parse
  14. * @param $builder Custom builder implementation
  15. * @return Parsed HTML as DOMDocument
  16. */
  17. static public function parse($text, $builder = null) {
  18. $tokenizer = new HTML5_Tokenizer($text, $builder);
  19. $tokenizer->parse();
  20. return $tokenizer->save();
  21. }
  22. /**
  23. * Parses an HTML fragment.
  24. * @param $text HTML text to parse
  25. * @param $context String name of context element to pretend parsing is in.
  26. * @param $builder Custom builder implementation
  27. * @return Parsed HTML as DOMDocument
  28. */
  29. static public function parseFragment($text, $context = null, $builder = null) {
  30. $tokenizer = new HTML5_Tokenizer($text, $builder);
  31. $tokenizer->parseFragment($context);
  32. return $tokenizer->save();
  33. }
  34. }