Source for file CSS.php

Documentation is available at CSS.php

  1. <?php
  2.  
  3. /**
  4.  * Validates the HTML attribute style, otherwise known as CSS.
  5.  * @note We don't implement the whole CSS specification, so it might be
  6.  *        difficult to reuse this component in the context of validating
  7.  *        actual stylesheet declarations.
  8.  * @note If we were really serious about validating the CSS, we would
  9.  *        tokenize the styles and then parse the tokens. Obviously, we
  10.  *        are not doing that. Doing that could seriously harm performance,
  11.  *        but would make these components a lot more viable for a CSS
  12.  *        filtering solution.
  13.  */
  14. {
  15.     
  16.     public function validate($css$config$context{
  17.         
  18.         $css $this->parseCDATA($css);
  19.         
  20.         $definition $config->getCSSDefinition();
  21.         
  22.         // we're going to break the spec and explode by semicolons.
  23.         // This is because semicolon rarely appears in escaped form
  24.         // Doing this is generally flaky but fast
  25.         // IT MIGHT APPEAR IN URIs, see HTMLPurifier_AttrDef_CSSURI
  26.         // for details
  27.         
  28.         $declarations explode(';'$css);
  29.         $propvalues array();
  30.         
  31.         /**
  32.          * Name of the current CSS property being validated.
  33.          */
  34.         $property false;
  35.         $context->register('CurrentCSSProperty'$property);
  36.         
  37.         foreach ($declarations as $declaration{
  38.             if (!$declarationcontinue;
  39.             if (!strpos($declaration':')) continue;
  40.             list($property$valueexplode(':'$declaration2);
  41.             $property trim($property);
  42.             $value    trim($value);
  43.             $ok false;
  44.             do {
  45.                 if (isset($definition->info[$property])) {
  46.                     $ok true;
  47.                     break;
  48.                 }
  49.                 if (ctype_lower($property)) break;
  50.                 $property strtolower($property);
  51.                 if (isset($definition->info[$property])) {
  52.                     $ok true;
  53.                     break;
  54.                 }
  55.             while(0);
  56.             if (!$okcontinue;
  57.             // inefficient call, since the validator will do this again
  58.             if (strtolower(trim($value)) !== 'inherit'{
  59.                 // inherit works for everything (but only on the base property)
  60.                 $result $definition->info[$property]->validate(
  61.                     $value$config$context );
  62.             else {
  63.                 $result 'inherit';
  64.             }
  65.             if ($result === falsecontinue;
  66.             $propvalues[$property$result;
  67.         }
  68.         
  69.         $context->destroy('CurrentCSSProperty');
  70.         
  71.         // procedure does not write the new CSS simultaneously, so it's
  72.         // slightly inefficient, but it's the only way of getting rid of
  73.         // duplicates. Perhaps config to optimize it, but not now.
  74.         
  75.         $new_declarations '';
  76.         foreach ($propvalues as $prop => $value{
  77.             $new_declarations .= "$prop:$value;";
  78.         }
  79.         
  80.         return $new_declarations $new_declarations false;
  81.         
  82.     }
  83.     
  84. }

Documentation generated on Thu, 19 Jun 2008 18:48:58 -0400 by phpDocumentor 1.4.2