Source for file PEARSax3.php

Documentation is available at PEARSax3.php

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

Documentation generated on Thu, 19 Jun 2008 18:49:51 -0400 by phpDocumentor 1.4.2