2006-07-23 23:29:12 +00:00
|
|
|
<?php
|
|
|
|
|
2006-07-24 01:50:41 +00:00
|
|
|
require_once 'HTMLPurifier/Strategy.php';
|
2006-08-14 21:22:49 +00:00
|
|
|
require_once 'HTMLPurifier/HTMLDefinition.php';
|
2006-07-24 01:50:41 +00:00
|
|
|
require_once 'HTMLPurifier/Generator.php';
|
2006-07-23 23:29:12 +00:00
|
|
|
|
2007-06-23 18:50:41 +00:00
|
|
|
require_once 'HTMLPurifier/Injector/AutoParagraph.php';
|
|
|
|
|
2007-06-22 21:32:56 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define(
|
|
|
|
'Core', 'AutoParagraph', false, 'bool', '
|
|
|
|
<p>
|
|
|
|
This directive will cause HTML Purifier to automatically paragraph text
|
|
|
|
in the document fragment root based on two newlines and block tags.
|
|
|
|
This directive has been available since 2.0.1.
|
|
|
|
</p>
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
2006-08-20 21:59:41 +00:00
|
|
|
/**
|
|
|
|
* Takes tokens makes them well-formed (balance end tags, etc.)
|
|
|
|
*/
|
2006-07-23 23:29:12 +00:00
|
|
|
class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
|
|
|
|
{
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
function execute($tokens, $config, &$context) {
|
2007-06-23 17:11:05 +00:00
|
|
|
|
2006-08-31 20:33:07 +00:00
|
|
|
$definition = $config->getHTMLDefinition();
|
|
|
|
$generator = new HTMLPurifier_Generator();
|
2007-06-23 17:11:05 +00:00
|
|
|
|
2006-07-23 23:29:12 +00:00
|
|
|
$current_nesting = array();
|
2007-06-23 17:11:05 +00:00
|
|
|
$context->register('CurrentNesting', $current_nesting);
|
|
|
|
|
|
|
|
$tokens_index = null;
|
|
|
|
$context->register('InputIndex', $tokens_index);
|
|
|
|
$context->register('InputTokens', $tokens);
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
$context->register('OutputTokens', $result);
|
2007-06-22 21:32:56 +00:00
|
|
|
|
2006-08-15 23:58:18 +00:00
|
|
|
$escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags');
|
2007-06-22 21:32:56 +00:00
|
|
|
$auto_paragraph = $config->get('Core', 'AutoParagraph');
|
|
|
|
|
2007-06-24 02:17:34 +00:00
|
|
|
$injector = array();
|
|
|
|
$injector['AutoParagraph'] = new HTMLPurifier_Injector_AutoParagraph();
|
|
|
|
$injector_skip['AutoParagraph'] = 0;
|
|
|
|
$injector_disabled['AutoParagraph'] = false;
|
2007-06-23 17:44:28 +00:00
|
|
|
|
2007-06-24 02:17:34 +00:00
|
|
|
$context->register('InjectorSkip', $injector_skip);
|
2007-06-23 18:50:41 +00:00
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
for ($tokens_index = 0; isset($tokens[$tokens_index]); $tokens_index++) {
|
|
|
|
|
|
|
|
// if all goes well, this token will be passed through unharmed
|
|
|
|
$token = $tokens[$tokens_index];
|
|
|
|
|
2007-06-23 17:44:28 +00:00
|
|
|
// this will be more complicated
|
2007-06-24 02:17:34 +00:00
|
|
|
if ($injector_skip['AutoParagraph'] > 0) {
|
|
|
|
$injector_skip['AutoParagraph']--;
|
|
|
|
$injector_disabled['AutoParagraph'] = true;
|
2007-06-23 17:44:28 +00:00
|
|
|
} else {
|
2007-06-24 02:17:34 +00:00
|
|
|
$injector_disabled['AutoParagraph'] = false;
|
2007-06-23 17:44:28 +00:00
|
|
|
}
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// quick-check: if it's not a tag, no need to process
|
2006-07-23 23:29:12 +00:00
|
|
|
if (empty( $token->is_tag )) {
|
2007-06-23 17:44:28 +00:00
|
|
|
|
|
|
|
if ($token->type === 'text') {
|
2007-06-24 02:17:34 +00:00
|
|
|
if ($auto_paragraph && !$injector_disabled['AutoParagraph']) {
|
|
|
|
$injector['AutoParagraph']->handleText($token, $config, $context);
|
2007-06-23 17:44:28 +00:00
|
|
|
}
|
2007-06-22 21:32:56 +00:00
|
|
|
}
|
2007-06-23 17:44:28 +00:00
|
|
|
|
|
|
|
$this->processToken($token, $config, $context);
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-07-30 19:11:18 +00:00
|
|
|
|
2006-08-31 20:33:07 +00:00
|
|
|
$info = $definition->info[$token->name]->child;
|
2006-07-23 23:29:12 +00:00
|
|
|
|
|
|
|
// test if it claims to be a start tag but is empty
|
2007-06-23 17:11:05 +00:00
|
|
|
if ($info->type == 'empty' && $token->type == 'start') {
|
|
|
|
$result[] = new HTMLPurifier_Token_Empty($token->name, $token->attr);
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// test if it claims to be empty but really is a start tag
|
2007-06-23 17:11:05 +00:00
|
|
|
if ($info->type != 'empty' && $token->type == 'empty' ) {
|
|
|
|
$result[] = new HTMLPurifier_Token_Start($token->name, $token->attr);
|
2006-07-23 23:29:12 +00:00
|
|
|
$result[] = new HTMLPurifier_Token_End($token->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// automatically insert empty tags
|
|
|
|
if ($token->type == 'empty') {
|
|
|
|
$result[] = $token;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// start tags have precedence, so they get passed through...
|
2006-07-23 23:29:12 +00:00
|
|
|
if ($token->type == 'start') {
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// ...unless they also have to close their parent
|
2006-07-23 23:29:12 +00:00
|
|
|
if (!empty($current_nesting)) {
|
|
|
|
|
2006-07-31 00:15:01 +00:00
|
|
|
$parent = array_pop($current_nesting);
|
2007-06-23 17:11:05 +00:00
|
|
|
$parent_info = $definition->info[$parent->name];
|
2006-07-30 22:57:54 +00:00
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// this can be replaced with a more general algorithm:
|
|
|
|
// if the token is not allowed by the parent, auto-close
|
|
|
|
// the parent
|
2007-06-23 20:52:57 +00:00
|
|
|
if (!isset($parent_info->child->elements[$token->name])) {
|
|
|
|
// close the parent, then append the token
|
2007-06-23 17:11:05 +00:00
|
|
|
$result[] = new HTMLPurifier_Token_End($parent->name);
|
2006-07-23 23:29:12 +00:00
|
|
|
$result[] = $token;
|
|
|
|
$current_nesting[] = $token;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-07-31 00:15:01 +00:00
|
|
|
$current_nesting[] = $parent; // undo the pop
|
2006-07-23 23:29:12 +00:00
|
|
|
}
|
|
|
|
|
2007-06-24 02:17:34 +00:00
|
|
|
if ($auto_paragraph && !$injector_disabled['AutoParagraph']) {
|
|
|
|
$injector['AutoParagraph']->handleStart($token, $config, $context);
|
2007-06-23 17:44:28 +00:00
|
|
|
}
|
2007-06-22 21:32:56 +00:00
|
|
|
|
2007-06-23 17:44:28 +00:00
|
|
|
$this->processToken($token, $config, $context);
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// sanity check: we should be dealing with a closing tag
|
2006-07-23 23:29:12 +00:00
|
|
|
if ($token->type != 'end') continue;
|
|
|
|
|
|
|
|
// make sure that we have something open
|
|
|
|
if (empty($current_nesting)) {
|
2006-08-15 23:58:18 +00:00
|
|
|
if ($escape_invalid_tags) {
|
|
|
|
$result[] = new HTMLPurifier_Token_Text(
|
2006-10-01 20:47:07 +00:00
|
|
|
$generator->generateFromToken($token, $config, $context)
|
2006-08-15 23:58:18 +00:00
|
|
|
);
|
|
|
|
}
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first, check for the simplest case: everything closes neatly
|
|
|
|
$current_parent = array_pop($current_nesting);
|
|
|
|
if ($current_parent->name == $token->name) {
|
|
|
|
$result[] = $token;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// okay, so we're trying to close the wrong tag
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// undo the pop previous pop
|
|
|
|
$current_nesting[] = $current_parent;
|
|
|
|
|
|
|
|
// scroll back the entire nest, trying to find our tag.
|
|
|
|
// (feature could be to specify how far you'd like to go)
|
2006-07-23 23:29:12 +00:00
|
|
|
$size = count($current_nesting);
|
|
|
|
// -2 because -1 is the last element, but we already checked that
|
|
|
|
$skipped_tags = false;
|
|
|
|
for ($i = $size - 2; $i >= 0; $i--) {
|
|
|
|
if ($current_nesting[$i]->name == $token->name) {
|
|
|
|
// current nesting is modified
|
|
|
|
$skipped_tags = array_splice($current_nesting, $i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// we still didn't find the tag, so remove
|
2006-07-23 23:29:12 +00:00
|
|
|
if ($skipped_tags === false) {
|
2006-08-15 23:58:18 +00:00
|
|
|
if ($escape_invalid_tags) {
|
|
|
|
$result[] = new HTMLPurifier_Token_Text(
|
2006-10-01 20:47:07 +00:00
|
|
|
$generator->generateFromToken($token, $config, $context)
|
2006-08-15 23:58:18 +00:00
|
|
|
);
|
|
|
|
}
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// okay, we found it, close all the skipped tags
|
|
|
|
// note that skipped tags contains the element we need closed
|
|
|
|
$size = count($skipped_tags);
|
|
|
|
for ($i = $size - 1; $i >= 0; $i--) {
|
|
|
|
$result[] = new HTMLPurifier_Token_End($skipped_tags[$i]->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're at the end now, fix all still unclosed tags
|
2007-06-23 17:44:28 +00:00
|
|
|
// not using processToken() because at this point we don't
|
|
|
|
// care about current nesting
|
2006-07-23 23:29:12 +00:00
|
|
|
if (!empty($current_nesting)) {
|
|
|
|
$size = count($current_nesting);
|
|
|
|
for ($i = $size - 1; $i >= 0; $i--) {
|
|
|
|
$result[] =
|
|
|
|
new HTMLPurifier_Token_End($current_nesting[$i]->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
$context->destroy('CurrentNesting');
|
|
|
|
$context->destroy('InputTokens');
|
|
|
|
$context->destroy('InputIndex');
|
|
|
|
$context->destroy('OutputTokens');
|
|
|
|
|
2006-07-23 23:29:12 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2007-06-23 17:44:28 +00:00
|
|
|
function processToken($token, $config, &$context) {
|
|
|
|
if (is_array($token)) {
|
|
|
|
// the original token was overloaded by a formatter, time
|
|
|
|
// to some fancy acrobatics
|
|
|
|
|
|
|
|
$tokens =& $context->get('InputTokens');
|
|
|
|
$tokens_index =& $context->get('InputIndex');
|
|
|
|
// $tokens_index is decremented so that the entire set gets
|
|
|
|
// re-processed
|
|
|
|
array_splice($tokens, $tokens_index--, 1, $token);
|
|
|
|
|
|
|
|
// this will be a bit more complicated when we add more formatters
|
|
|
|
// we need to prevent the same formatter from running twice on it
|
2007-06-24 02:17:34 +00:00
|
|
|
$injector_skip =& $context->get('InjectorSkip');
|
|
|
|
$injector_skip['AutoParagraph'] = count($token);
|
2007-06-23 17:44:28 +00:00
|
|
|
|
|
|
|
} elseif ($token) {
|
|
|
|
// regular case
|
|
|
|
$result =& $context->get('OutputTokens');
|
|
|
|
$current_nesting =& $context->get('CurrentNesting');
|
|
|
|
$result[] = $token;
|
|
|
|
if ($token->type == 'start') {
|
|
|
|
$current_nesting[] = $token;
|
|
|
|
} elseif ($token->type == 'end') {
|
|
|
|
// theoretical: this isn't used because performing
|
2007-06-23 19:39:03 +00:00
|
|
|
// the calculations inline is more efficient, and
|
|
|
|
// end tokens currently do not cause a handler invocation
|
2007-06-23 17:44:28 +00:00
|
|
|
array_pop($current_nesting);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-23 23:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|