mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-03-23 14:27:02 +00:00
Refactor Injector not to edit $result directly.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1212 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
ae83bebc98
commit
5d0a992579
@ -10,27 +10,21 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
|||||||
{
|
{
|
||||||
|
|
||||||
function handleText(&$token, $config, &$context) {
|
function handleText(&$token, $config, &$context) {
|
||||||
$dnl = PHP_EOL . PHP_EOL; // double-newline
|
|
||||||
$current_nesting =& $context->get('CurrentNesting');
|
$current_nesting =& $context->get('CurrentNesting');
|
||||||
// paragraphing is on
|
$text = $token->data;
|
||||||
|
// $token is the focus: if processing is needed, it gets
|
||||||
|
// turned into an array of tokens that will replace the
|
||||||
|
// original token
|
||||||
if (empty($current_nesting)) {
|
if (empty($current_nesting)) {
|
||||||
// we're in root node, great time to start a paragraph
|
// we're in root node, great time to start a paragraph
|
||||||
// since we're also dealing with a text node
|
// since we're also dealing with a text node
|
||||||
$result =& $context->get('OutputTokens');
|
$token = array(new HTMLPurifier_Token_Start('p'));
|
||||||
$result[] = new HTMLPurifier_Token_Start('p');
|
$this->_splitText($text, $token, $config, $context);
|
||||||
$current_nesting[] = new HTMLPurifier_Token_Start('p');
|
} elseif ($current_nesting[count($current_nesting)-1]->name == 'p') {
|
||||||
$this->_splitText($token, $config, $context);
|
// we're not in root node but we're in a paragraph, so don't
|
||||||
} else {
|
// add a paragraph start tag but still perform processing
|
||||||
// we're not in root node, so let's see whether or not
|
$token = array();
|
||||||
// we're in a paragraph
|
$this->_splitText($text, $token, $config, $context);
|
||||||
|
|
||||||
// losslessly access the parent element
|
|
||||||
$parent = array_pop($current_nesting);
|
|
||||||
$current_nesting[] = $parent;
|
|
||||||
|
|
||||||
if ($parent->name === 'p') {
|
|
||||||
$this->_splitText($token, $config, $context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,26 +35,25 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
|||||||
if (!empty($current_nesting)) return;
|
if (!empty($current_nesting)) return;
|
||||||
|
|
||||||
// check if the start tag counts as a "block" element
|
// check if the start tag counts as a "block" element
|
||||||
$definition = $config->getHTMLDefinition();
|
if (!$this->_isInline($token, $config)) return;
|
||||||
if (isset($definition->info['p']->auto_close[$token->name])) return;
|
|
||||||
|
|
||||||
// append a paragraph tag before the token
|
// append a paragraph tag before the token
|
||||||
$token = array(new HTMLPurifier_Token_Start('p'), $token);
|
$token = array(new HTMLPurifier_Token_Start('p'), $token);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sub-function for auto-paragraphing that takes a token and splits it
|
* Splits up a text in paragraph tokens and appends them
|
||||||
* up into paragraphs unconditionally. Requires that a paragraph was
|
* to the result stream that will replace the original
|
||||||
* already started
|
* @param $data String text data that will be processed
|
||||||
|
* into paragraphs
|
||||||
|
* @param $result Reference to array of tokens that the
|
||||||
|
* tags will be appended onto
|
||||||
|
* @param $config Instance of HTMLPurifier_Config
|
||||||
|
* @param $context Instance of HTMLPurifier_Context
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function _splitText(&$token, $config, &$context) {
|
function _splitText($data, &$result, $config, &$context) {
|
||||||
$dnl = PHP_EOL . PHP_EOL; // double-newline
|
$raw_paragraphs = explode(PHP_EOL . PHP_EOL, $data);
|
||||||
$definition = $config->getHTMLDefinition();
|
|
||||||
$current_nesting =& $context->get('CurrentNesting');
|
|
||||||
|
|
||||||
$raw_paragraphs = explode($dnl, $token->data);
|
|
||||||
|
|
||||||
$token = false; // token has been completely dismantled
|
|
||||||
|
|
||||||
// remove empty paragraphs
|
// remove empty paragraphs
|
||||||
$paragraphs = array();
|
$paragraphs = array();
|
||||||
@ -68,67 +61,62 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
|||||||
if (trim($par) !== '') $paragraphs[] = $par;
|
if (trim($par) !== '') $paragraphs[] = $par;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result =& $context->get('OutputTokens');
|
// check if there are no "real" paragraphs to be processed
|
||||||
|
|
||||||
if (empty($paragraphs) && count($raw_paragraphs) > 1) {
|
if (empty($paragraphs) && count($raw_paragraphs) > 1) {
|
||||||
$result[] = new HTMLPurifier_Token_End('p');
|
$result[] = new HTMLPurifier_Token_End('p');
|
||||||
array_pop($current_nesting);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($paragraphs as $data) {
|
// append the paragraphs onto the result
|
||||||
$result[] = new HTMLPurifier_Token_Text($data);
|
foreach ($paragraphs as $par) {
|
||||||
|
$result[] = new HTMLPurifier_Token_Text($par);
|
||||||
$result[] = new HTMLPurifier_Token_End('p');
|
$result[] = new HTMLPurifier_Token_End('p');
|
||||||
$result[] = new HTMLPurifier_Token_Start('p');
|
$result[] = new HTMLPurifier_Token_Start('p');
|
||||||
}
|
}
|
||||||
array_pop($result); // remove trailing start token
|
array_pop($result); // remove trailing start token
|
||||||
|
|
||||||
// check the outside to determine whether or not end
|
// check the outside to determine whether or not the
|
||||||
// paragraph tag is needed (it's already there)
|
// end paragraph tag should be removed
|
||||||
$end_paragraph = $this->_needsEndTag(
|
if ($this->_removeParagraphEnd($config, $context)) {
|
||||||
$context->get('InputTokens'),
|
|
||||||
$context->get('InputIndex'),
|
|
||||||
$definition
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($end_paragraph) {
|
|
||||||
// things are good as they stand, remove top-level parent
|
|
||||||
// that we deferred
|
|
||||||
array_pop($current_nesting);
|
|
||||||
} else {
|
|
||||||
// remove the ending tag, no nesting modifications necessary
|
|
||||||
array_pop($result);
|
array_pop($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines if up-coming code requires an end-paragraph tag,
|
* Returns boolean whether or not to remove the paragraph end tag
|
||||||
* otherwise, keep the paragraph open (don't make another one)
|
* that was automatically added. The paragraph end tag should be
|
||||||
* @protected
|
* removed unless the next token is a paragraph or block element.
|
||||||
|
* @param $config Instance of HTMLPurifier_Config
|
||||||
|
* @param $context Instance of HTMLPurifier_Context
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
function _needsEndTag($tokens, $k, $definition) {
|
function _removeParagraphEnd($config, &$context) {
|
||||||
$end_paragraph = false;
|
$tokens = $context->get('InputTokens');
|
||||||
for ($j = $k + 1; isset($tokens[$j]); $j++) {
|
$i = $context->get('InputIndex');
|
||||||
if ($tokens[$j]->type == 'start' || $tokens[$j]->type == 'empty') {
|
$remove_paragraph_end = true;
|
||||||
if ($tokens[$j]->name == 'p') {
|
// Start of the checks one after the current token's index
|
||||||
$end_paragraph = true;
|
for ($i++; isset($tokens[$i]); $i++) {
|
||||||
} else {
|
if ($tokens[$i]->type == 'start' || $tokens[$i]->type == 'empty') {
|
||||||
$end_paragraph = isset($definition->info['p']->auto_close[$tokens[$j]->name]);
|
$definition = $config->getHTMLDefinition();
|
||||||
}
|
$remove_paragraph_end = $this->_isInline($tokens[$i], $config);
|
||||||
break;
|
|
||||||
} elseif ($tokens[$j]->type == 'text') {
|
|
||||||
if (!$tokens[$j]->is_whitespace) {
|
|
||||||
$end_paragraph = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} elseif ($tokens[$j]->type == 'end') {
|
|
||||||
// nonsensical case
|
|
||||||
$end_paragraph = false;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// check if we can abort early (whitespace means we carry-on!)
|
||||||
|
if ($tokens[$i]->type == 'text' && !$tokens[$i]->is_whitespace) break;
|
||||||
|
if ($tokens[$i]->type == 'end') break; // nonsensical
|
||||||
}
|
}
|
||||||
return $end_paragraph;
|
return $remove_paragraph_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if passed token is inline (and, ergo, allowed in
|
||||||
|
* paragraph tags)
|
||||||
|
*/
|
||||||
|
function _isInline($token, $config) {
|
||||||
|
$definition = $config->getHTMLDefinition();
|
||||||
|
return !isset($definition->info['p']->auto_close[$token->name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -225,7 +225,8 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
|
|||||||
$current_nesting[] = $token;
|
$current_nesting[] = $token;
|
||||||
} elseif ($token->type == 'end') {
|
} elseif ($token->type == 'end') {
|
||||||
// theoretical: this isn't used because performing
|
// theoretical: this isn't used because performing
|
||||||
// the calculations otherwise is more efficient
|
// the calculations inline is more efficient, and
|
||||||
|
// end tokens currently do not cause a handler invocation
|
||||||
array_pop($current_nesting);
|
array_pop($current_nesting);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,6 +92,15 @@ Par 1 still</p>'
|
|||||||
$this->assertResult(
|
$this->assertResult(
|
||||||
'Par1
|
'Par1
|
||||||
|
|
||||||
|
Par2',
|
||||||
|
'<p>Par1</p><p>Par2</p>'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertResult(
|
||||||
|
'Par1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Par2',
|
Par2',
|
||||||
'<p>Par1</p><p>Par2</p>'
|
'<p>Par1</p><p>Par2</p>'
|
||||||
);
|
);
|
||||||
@ -149,6 +158,22 @@ Par3',
|
|||||||
'<p>Par<b>1</b></p>'
|
'<p>Par<b>1</b></p>'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->assertResult(
|
||||||
|
'
|
||||||
|
|
||||||
|
Par',
|
||||||
|
'<p>Par</p>'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertResult(
|
||||||
|
'
|
||||||
|
|
||||||
|
Par
|
||||||
|
|
||||||
|
',
|
||||||
|
'<p>Par</p>'
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user