3 require_once 'HTMLPurifier/Strategy.php';
4 require_once 'HTMLPurifier/HTMLDefinition.php';
5 require_once 'HTMLPurifier/Generator.php';
8 * Takes tokens makes them well-formed (balance end tags, etc.)
10 class HTMLPurifier_Strategy_MakeWellFormed
extends HTMLPurifier_Strategy
13 function execute($tokens, $config, &$context) {
14 $definition = $config->getHTMLDefinition();
15 $generator = new HTMLPurifier_Generator();
17 $current_nesting = array();
18 $escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags');
19 foreach ($tokens as $token) {
20 if (empty( $token->is_tag
)) {
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
,
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
,
43 $result[] = new HTMLPurifier_Token_End($token->name
);
48 // automatically insert empty tags
49 if ($token->type
== 'empty') {
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);
68 $current_nesting[] = $token;
72 $current_nesting[] = $parent; // undo the pop
76 $current_nesting[] = $token;
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)
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
) {
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);
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)
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
);
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--) {
149 new HTMLPurifier_Token_End($current_nesting[$i]->name
);