Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / ChildDef / Required.php
blobc6f706e29a640a0ea029486a55b7ebda2b56f2f0
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]);
31 $this->elements = $elements;
32 $this->gen = new HTMLPurifier_Generator();
34 var $allow_empty = false;
35 var $type = 'required';
36 function validateChildren($tokens_of_children, $config, &$context) {
37 // if there are no tokens, delete parent node
38 if (empty($tokens_of_children)) return false;
40 // the new set of children
41 $result = array();
43 // current depth into the nest
44 $nesting = 0;
46 // whether or not we're deleting a node
47 $is_deleting = false;
49 // whether or not parsed character data is allowed
50 // this controls whether or not we silently drop a tag
51 // or generate escaped HTML from it
52 $pcdata_allowed = isset($this->elements['#PCDATA']);
54 // a little sanity check to make sure it's not ALL whitespace
55 $all_whitespace = true;
57 // some configuration
58 $escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
60 foreach ($tokens_of_children as $token) {
61 if (!empty($token->is_whitespace)) {
62 $result[] = $token;
63 continue;
65 $all_whitespace = false; // phew, we're not talking about whitespace
67 $is_child = ($nesting == 0);
69 if ($token->type == 'start') {
70 $nesting++;
71 } elseif ($token->type == 'end') {
72 $nesting--;
75 if ($is_child) {
76 $is_deleting = false;
77 if (!isset($this->elements[$token->name])) {
78 $is_deleting = true;
79 if ($pcdata_allowed && $token->type == 'text') {
80 $result[] = $token;
81 } elseif ($pcdata_allowed && $escape_invalid_children) {
82 $result[] = new HTMLPurifier_Token_Text(
83 $this->gen->generateFromToken($token, $config)
86 continue;
89 if (!$is_deleting || ($pcdata_allowed && $token->type == 'text')) {
90 $result[] = $token;
91 } elseif ($pcdata_allowed && $escape_invalid_children) {
92 $result[] =
93 new HTMLPurifier_Token_Text(
94 $this->gen->generateFromToken( $token, $config )
96 } else {
97 // drop silently
100 if (empty($result)) return false;
101 if ($all_whitespace) return false;
102 if ($tokens_of_children == $result) return true;
103 return $result;