Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / ChildDef / Required.php
blobf4d908b05d8d2b78ac886c89cccf2c1ad84ebe0a
1 <?php
3 require_once 'HTMLPurifier/ChildDef.php';
5 /**
6 * Definition that allows a set of elements, but disallows empty children.
7 */
8 class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
10 /**
11 * Lookup table of allowed elements.
12 * @public
14 var $elements = array();
15 /**
16 * @param $elements List of allowed element names (lowercase).
18 function HTMLPurifier_ChildDef_Required($elements) {
19 if (is_string($elements)) {
20 $elements = str_replace(' ', '', $elements);
21 $elements = explode('|', $elements);
23 $keys = array_keys($elements);
24 if ($keys == array_keys($keys)) {
25 $elements = array_flip($elements);
26 foreach ($elements as $i => $x) {
27 $elements[$i] = true;
28 if (empty($i)) unset($elements[$i]); // remove blank
31 $this->elements = $elements;
33 var $allow_empty = false;
34 var $type = 'required';
35 function validateChildren($tokens_of_children, $config, &$context) {
36 // if there are no tokens, delete parent node
37 if (empty($tokens_of_children)) return false;
39 // the new set of children
40 $result = array();
42 // current depth into the nest
43 $nesting = 0;
45 // whether or not we're deleting a node
46 $is_deleting = false;
48 // whether or not parsed character data is allowed
49 // this controls whether or not we silently drop a tag
50 // or generate escaped HTML from it
51 $pcdata_allowed = isset($this->elements['#PCDATA']);
53 // a little sanity check to make sure it's not ALL whitespace
54 $all_whitespace = true;
56 // some configuration
57 $escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
59 // generator
60 static $gen = null;
61 if ($gen === null) {
62 $gen = new HTMLPurifier_Generator();
65 foreach ($tokens_of_children as $token) {
66 if (!empty($token->is_whitespace)) {
67 $result[] = $token;
68 continue;
70 $all_whitespace = false; // phew, we're not talking about whitespace
72 $is_child = ($nesting == 0);
74 if ($token->type == 'start') {
75 $nesting++;
76 } elseif ($token->type == 'end') {
77 $nesting--;
80 if ($is_child) {
81 $is_deleting = false;
82 if (!isset($this->elements[$token->name])) {
83 $is_deleting = true;
84 if ($pcdata_allowed && $token->type == 'text') {
85 $result[] = $token;
86 } elseif ($pcdata_allowed && $escape_invalid_children) {
87 $result[] = new HTMLPurifier_Token_Text(
88 $gen->generateFromToken($token, $config)
91 continue;
94 if (!$is_deleting || ($pcdata_allowed && $token->type == 'text')) {
95 $result[] = $token;
96 } elseif ($pcdata_allowed && $escape_invalid_children) {
97 $result[] =
98 new HTMLPurifier_Token_Text(
99 $gen->generateFromToken( $token, $config )
101 } else {
102 // drop silently
105 if (empty($result)) return false;
106 if ($all_whitespace) return false;
107 if ($tokens_of_children == $result) return true;
108 return $result;