Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / Strategy / MakeWellFormed.php
blob84580d3d34364707cd83444333b8fe18129e0bc0
1 <?php
3 require_once 'HTMLPurifier/Strategy.php';
4 require_once 'HTMLPurifier/HTMLDefinition.php';
5 require_once 'HTMLPurifier/Generator.php';
7 /**
8 * Takes tokens makes them well-formed (balance end tags, etc.)
9 */
10 class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
13 function execute($tokens, $config, &$context) {
14 $definition = $config->getHTMLDefinition();
15 $generator = new HTMLPurifier_Generator();
16 $result = array();
17 $current_nesting = array();
18 $escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags');
19 foreach ($tokens as $token) {
20 if (empty( $token->is_tag )) {
21 $result[] = $token;
22 continue;
25 // DEFINITION CALL
26 $info = $definition->info[$token->name]->child;
28 // test if it claims to be a start tag but is empty
29 if ($info->type == 'empty' &&
30 $token->type == 'start' ) {
32 $result[] = new HTMLPurifier_Token_Empty($token->name,
33 $token->attr);
34 continue;
37 // test if it claims to be empty but really is a start tag
38 if ($info->type != 'empty' &&
39 $token->type == 'empty' ) {
41 $result[] = new HTMLPurifier_Token_Start($token->name,
42 $token->attr);
43 $result[] = new HTMLPurifier_Token_End($token->name);
45 continue;
48 // automatically insert empty tags
49 if ($token->type == 'empty') {
50 $result[] = $token;
51 continue;
54 // we give start tags precedence, so automatically accept unless...
55 // it's one of those special cases
56 if ($token->type == 'start') {
58 // if there's a parent, check for special case
59 if (!empty($current_nesting)) {
61 $parent = array_pop($current_nesting);
62 $parent_name = $parent->name;
63 $parent_info = $definition->info[$parent_name];
65 if (isset($parent_info->auto_close[$token->name])) {
66 $result[] = new HTMLPurifier_Token_End($parent_name);
67 $result[] = $token;
68 $current_nesting[] = $token;
69 continue;
72 $current_nesting[] = $parent; // undo the pop
75 $result[] = $token;
76 $current_nesting[] = $token;
77 continue;
80 // sanity check
81 if ($token->type != 'end') continue;
83 // okay, we're dealing with a closing tag
85 // make sure that we have something open
86 if (empty($current_nesting)) {
87 if ($escape_invalid_tags) {
88 $result[] = new HTMLPurifier_Token_Text(
89 $generator->generateFromToken($token, $config, $context)
92 continue;
95 // first, check for the simplest case: everything closes neatly
97 // current_nesting is modified
98 $current_parent = array_pop($current_nesting);
99 if ($current_parent->name == $token->name) {
100 $result[] = $token;
101 continue;
104 // undo the array_pop
105 $current_nesting[] = $current_parent;
107 // okay, so we're trying to close the wrong tag
109 // scroll back the entire nest, trying to find our tag
110 // feature could be to specify how far you'd like to go
111 $size = count($current_nesting);
112 // -2 because -1 is the last element, but we already checked that
113 $skipped_tags = false;
114 for ($i = $size - 2; $i >= 0; $i--) {
115 if ($current_nesting[$i]->name == $token->name) {
116 // current nesting is modified
117 $skipped_tags = array_splice($current_nesting, $i);
118 break;
122 // we still didn't find the tag, so translate to text
123 if ($skipped_tags === false) {
124 if ($escape_invalid_tags) {
125 $result[] = new HTMLPurifier_Token_Text(
126 $generator->generateFromToken($token, $config, $context)
129 continue;
132 // okay, we found it, close all the skipped tags
133 // note that skipped tags contains the element we need closed
134 $size = count($skipped_tags);
135 for ($i = $size - 1; $i >= 0; $i--) {
136 $result[] = new HTMLPurifier_Token_End($skipped_tags[$i]->name);
139 // done!
143 // we're at the end now, fix all still unclosed tags
145 if (!empty($current_nesting)) {
146 $size = count($current_nesting);
147 for ($i = $size - 1; $i >= 0; $i--) {
148 $result[] =
149 new HTMLPurifier_Token_End($current_nesting[$i]->name);
153 return $result;