XML_SaxFilters
[ class tree: XML_SaxFilters ] [ index: XML_SaxFilters ] [ all elements ]

Source for file htmlfilter.php

Documentation is available at htmlfilter.php

  1. <?php
  2. /*
  3. Filtering HTML output to remove whitespace and
  4. convert tags / attrs to lower. Note how using
  5. references allows the incoming XML stream to be
  6. modified.
  7. */
  8. # require_once 'XML/SaxFilters.php'; // This is the normal way to do it
  9.  
  10. # Done to help development
  11. if !@include_once 'XML/SaxFilters.php' {
  12.     define('XML_SAXFILTERS''../../');
  13.     include_once XML_SAXFILTERS . 'SaxFilters.php';
  14. }
  15.  
  16. //----------------------------------------------------------------------------
  17. /**
  18. * Strips whitespace
  19. */
  20.     /**
  21.     * Whether we're inside an HTML page
  22.     * @var boolean (default = FALSE)
  23.     * @access private
  24.     */
  25.     var $inHtml = FALSE;
  26.  
  27.     /**
  28.     * Whether we're inside an HTML where the contents
  29.     * are preformatted e.g. pre or script
  30.     * @var boolean (default = FALSE)
  31.     * @access private
  32.     */
  33.     var $inPre = FALSE;
  34.     
  35.     function open($tag,$attrs,$empty{
  36.         $this->child->open($tag,$attrs,$empty);
  37.         switch strtolower($tag) ) {
  38.             case 'textarea':
  39.             case 'script':
  40.             case 'pre':
  41.                 $this->inPre = TRUE;
  42.             break;
  43.             case 'html':
  44.                 $this->inHtml = TRUE;
  45.             break;
  46.         }
  47.     }
  48.  
  49.     function close($tag,$empty{
  50.         $this->child->close($tag,$empty);
  51.         switch strtolower($tag) ) {
  52.             case 'textarea':
  53.             case 'script':
  54.             case 'pre':
  55.                 $this->inPre = FALSE;
  56.             break;
  57.             case 'html':
  58.                 $this->inHtml = FALSE;
  59.             break;
  60.         }
  61.     }
  62.     function data($text{
  63.         $this->child->data($text);
  64.         if !$this->inPre && $this->inHtml {
  65.             // Note here - DOT NOT pass by reference
  66.             $text preg_replace('/\s+/u'' '$text);
  67.         }
  68.     }
  69. }
  70.  
  71. //----------------------------------------------------------------------------
  72. /**
  73. * Converts tags and attribute names to lower case
  74. */
  75.  
  76.     function open($tag,$attrs,$empty{
  77.         $tag strtolower($tag);
  78.         if is_array($attrs) ) {
  79.             $attrs array_change_key_case($attrs,CASE_LOWER);
  80.         }
  81.     }
  82.  
  83.     function close($tag,$empty{
  84.         $tag strtolower($tag);
  85.     }
  86. }
  87.  
  88. //----------------------------------------------------------------------------
  89. /**
  90. * Reconstructs the HTML - this filter is chained directly to the parser. By
  91. * chaining the WhitespaceFilter and TagsToLowerFilter filter to this filters,
  92. * it's possible to modify the incoming XML stream before it is handled by
  93. * HTMLFilter
  94. */
  95.     var $html = '';
  96.     function open($tag,$attrs,$empty{
  97.         $this->child->open($tag,$attrs,$empty);
  98.         $this->html.= '<'.$tag;
  99.         if is_array($attrs) ) {
  100.             foreach $attrs as $key => $val {
  101.                 if $val === TRUE {
  102.                     $this->html.=', '.$key;
  103.                 else {
  104.                     $this->html.=' '.$key.='="'.$val.'"';
  105.                 }
  106.             }
  107.         }
  108.         if $empty {
  109.             $this->html.= '/>';
  110.         else {
  111.             $this->html.= '>';
  112.         }
  113.     }
  114.  
  115.     function close($tag,$empty{
  116.         $this->child->close($tag,$empty);
  117.         if !$empty {
  118.             $this->html.='</'.$tag.'>';
  119.         }
  120.     }
  121.     
  122.     function data($data{
  123.         $this->child->data($data);
  124.         $this->html.= $data;
  125.     }
  126.     
  127.     function escape($data{
  128.         $this->child->escape($data);
  129.         $this->html.='<!'.$data.'>';
  130.     }
  131.     function getHTML({
  132.         return $this->html;
  133.     }
  134. }
  135. //----------------------------------------------------------------------------
  136.  
  137. // Capture the HTML with output buffering
  138. include 'example.html';
  139. $html ob_get_contents();
  140.  
  141. // Create the filters
  142. $HF new HTMLFilter();
  143. $filters = array();
  144. $filters[$HF;
  145. $filters[new WhitespaceFilter();
  146. $filters[new TagsToLowerFilter();
  147.  
  148. // Use the HTMLSax parser - note fourth argument - builds the chain
  149. $parser XML_SaxFilters_createParser('HTMLSax','String',$html);
  150.  
  151. // Chain the filters
  152. XML_SaxFilters_buildChain($parser,$filters);
  153.  
  154. // Parse the HTML
  155. $parser->parse();
  156.  
  157. // Display it
  158. echo $HF->getHTML();
  159. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.