2006-11-22 18:55:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Definition that allows a set of elements, but disallows empty children.
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Lookup table of allowed elements.
|
2006-11-25 05:05:32 +00:00
|
|
|
* @public
|
2006-11-22 18:55:15 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $elements = array();
|
2008-12-03 01:13:47 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the last passed node was all whitespace.
|
|
|
|
*/
|
|
|
|
protected $whitespace = false;
|
2006-11-22 18:55:15 +00:00
|
|
|
/**
|
|
|
|
* @param $elements List of allowed element names (lowercase).
|
|
|
|
*/
|
2007-11-29 04:29:51 +00:00
|
|
|
public function __construct($elements) {
|
2006-11-22 18:55:15 +00:00
|
|
|
if (is_string($elements)) {
|
|
|
|
$elements = str_replace(' ', '', $elements);
|
|
|
|
$elements = explode('|', $elements);
|
|
|
|
}
|
2007-02-04 23:10:10 +00:00
|
|
|
$keys = array_keys($elements);
|
|
|
|
if ($keys == array_keys($keys)) {
|
|
|
|
$elements = array_flip($elements);
|
|
|
|
foreach ($elements as $i => $x) {
|
|
|
|
$elements[$i] = true;
|
2007-06-23 20:52:57 +00:00
|
|
|
if (empty($i)) unset($elements[$i]); // remove blank
|
2007-02-04 23:10:10 +00:00
|
|
|
}
|
2006-11-23 03:23:35 +00:00
|
|
|
}
|
2006-11-22 18:55:15 +00:00
|
|
|
$this->elements = $elements;
|
|
|
|
}
|
2007-11-25 02:24:39 +00:00
|
|
|
public $allow_empty = false;
|
|
|
|
public $type = 'required';
|
2008-01-05 00:10:43 +00:00
|
|
|
public function validateChildren($tokens_of_children, $config, $context) {
|
2008-12-03 01:13:47 +00:00
|
|
|
// Flag for subclasses
|
|
|
|
$this->whitespace = false;
|
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
// if there are no tokens, delete parent node
|
|
|
|
if (empty($tokens_of_children)) return false;
|
|
|
|
|
|
|
|
// the new set of children
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
// current depth into the nest
|
|
|
|
$nesting = 0;
|
|
|
|
|
|
|
|
// whether or not we're deleting a node
|
|
|
|
$is_deleting = false;
|
|
|
|
|
|
|
|
// whether or not parsed character data is allowed
|
|
|
|
// this controls whether or not we silently drop a tag
|
|
|
|
// or generate escaped HTML from it
|
|
|
|
$pcdata_allowed = isset($this->elements['#PCDATA']);
|
|
|
|
|
|
|
|
// a little sanity check to make sure it's not ALL whitespace
|
|
|
|
$all_whitespace = true;
|
|
|
|
|
|
|
|
// some configuration
|
|
|
|
$escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
|
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
// generator
|
2008-05-26 04:05:48 +00:00
|
|
|
$gen = new HTMLPurifier_Generator($config, $context);
|
2007-05-15 02:33:19 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
foreach ($tokens_of_children as $token) {
|
|
|
|
if (!empty($token->is_whitespace)) {
|
|
|
|
$result[] = $token;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$all_whitespace = false; // phew, we're not talking about whitespace
|
|
|
|
|
|
|
|
$is_child = ($nesting == 0);
|
|
|
|
|
2008-01-19 20:23:01 +00:00
|
|
|
if ($token instanceof HTMLPurifier_Token_Start) {
|
2006-11-22 18:55:15 +00:00
|
|
|
$nesting++;
|
2008-01-19 20:23:01 +00:00
|
|
|
} elseif ($token instanceof HTMLPurifier_Token_End) {
|
2006-11-22 18:55:15 +00:00
|
|
|
$nesting--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_child) {
|
|
|
|
$is_deleting = false;
|
|
|
|
if (!isset($this->elements[$token->name])) {
|
|
|
|
$is_deleting = true;
|
2008-01-19 20:23:01 +00:00
|
|
|
if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text) {
|
2006-11-22 18:55:15 +00:00
|
|
|
$result[] = $token;
|
|
|
|
} elseif ($pcdata_allowed && $escape_invalid_children) {
|
|
|
|
$result[] = new HTMLPurifier_Token_Text(
|
2008-05-26 04:05:48 +00:00
|
|
|
$gen->generateFromToken($token)
|
2006-11-22 18:55:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2008-01-19 20:23:01 +00:00
|
|
|
if (!$is_deleting || ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text)) {
|
2006-11-22 18:55:15 +00:00
|
|
|
$result[] = $token;
|
|
|
|
} elseif ($pcdata_allowed && $escape_invalid_children) {
|
|
|
|
$result[] =
|
|
|
|
new HTMLPurifier_Token_Text(
|
2008-05-26 04:05:48 +00:00
|
|
|
$gen->generateFromToken($token)
|
2006-11-22 18:55:15 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// drop silently
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($result)) return false;
|
2008-12-03 01:13:47 +00:00
|
|
|
if ($all_whitespace) {
|
|
|
|
$this->whitespace = true;
|
|
|
|
return false;
|
|
|
|
}
|
2006-11-22 18:55:15 +00:00
|
|
|
if ($tokens_of_children == $result) return true;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|