HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/CSSDefinition.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class HTMLPurifier_CSSDefinition extends HTMLPurifier_Definition
00008 {
00009 
00010     public $type = 'CSS';
00011 
00015     public $info = array();
00016 
00020     protected function doSetup($config) {
00021 
00022         $this->info['text-align'] = new HTMLPurifier_AttrDef_Enum(
00023             array('left', 'right', 'center', 'justify'), false);
00024 
00025         $border_style =
00026         $this->info['border-bottom-style'] =
00027         $this->info['border-right-style'] =
00028         $this->info['border-left-style'] =
00029         $this->info['border-top-style'] =  new HTMLPurifier_AttrDef_Enum(
00030             array('none', 'hidden', 'dotted', 'dashed', 'solid', 'double',
00031             'groove', 'ridge', 'inset', 'outset'), false);
00032 
00033         $this->info['border-style'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_style);
00034 
00035         $this->info['clear'] = new HTMLPurifier_AttrDef_Enum(
00036             array('none', 'left', 'right', 'both'), false);
00037         $this->info['float'] = new HTMLPurifier_AttrDef_Enum(
00038             array('none', 'left', 'right'), false);
00039         $this->info['font-style'] = new HTMLPurifier_AttrDef_Enum(
00040             array('normal', 'italic', 'oblique'), false);
00041         $this->info['font-variant'] = new HTMLPurifier_AttrDef_Enum(
00042             array('normal', 'small-caps'), false);
00043 
00044         $uri_or_none = new HTMLPurifier_AttrDef_CSS_Composite(
00045             array(
00046                 new HTMLPurifier_AttrDef_Enum(array('none')),
00047                 new HTMLPurifier_AttrDef_CSS_URI()
00048             )
00049         );
00050 
00051         $this->info['list-style-position'] = new HTMLPurifier_AttrDef_Enum(
00052             array('inside', 'outside'), false);
00053         $this->info['list-style-type'] = new HTMLPurifier_AttrDef_Enum(
00054             array('disc', 'circle', 'square', 'decimal', 'lower-roman',
00055             'upper-roman', 'lower-alpha', 'upper-alpha', 'none'), false);
00056         $this->info['list-style-image'] = $uri_or_none;
00057 
00058         $this->info['list-style'] = new HTMLPurifier_AttrDef_CSS_ListStyle($config);
00059 
00060         $this->info['text-transform'] = new HTMLPurifier_AttrDef_Enum(
00061             array('capitalize', 'uppercase', 'lowercase', 'none'), false);
00062         $this->info['color'] = new HTMLPurifier_AttrDef_CSS_Color();
00063 
00064         $this->info['background-image'] = $uri_or_none;
00065         $this->info['background-repeat'] = new HTMLPurifier_AttrDef_Enum(
00066             array('repeat', 'repeat-x', 'repeat-y', 'no-repeat')
00067         );
00068         $this->info['background-attachment'] = new HTMLPurifier_AttrDef_Enum(
00069             array('scroll', 'fixed')
00070         );
00071         $this->info['background-position'] = new HTMLPurifier_AttrDef_CSS_BackgroundPosition();
00072 
00073         $border_color =
00074         $this->info['border-top-color'] =
00075         $this->info['border-bottom-color'] =
00076         $this->info['border-left-color'] =
00077         $this->info['border-right-color'] =
00078         $this->info['background-color'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00079             new HTMLPurifier_AttrDef_Enum(array('transparent')),
00080             new HTMLPurifier_AttrDef_CSS_Color()
00081         ));
00082 
00083         $this->info['background'] = new HTMLPurifier_AttrDef_CSS_Background($config);
00084 
00085         $this->info['border-color'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_color);
00086 
00087         $border_width =
00088         $this->info['border-top-width'] =
00089         $this->info['border-bottom-width'] =
00090         $this->info['border-left-width'] =
00091         $this->info['border-right-width'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00092             new HTMLPurifier_AttrDef_Enum(array('thin', 'medium', 'thick')),
00093             new HTMLPurifier_AttrDef_CSS_Length('0') //disallow negative
00094         ));
00095 
00096         $this->info['border-width'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_width);
00097 
00098         $this->info['letter-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00099             new HTMLPurifier_AttrDef_Enum(array('normal')),
00100             new HTMLPurifier_AttrDef_CSS_Length()
00101         ));
00102 
00103         $this->info['word-spacing'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00104             new HTMLPurifier_AttrDef_Enum(array('normal')),
00105             new HTMLPurifier_AttrDef_CSS_Length()
00106         ));
00107 
00108         $this->info['font-size'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00109             new HTMLPurifier_AttrDef_Enum(array('xx-small', 'x-small',
00110                 'small', 'medium', 'large', 'x-large', 'xx-large',
00111                 'larger', 'smaller')),
00112             new HTMLPurifier_AttrDef_CSS_Percentage(),
00113             new HTMLPurifier_AttrDef_CSS_Length()
00114         ));
00115 
00116         $this->info['line-height'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00117             new HTMLPurifier_AttrDef_Enum(array('normal')),
00118             new HTMLPurifier_AttrDef_CSS_Number(true), // no negatives
00119             new HTMLPurifier_AttrDef_CSS_Length('0'),
00120             new HTMLPurifier_AttrDef_CSS_Percentage(true)
00121         ));
00122 
00123         $margin =
00124         $this->info['margin-top'] =
00125         $this->info['margin-bottom'] =
00126         $this->info['margin-left'] =
00127         $this->info['margin-right'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00128             new HTMLPurifier_AttrDef_CSS_Length(),
00129             new HTMLPurifier_AttrDef_CSS_Percentage(),
00130             new HTMLPurifier_AttrDef_Enum(array('auto'))
00131         ));
00132 
00133         $this->info['margin'] = new HTMLPurifier_AttrDef_CSS_Multiple($margin);
00134 
00135         // non-negative
00136         $padding =
00137         $this->info['padding-top'] =
00138         $this->info['padding-bottom'] =
00139         $this->info['padding-left'] =
00140         $this->info['padding-right'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00141             new HTMLPurifier_AttrDef_CSS_Length('0'),
00142             new HTMLPurifier_AttrDef_CSS_Percentage(true)
00143         ));
00144 
00145         $this->info['padding'] = new HTMLPurifier_AttrDef_CSS_Multiple($padding);
00146 
00147         $this->info['text-indent'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00148             new HTMLPurifier_AttrDef_CSS_Length(),
00149             new HTMLPurifier_AttrDef_CSS_Percentage()
00150         ));
00151 
00152         $trusted_wh = new HTMLPurifier_AttrDef_CSS_Composite(array(
00153             new HTMLPurifier_AttrDef_CSS_Length('0'),
00154             new HTMLPurifier_AttrDef_CSS_Percentage(true),
00155             new HTMLPurifier_AttrDef_Enum(array('auto'))
00156         ));
00157         $max = $config->get('CSS.MaxImgLength');
00158 
00159         $this->info['width'] =
00160         $this->info['height'] =
00161             $max === null ?
00162             $trusted_wh :
00163             new HTMLPurifier_AttrDef_Switch('img',
00164                 // For img tags:
00165                 new HTMLPurifier_AttrDef_CSS_Composite(array(
00166                     new HTMLPurifier_AttrDef_CSS_Length('0', $max),
00167                     new HTMLPurifier_AttrDef_Enum(array('auto'))
00168                 )),
00169                 // For everyone else:
00170                 $trusted_wh
00171             );
00172 
00173         $this->info['text-decoration'] = new HTMLPurifier_AttrDef_CSS_TextDecoration();
00174 
00175         $this->info['font-family'] = new HTMLPurifier_AttrDef_CSS_FontFamily();
00176 
00177         // this could use specialized code
00178         $this->info['font-weight'] = new HTMLPurifier_AttrDef_Enum(
00179             array('normal', 'bold', 'bolder', 'lighter', '100', '200', '300',
00180             '400', '500', '600', '700', '800', '900'), false);
00181 
00182         // MUST be called after other font properties, as it references
00183         // a CSSDefinition object
00184         $this->info['font'] = new HTMLPurifier_AttrDef_CSS_Font($config);
00185 
00186         // same here
00187         $this->info['border'] =
00188         $this->info['border-bottom'] =
00189         $this->info['border-top'] =
00190         $this->info['border-left'] =
00191         $this->info['border-right'] = new HTMLPurifier_AttrDef_CSS_Border($config);
00192 
00193         $this->info['border-collapse'] = new HTMLPurifier_AttrDef_Enum(array(
00194             'collapse', 'separate'));
00195 
00196         $this->info['caption-side'] = new HTMLPurifier_AttrDef_Enum(array(
00197             'top', 'bottom'));
00198 
00199         $this->info['table-layout'] = new HTMLPurifier_AttrDef_Enum(array(
00200             'auto', 'fixed'));
00201 
00202         $this->info['vertical-align'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00203             new HTMLPurifier_AttrDef_Enum(array('baseline', 'sub', 'super',
00204                 'top', 'text-top', 'middle', 'bottom', 'text-bottom')),
00205             new HTMLPurifier_AttrDef_CSS_Length(),
00206             new HTMLPurifier_AttrDef_CSS_Percentage()
00207         ));
00208 
00209         $this->info['border-spacing'] = new HTMLPurifier_AttrDef_CSS_Multiple(new HTMLPurifier_AttrDef_CSS_Length(), 2);
00210 
00211         // partial support
00212         $this->info['white-space'] = new HTMLPurifier_AttrDef_Enum(array('nowrap'));
00213 
00214         if ($config->get('CSS.Proprietary')) {
00215             $this->doSetupProprietary($config);
00216         }
00217 
00218         if ($config->get('CSS.AllowTricky')) {
00219             $this->doSetupTricky($config);
00220         }
00221 
00222         if ($config->get('CSS.Trusted')) {
00223             $this->doSetupTrusted($config);
00224         }
00225 
00226         $allow_important = $config->get('CSS.AllowImportant');
00227         // wrap all attr-defs with decorator that handles !important
00228         foreach ($this->info as $k => $v) {
00229             $this->info[$k] = new HTMLPurifier_AttrDef_CSS_ImportantDecorator($v, $allow_important);
00230         }
00231 
00232         $this->setupConfigStuff($config);
00233     }
00234 
00235     protected function doSetupProprietary($config) {
00236         // Internet Explorer only scrollbar colors
00237         $this->info['scrollbar-arrow-color']        = new HTMLPurifier_AttrDef_CSS_Color();
00238         $this->info['scrollbar-base-color']         = new HTMLPurifier_AttrDef_CSS_Color();
00239         $this->info['scrollbar-darkshadow-color']   = new HTMLPurifier_AttrDef_CSS_Color();
00240         $this->info['scrollbar-face-color']         = new HTMLPurifier_AttrDef_CSS_Color();
00241         $this->info['scrollbar-highlight-color']    = new HTMLPurifier_AttrDef_CSS_Color();
00242         $this->info['scrollbar-shadow-color']       = new HTMLPurifier_AttrDef_CSS_Color();
00243 
00244         // technically not proprietary, but CSS3, and no one supports it
00245         $this->info['opacity']          = new HTMLPurifier_AttrDef_CSS_AlphaValue();
00246         $this->info['-moz-opacity']     = new HTMLPurifier_AttrDef_CSS_AlphaValue();
00247         $this->info['-khtml-opacity']   = new HTMLPurifier_AttrDef_CSS_AlphaValue();
00248 
00249         // only opacity, for now
00250         $this->info['filter'] = new HTMLPurifier_AttrDef_CSS_Filter();
00251 
00252     }
00253 
00254     protected function doSetupTricky($config) {
00255         $this->info['display'] = new HTMLPurifier_AttrDef_Enum(array(
00256             'inline', 'block', 'list-item', 'run-in', 'compact',
00257             'marker', 'table', 'inline-table', 'table-row-group',
00258             'table-header-group', 'table-footer-group', 'table-row',
00259             'table-column-group', 'table-column', 'table-cell', 'table-caption', 'none'
00260         ));
00261         $this->info['visibility'] = new HTMLPurifier_AttrDef_Enum(array(
00262             'visible', 'hidden', 'collapse'
00263         ));
00264         $this->info['overflow'] = new HTMLPurifier_AttrDef_Enum(array('visible', 'hidden', 'auto', 'scroll'));
00265     }
00266 
00267     protected function doSetupTrusted($config) {
00268         $this->info['position'] = new HTMLPurifier_AttrDef_Enum(array(
00269             'static', 'relative', 'absolute', 'fixed'
00270         ));
00271         $this->info['top'] =
00272         $this->info['left'] =
00273         $this->info['right'] =
00274         $this->info['bottom'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00275             new HTMLPurifier_AttrDef_CSS_Length(),
00276             new HTMLPurifier_AttrDef_CSS_Percentage(),
00277             new HTMLPurifier_AttrDef_Enum(array('auto')),
00278         ));
00279         $this->info['z-index'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
00280             new HTMLPurifier_AttrDef_Integer(),
00281             new HTMLPurifier_AttrDef_Enum(array('auto')),
00282         ));
00283     }
00284 
00291     protected function setupConfigStuff($config) {
00292 
00293         // setup allowed elements
00294         $support = "(for information on implementing this, see the ".
00295                    "support forums) ";
00296         $allowed_properties = $config->get('CSS.AllowedProperties');
00297         if ($allowed_properties !== null) {
00298             foreach ($this->info as $name => $d) {
00299                 if(!isset($allowed_properties[$name])) unset($this->info[$name]);
00300                 unset($allowed_properties[$name]);
00301             }
00302             // emit errors
00303             foreach ($allowed_properties as $name => $d) {
00304                 // :TODO: Is this htmlspecialchars() call really necessary?
00305                 $name = htmlspecialchars($name);
00306                 trigger_error("Style attribute '$name' is not supported $support", E_USER_WARNING);
00307             }
00308         }
00309 
00310         $forbidden_properties = $config->get('CSS.ForbiddenProperties');
00311         if ($forbidden_properties !== null) {
00312             foreach ($this->info as $name => $d) {
00313                 if (isset($forbidden_properties[$name])) {
00314                     unset($this->info[$name]);
00315                 }
00316             }
00317         }
00318 
00319     }
00320 }
00321 
00322 // vim: et sw=4 sts=4