Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion library/HTMLPurifier/AttrDef/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
class HTMLPurifier_AttrDef_CSS extends HTMLPurifier_AttrDef
{

const CACHE_LIMIT = 1000;

/**
* Internal values cache for styles.
* @type array
*/
protected $cache = array();

/**
* Cached serial for the CSS config namespace.
* @type string|null
*/
protected $cacheSerial;

/**
* @param string $css
* @param HTMLPurifier_Config $config
Expand All @@ -23,6 +37,20 @@ class HTMLPurifier_AttrDef_CSS extends HTMLPurifier_AttrDef
public function validate($css, $config, $context)
{
$css = $this->parseCDATA($css);
if ($css === '') {
return false;
}

// Warning: This is a VERY simple cache, so we only keep it valid
// for the current CSS configuration namespace.
$serial = $config->getBatchSerial('CSS');
if ($this->cacheSerial !== $serial) {
$this->cache = array();
$this->cacheSerial = $serial;
}
if (array_key_exists($css, $this->cache)) {
return $this->cache[$css];
}

$definition = $config->getCSSDefinition();
$allow_duplicates = $config->get("CSS.AllowDuplicates");
Expand Down Expand Up @@ -131,7 +159,11 @@ public function validate($css, $config, $context)
$new_declarations .= "$prop:$value;";
}

return $new_declarations ? $new_declarations : false;
$result = $new_declarations ? $new_declarations : false;
if (count($this->cache) < self::CACHE_LIMIT) {
$this->cache[$css] = $result;
}
return $result;

}

Expand Down
Loading