friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/AttrDef/CSS/Background.php

112 lines
3.1 KiB
PHP
Raw Normal View History

2010-09-09 05:14:17 +02:00
<?php
/**
* Validates shorthand CSS property background.
* @warning Does not support url tokens that have internal spaces.
*/
class HTMLPurifier_AttrDef_CSS_Background extends HTMLPurifier_AttrDef
{
/**
* Local copy of component validators.
2016-02-09 11:06:17 +01:00
* @type HTMLPurifier_AttrDef[]
2010-09-09 05:14:17 +02:00
* @note See HTMLPurifier_AttrDef_Font::$info for a similar impl.
*/
protected $info;
2016-02-09 11:06:17 +01:00
/**
* @param HTMLPurifier_Config $config
*/
public function __construct($config)
{
2010-09-09 05:14:17 +02:00
$def = $config->getCSSDefinition();
$this->info['background-color'] = $def->info['background-color'];
$this->info['background-image'] = $def->info['background-image'];
$this->info['background-repeat'] = $def->info['background-repeat'];
$this->info['background-attachment'] = $def->info['background-attachment'];
$this->info['background-position'] = $def->info['background-position'];
}
2016-02-09 11:06:17 +01:00
/**
* @param string $string
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool|string
*/
public function validate($string, $config, $context)
{
2010-09-09 05:14:17 +02:00
// regular pre-processing
$string = $this->parseCDATA($string);
2016-02-09 11:06:17 +01:00
if ($string === '') {
return false;
}
2010-09-09 05:14:17 +02:00
// munge rgb() decl if necessary
$string = $this->mungeRgb($string);
// assumes URI doesn't have spaces in it
2016-02-09 11:06:17 +01:00
$bits = explode(' ', $string); // bits to process
2010-09-09 05:14:17 +02:00
$caught = array();
2016-02-09 11:06:17 +01:00
$caught['color'] = false;
$caught['image'] = false;
$caught['repeat'] = false;
2010-09-09 05:14:17 +02:00
$caught['attachment'] = false;
$caught['position'] = false;
$i = 0; // number of catches
foreach ($bits as $bit) {
2016-02-09 11:06:17 +01:00
if ($bit === '') {
continue;
}
2010-09-09 05:14:17 +02:00
foreach ($caught as $key => $status) {
if ($key != 'position') {
2016-02-09 11:06:17 +01:00
if ($status !== false) {
continue;
}
2010-09-09 05:14:17 +02:00
$r = $this->info['background-' . $key]->validate($bit, $config, $context);
} else {
$r = $bit;
}
2016-02-09 11:06:17 +01:00
if ($r === false) {
continue;
}
2010-09-09 05:14:17 +02:00
if ($key == 'position') {
2016-02-09 11:06:17 +01:00
if ($caught[$key] === false) {
$caught[$key] = '';
}
2010-09-09 05:14:17 +02:00
$caught[$key] .= $r . ' ';
} else {
$caught[$key] = $r;
}
$i++;
break;
}
}
2016-02-09 11:06:17 +01:00
if (!$i) {
return false;
}
2010-09-09 05:14:17 +02:00
if ($caught['position'] !== false) {
$caught['position'] = $this->info['background-position']->
validate($caught['position'], $config, $context);
}
$ret = array();
foreach ($caught as $value) {
2016-02-09 11:06:17 +01:00
if ($value === false) {
continue;
}
2010-09-09 05:14:17 +02:00
$ret[] = $value;
}
2016-02-09 11:06:17 +01:00
if (empty($ret)) {
return false;
}
2010-09-09 05:14:17 +02:00
return implode(' ', $ret);
}
}
// vim: et sw=4 sts=4