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.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @type array
|
2006-11-22 18:55:15 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $elements = array();
|
2013-07-16 11:56:14 +00:00
|
|
|
|
2008-12-03 01:13:47 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the last passed node was all whitespace.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @type bool
|
2008-12-03 01:13:47 +00:00
|
|
|
*/
|
|
|
|
protected $whitespace = false;
|
2013-07-16 11:56:14 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param array|string $elements List of allowed element names (lowercase).
|
2006-11-22 18:55:15 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +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;
|
2013-07-16 11:56:14 +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;
|
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type bool
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $allow_empty = false;
|
2013-07-16 11:56:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type string
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $type = 'required';
|
2013-07-16 11:56:14 +00:00
|
|
|
|
|
|
|
/**
|
2013-10-21 05:18:59 +00:00
|
|
|
* @param array $children
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Config $config
|
|
|
|
* @param HTMLPurifier_Context $context
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-10-21 05:18:59 +00:00
|
|
|
public function validateChildren($children, $config, $context)
|
2013-07-16 11:56:14 +00:00
|
|
|
{
|
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
|
2013-10-21 05:18:59 +00:00
|
|
|
if (empty($children)) {
|
2013-07-16 11:56:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
// the new set of children
|
|
|
|
$result = array();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
// 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']);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-11-22 18:55:15 +00:00
|
|
|
// a little sanity check to make sure it's not ALL whitespace
|
|
|
|
$all_whitespace = true;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-10-21 05:18:59 +00:00
|
|
|
$stack = array_reverse($children);
|
|
|
|
while (!empty($stack)) {
|
|
|
|
$node = array_pop($stack);
|
|
|
|
if (!empty($node->is_whitespace)) {
|
|
|
|
$result[] = $node;
|
2006-11-22 18:55:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$all_whitespace = false; // phew, we're not talking about whitespace
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-10-21 05:18:59 +00:00
|
|
|
if (!isset($this->elements[$node->name])) {
|
|
|
|
// special case text
|
|
|
|
// XXX One of these ought to be redundant or something
|
|
|
|
if ($pcdata_allowed && $node instanceof HTMLPurifier_Node_Text) {
|
|
|
|
$result[] = $node;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// spill the child contents in
|
|
|
|
// ToDo: Make configurable
|
|
|
|
if ($node instanceof HTMLPurifier_Node_Element) {
|
|
|
|
for ($i = count($node->children) - 1; $i >= 0; $i--) {
|
|
|
|
$stack[] = $node->children[$i];
|
2006-11-22 18:55:15 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-21 05:18:59 +00:00
|
|
|
continue;
|
2006-11-22 18:55:15 +00:00
|
|
|
}
|
2013-10-21 05:18:59 +00:00
|
|
|
$result[] = $node;
|
2006-11-22 18:55:15 +00:00
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
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
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|