Source for file CSSDefinition.php

Documentation is available at CSSDefinition.php

  1. <?php
  2.  
  3. /**
  4.  * Defines allowed CSS attributes and what their values are.
  5.  * @see HTMLPurifier_HTMLDefinition
  6.  */
  7. {
  8.     
  9.     public $type = 'CSS';
  10.     
  11.     /**
  12.      * Assoc array of attribute name to definition object.
  13.      */
  14.     public $info = array();
  15.     
  16.     /**
  17.      * Constructs the info array.  The meat of this class.
  18.      */
  19.     protected function doSetup($config{
  20.         
  21.         $this->info['text-align'new HTMLPurifier_AttrDef_Enum(
  22.             array('left''right''center''justify')false);
  23.         
  24.         $border_style =
  25.         $this->info['border-bottom-style'
  26.         $this->info['border-right-style'
  27.         $this->info['border-left-style'
  28.         $this->info['border-top-style'=  new HTMLPurifier_AttrDef_Enum(
  29.             array('none''hidden''dotted''dashed''solid''double',
  30.             'groove''ridge''inset''outset')false);
  31.         
  32.         $this->info['border-style'new HTMLPurifier_AttrDef_CSS_Multiple($border_style);
  33.         
  34.         $this->info['clear'new HTMLPurifier_AttrDef_Enum(
  35.             array('none''left''right''both')false);
  36.         $this->info['float'new HTMLPurifier_AttrDef_Enum(
  37.             array('none''left''right')false);
  38.         $this->info['font-style'new HTMLPurifier_AttrDef_Enum(
  39.             array('normal''italic''oblique')false);
  40.         $this->info['font-variant'new HTMLPurifier_AttrDef_Enum(
  41.             array('normal''small-caps')false);
  42.         
  43.         $uri_or_none new HTMLPurifier_AttrDef_CSS_Composite(
  44.             array(
  45.                 new HTMLPurifier_AttrDef_Enum(array('none')),
  46.                 new HTMLPurifier_AttrDef_CSS_URI()
  47.             )
  48.         );
  49.         
  50.         $this->info['list-style-position'new HTMLPurifier_AttrDef_Enum(
  51.             array('inside''outside')false);
  52.         $this->info['list-style-type'new HTMLPurifier_AttrDef_Enum(
  53.             array('disc''circle''square''decimal''lower-roman',
  54.             'upper-roman''lower-alpha''upper-alpha''none')false);
  55.         $this->info['list-style-image'$uri_or_none;
  56.         
  57.         $this->info['list-style'new HTMLPurifier_AttrDef_CSS_ListStyle($config);
  58.         
  59.         $this->info['text-transform'new HTMLPurifier_AttrDef_Enum(
  60.             array('capitalize''uppercase''lowercase''none')false);
  61.         $this->info['color'new HTMLPurifier_AttrDef_CSS_Color();
  62.         
  63.         $this->info['background-image'$uri_or_none;
  64.         $this->info['background-repeat'new HTMLPurifier_AttrDef_Enum(
  65.             array('repeat''repeat-x''repeat-y''no-repeat')
  66.         );
  67.         $this->info['background-attachment'new HTMLPurifier_AttrDef_Enum(
  68.             array('scroll''fixed')
  69.         );
  70.         $this->info['background-position'new HTMLPurifier_AttrDef_CSS_BackgroundPosition();
  71.         
  72.         $border_color 
  73.         $this->info['border-top-color'
  74.         $this->info['border-bottom-color'
  75.         $this->info['border-left-color'
  76.         $this->info['border-right-color'
  77.         $this->info['background-color'new HTMLPurifier_AttrDef_CSS_Composite(array(
  78.             new HTMLPurifier_AttrDef_Enum(array('transparent')),
  79.             new HTMLPurifier_AttrDef_CSS_Color()
  80.         ));
  81.         
  82.         $this->info['background'new HTMLPurifier_AttrDef_CSS_Background($config);
  83.         
  84.         $this->info['border-color'new HTMLPurifier_AttrDef_CSS_Multiple($border_color);
  85.         
  86.         $border_width 
  87.         $this->info['border-top-width'
  88.         $this->info['border-bottom-width'
  89.         $this->info['border-left-width'
  90.         $this->info['border-right-width'new HTMLPurifier_AttrDef_CSS_Composite(array(
  91.             new HTMLPurifier_AttrDef_Enum(array('thin''medium''thick')),
  92.             new HTMLPurifier_AttrDef_CSS_Length('0'//disallow negative
  93.         ));
  94.         
  95.         $this->info['border-width'new HTMLPurifier_AttrDef_CSS_Multiple($border_width);
  96.         
  97.         $this->info['letter-spacing'new HTMLPurifier_AttrDef_CSS_Composite(array(
  98.             new HTMLPurifier_AttrDef_Enum(array('normal')),
  99.             new HTMLPurifier_AttrDef_CSS_Length()
  100.         ));
  101.         
  102.         $this->info['word-spacing'new HTMLPurifier_AttrDef_CSS_Composite(array(
  103.             new HTMLPurifier_AttrDef_Enum(array('normal')),
  104.             new HTMLPurifier_AttrDef_CSS_Length()
  105.         ));
  106.         
  107.         $this->info['font-size'new HTMLPurifier_AttrDef_CSS_Composite(array(
  108.             new HTMLPurifier_AttrDef_Enum(array('xx-small''x-small',
  109.                 'small''medium''large''x-large''xx-large',
  110.                 'larger''smaller')),
  111.             new HTMLPurifier_AttrDef_CSS_Percentage(),
  112.             new HTMLPurifier_AttrDef_CSS_Length()
  113.         ));
  114.         
  115.         $this->info['line-height'new HTMLPurifier_AttrDef_CSS_Composite(array(
  116.             new HTMLPurifier_AttrDef_Enum(array('normal')),
  117.             new HTMLPurifier_AttrDef_CSS_Number(true)// no negatives
  118.             new HTMLPurifier_AttrDef_CSS_Length('0'),
  119.             new HTMLPurifier_AttrDef_CSS_Percentage(true)
  120.         ));
  121.         
  122.         $margin =
  123.         $this->info['margin-top'
  124.         $this->info['margin-bottom'
  125.         $this->info['margin-left'
  126.         $this->info['margin-right'new HTMLPurifier_AttrDef_CSS_Composite(array(
  127.             new HTMLPurifier_AttrDef_CSS_Length(),
  128.             new HTMLPurifier_AttrDef_CSS_Percentage(),
  129.             new HTMLPurifier_AttrDef_Enum(array('auto'))
  130.         ));
  131.         
  132.         $this->info['margin'new HTMLPurifier_AttrDef_CSS_Multiple($margin);
  133.         
  134.         // non-negative
  135.         $padding =
  136.         $this->info['padding-top'
  137.         $this->info['padding-bottom'
  138.         $this->info['padding-left'
  139.         $this->info['padding-right'new HTMLPurifier_AttrDef_CSS_Composite(array(
  140.             new HTMLPurifier_AttrDef_CSS_Length('0'),
  141.             new HTMLPurifier_AttrDef_CSS_Percentage(true)
  142.         ));
  143.         
  144.         $this->info['padding'new HTMLPurifier_AttrDef_CSS_Multiple($padding);
  145.         
  146.         $this->info['text-indent'new HTMLPurifier_AttrDef_CSS_Composite(array(
  147.             new HTMLPurifier_AttrDef_CSS_Length(),
  148.             new HTMLPurifier_AttrDef_CSS_Percentage()
  149.         ));
  150.         
  151.         $trusted_wh new HTMLPurifier_AttrDef_CSS_Composite(array(
  152.             new HTMLPurifier_AttrDef_CSS_Length('0'),
  153.             new HTMLPurifier_AttrDef_CSS_Percentage(true),
  154.             new HTMLPurifier_AttrDef_Enum(array('auto'))
  155.         ));
  156.         $max $config->get('CSS''MaxImgLength');
  157.         
  158.         $this->info['width'=
  159.         $this->info['height'=
  160.             $max === null ?
  161.             $trusted_wh 
  162.             new HTMLPurifier_AttrDef_Switch('img',
  163.                 // For img tags:
  164.                 new HTMLPurifier_AttrDef_CSS_Composite(array(
  165.                     new HTMLPurifier_AttrDef_CSS_Length('0'$max),
  166.                     new HTMLPurifier_AttrDef_Enum(array('auto'))
  167.                 )),
  168.                 // For everyone else:
  169.                 $trusted_wh
  170.             );
  171.         
  172.         $this->info['text-decoration'new HTMLPurifier_AttrDef_CSS_TextDecoration();
  173.         
  174.         $this->info['font-family'new HTMLPurifier_AttrDef_CSS_FontFamily();
  175.         
  176.         // this could use specialized code
  177.         $this->info['font-weight'new HTMLPurifier_AttrDef_Enum(
  178.             array('normal''bold''bolder''lighter''100''200''300',
  179.             '400''500''600''700''800''900')false);
  180.         
  181.         // MUST be called after other font properties, as it references
  182.         // a CSSDefinition object
  183.         $this->info['font'new HTMLPurifier_AttrDef_CSS_Font($config);
  184.         
  185.         // same here
  186.         $this->info['border'=
  187.         $this->info['border-bottom'
  188.         $this->info['border-top'
  189.         $this->info['border-left'
  190.         $this->info['border-right'new HTMLPurifier_AttrDef_CSS_Border($config);
  191.         
  192.         $this->info['border-collapse'new HTMLPurifier_AttrDef_Enum(array(
  193.             'collapse''separate'));
  194.         
  195.         $this->info['caption-side'new HTMLPurifier_AttrDef_Enum(array(
  196.             'top''bottom'));
  197.         
  198.         $this->info['table-layout'new HTMLPurifier_AttrDef_Enum(array(
  199.             'auto''fixed'));
  200.         
  201.         $this->info['vertical-align'new HTMLPurifier_AttrDef_CSS_Composite(array(
  202.             new HTMLPurifier_AttrDef_Enum(array('baseline''sub''super',
  203.                 'top''text-top''middle''bottom''text-bottom')),
  204.             new HTMLPurifier_AttrDef_CSS_Length(),
  205.             new HTMLPurifier_AttrDef_CSS_Percentage()
  206.         ));
  207.         
  208.         $this->info['border-spacing'new HTMLPurifier_AttrDef_CSS_Multiple(new HTMLPurifier_AttrDef_CSS_Length()2);
  209.         
  210.         // partial support
  211.         $this->info['white-space'new HTMLPurifier_AttrDef_Enum(array('nowrap'));
  212.         
  213.         if ($config->get('CSS''Proprietary')) {
  214.             $this->doSetupProprietary($config);
  215.         }
  216.         
  217.         if ($config->get('CSS''AllowTricky')) {
  218.             $this->doSetupTricky($config);
  219.         }
  220.         
  221.         $allow_important $config->get('CSS''AllowImportant');
  222.         // wrap all attr-defs with decorator that handles !important
  223.         foreach ($this->info as $k => $v{
  224.             $this->info[$knew HTMLPurifier_AttrDef_CSS_ImportantDecorator($v$allow_important);
  225.         }
  226.         
  227.         $this->setupConfigStuff($config);
  228.     }
  229.     
  230.     protected function doSetupProprietary($config{
  231.         // Internet Explorer only scrollbar colors
  232.         $this->info['scrollbar-arrow-color']        new HTMLPurifier_AttrDef_CSS_Color();
  233.         $this->info['scrollbar-base-color']         new HTMLPurifier_AttrDef_CSS_Color();
  234.         $this->info['scrollbar-darkshadow-color']   new HTMLPurifier_AttrDef_CSS_Color();
  235.         $this->info['scrollbar-face-color']         new HTMLPurifier_AttrDef_CSS_Color();
  236.         $this->info['scrollbar-highlight-color']    new HTMLPurifier_AttrDef_CSS_Color();
  237.         $this->info['scrollbar-shadow-color']       new HTMLPurifier_AttrDef_CSS_Color();
  238.         
  239.         // technically not proprietary, but CSS3, and no one supports it
  240.         $this->info['opacity']          new HTMLPurifier_AttrDef_CSS_AlphaValue();
  241.         $this->info['-moz-opacity']     new HTMLPurifier_AttrDef_CSS_AlphaValue();
  242.         $this->info['-khtml-opacity']   new HTMLPurifier_AttrDef_CSS_AlphaValue();
  243.         
  244.         // only opacity, for now
  245.         $this->info['filter'new HTMLPurifier_AttrDef_CSS_Filter();
  246.         
  247.     }
  248.     
  249.     protected function doSetupTricky($config{
  250.         $this->info['display'new HTMLPurifier_AttrDef_Enum(array(
  251.             'inline''block''list-item''run-in''compact',
  252.             'marker''table''inline-table''table-row-group',
  253.             'table-header-group''table-footer-group''table-row',
  254.             'table-column-group''table-column''table-cell''table-caption''none'
  255.         ));
  256.         $this->info['visibility'new HTMLPurifier_AttrDef_Enum(array(
  257.             'visible''hidden''collapse'
  258.         ));
  259.     }
  260.     
  261.     
  262.     /**
  263.      * Performs extra config-based processing. Based off of
  264.      * HTMLPurifier_HTMLDefinition.
  265.      * @todo Refactor duplicate elements into common class (probably using
  266.      *        composition, not inheritance).
  267.      */
  268.     protected function setupConfigStuff($config{
  269.         
  270.         // setup allowed elements
  271.         $support "(for information on implementing this, see the ".
  272.                    "support forums) ";
  273.         $allowed_attributes $config->get('CSS''AllowedProperties');
  274.         if ($allowed_attributes !== null{
  275.             foreach ($this->info as $name => $d{
  276.                 if(!isset($allowed_attributes[$name])) unset($this->info[$name]);
  277.                 unset($allowed_attributes[$name]);
  278.             }
  279.             // emit errors
  280.             foreach ($allowed_attributes as $name => $d{
  281.                 // :TODO: Is this htmlspecialchars() call really necessary?
  282.                 $name htmlspecialchars($name);
  283.                 trigger_error("Style attribute '$name' is not supported $support"E_USER_WARNING);
  284.             }
  285.         }
  286.         
  287.     }
  288. }

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