PEARSax3.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Proof-of-concept lexer that uses the PEAR package XML_HTMLSax3 to parse HTML.
  4. *
  5. * PEAR, not suprisingly, also has a SAX parser for HTML. I don't know
  6. * very much about implementation, but it's fairly well written. However, that
  7. * abstraction comes at a price: performance. You need to have it installed,
  8. * and if the API changes, it might break our adapter. Not sure whether or not
  9. * it's UTF-8 aware, but it has some entity parsing trouble (in all areas,
  10. * text and attributes).
  11. *
  12. * Quite personally, I don't recommend using the PEAR class, and the defaults
  13. * don't use it. The unit tests do perform the tests on the SAX parser too, but
  14. * whatever it does for poorly formed HTML is up to it.
  15. *
  16. * @todo Generalize so that XML_HTMLSax is also supported.
  17. *
  18. * @warning Entity-resolution inside attributes is broken.
  19. */
  20. class HTMLPurifier_Lexer_PEARSax3 extends HTMLPurifier_Lexer
  21. {
  22. /**
  23. * Internal accumulator array for SAX parsers.
  24. */
  25. protected $tokens = array();
  26. protected $last_token_was_empty;
  27. private $parent_handler;
  28. private $stack = array();
  29. public function tokenizeHTML($string, $config, $context) {
  30. $this->tokens = array();
  31. $this->last_token_was_empty = false;
  32. $string = $this->normalize($string, $config, $context);
  33. $this->parent_handler = set_error_handler(array($this, 'muteStrictErrorHandler'));
  34. $parser = new XML_HTMLSax3();
  35. $parser->set_object($this);
  36. $parser->set_element_handler('openHandler','closeHandler');
  37. $parser->set_data_handler('dataHandler');
  38. $parser->set_escape_handler('escapeHandler');
  39. // doesn't seem to work correctly for attributes
  40. $parser->set_option('XML_OPTION_ENTITIES_PARSED', 1);
  41. $parser->parse($string);
  42. restore_error_handler();
  43. return $this->tokens;
  44. }
  45. /**
  46. * Open tag event handler, interface is defined by PEAR package.
  47. */
  48. public function openHandler(&$parser, $name, $attrs, $closed) {
  49. // entities are not resolved in attrs
  50. foreach ($attrs as $key => $attr) {
  51. $attrs[$key] = $this->parseData($attr);
  52. }
  53. if ($closed) {
  54. $this->tokens[] = new HTMLPurifier_Token_Empty($name, $attrs);
  55. $this->last_token_was_empty = true;
  56. } else {
  57. $this->tokens[] = new HTMLPurifier_Token_Start($name, $attrs);
  58. }
  59. $this->stack[] = $name;
  60. return true;
  61. }
  62. /**
  63. * Close tag event handler, interface is defined by PEAR package.
  64. */
  65. public function closeHandler(&$parser, $name) {
  66. // HTMLSax3 seems to always send empty tags an extra close tag
  67. // check and ignore if you see it:
  68. // [TESTME] to make sure it doesn't overreach
  69. if ($this->last_token_was_empty) {
  70. $this->last_token_was_empty = false;
  71. return true;
  72. }
  73. $this->tokens[] = new HTMLPurifier_Token_End($name);
  74. if (!empty($this->stack)) array_pop($this->stack);
  75. return true;
  76. }
  77. /**
  78. * Data event handler, interface is defined by PEAR package.
  79. */
  80. public function dataHandler(&$parser, $data) {
  81. $this->last_token_was_empty = false;
  82. $this->tokens[] = new HTMLPurifier_Token_Text($data);
  83. return true;
  84. }
  85. /**
  86. * Escaped text handler, interface is defined by PEAR package.
  87. */
  88. public function escapeHandler(&$parser, $data) {
  89. if (strpos($data, '--') === 0) {
  90. // remove trailing and leading double-dashes
  91. $data = substr($data, 2);
  92. if (strlen($data) >= 2 && substr($data, -2) == "--") {
  93. $data = substr($data, 0, -2);
  94. }
  95. if (isset($this->stack[sizeof($this->stack) - 1]) &&
  96. $this->stack[sizeof($this->stack) - 1] == "style") {
  97. $this->tokens[] = new HTMLPurifier_Token_Text($data);
  98. } else {
  99. $this->tokens[] = new HTMLPurifier_Token_Comment($data);
  100. }
  101. $this->last_token_was_empty = false;
  102. }
  103. // CDATA is handled elsewhere, but if it was handled here:
  104. //if (strpos($data, '[CDATA[') === 0) {
  105. // $this->tokens[] = new HTMLPurifier_Token_Text(
  106. // substr($data, 7, strlen($data) - 9) );
  107. //}
  108. return true;
  109. }
  110. /**
  111. * An error handler that mutes strict errors
  112. */
  113. public function muteStrictErrorHandler($errno, $errstr, $errfile=null, $errline=null, $errcontext=null) {
  114. if ($errno == E_STRICT) return;
  115. return call_user_func($this->parent_handler, $errno, $errstr, $errfile, $errline, $errcontext);
  116. }
  117. }
  118. // vim: et sw=4 sts=4