mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-30 19:00:10 +02:00
Added ValidateAttributes strategy and associated unit tests. Amended Generator with some sanity checks. Made Definition include all necessary definitions. Note the two elements (bdo and br) that only use coreattrs.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@132 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
52
library/HTMLPurifier/Strategy/ValidateAttributes.php
Normal file
52
library/HTMLPurifier/Strategy/ValidateAttributes.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Strategy.php';
|
||||
require_once 'HTMLPurifier/Definition.php';
|
||||
require_once 'HTMLPurifier/IDAccumulator.php';
|
||||
|
||||
class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
|
||||
{
|
||||
|
||||
var $definition;
|
||||
|
||||
function HTMLPurifier_Strategy_ValidateAttributes() {
|
||||
$this->definition = HTMLPurifier_Definition::instance();
|
||||
}
|
||||
|
||||
function execute($tokens) {
|
||||
$accumulator = new HTMLPurifier_IDAccumulator();
|
||||
$d_defs = $this->definition->info['attr']['*'];
|
||||
foreach ($tokens as $key => $token) {
|
||||
if ($token->type !== 'start' && $token->type !== 'end') continue;
|
||||
$name = $token->name;
|
||||
$attr = $token->attributes;
|
||||
$defs = isset($this->definition->info['attr'][$name]) ?
|
||||
$this->definition->attr[$name] : array();
|
||||
$changed = false;
|
||||
foreach ($attr as $attr_key => $value) {
|
||||
if ( isset($defs[$attr_key]) ) {
|
||||
if (!$defs[$attr_key]) {
|
||||
$result = false;
|
||||
} else {
|
||||
$result = $defs[$attr_key]->validate($value, $accumulator);
|
||||
}
|
||||
} elseif ( isset($d_defs[$attr_key]) ) {
|
||||
$result = $d_defs[$attr_key]->validate($value, $accumulator);
|
||||
} else {
|
||||
$result = false;
|
||||
}
|
||||
if (!$result) {
|
||||
$changed = true;
|
||||
unset($attr[$attr_key]);
|
||||
}
|
||||
}
|
||||
if ($changed) {
|
||||
$tokens[$key]->attributes = $attr;
|
||||
}
|
||||
}
|
||||
return $tokens;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user