2007-06-23 18:50:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Injector.php';
|
|
|
|
|
2007-06-24 20:29:50 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define(
|
|
|
|
'AutoFormat', 'AutoParagraph', false, 'bool', '
|
|
|
|
<p>
|
|
|
|
This directive turns on auto-paragraphing, where double newlines are
|
|
|
|
converted in to paragraphs whenever possible. Auto-paragraphing
|
|
|
|
applies when:
|
|
|
|
</p>
|
|
|
|
<ul>
|
|
|
|
<li>There are inline elements or text in the root node</li>
|
|
|
|
<li>There are inline elements or text with double newlines or
|
|
|
|
block elements in nodes that allow paragraph tags</li>
|
|
|
|
<li>There are double newlines in paragraph tags</li>
|
|
|
|
</ul>
|
|
|
|
<p>
|
|
|
|
This directive has been available since 2.0.1.
|
|
|
|
</p>
|
|
|
|
');
|
|
|
|
|
2007-06-23 18:50:41 +00:00
|
|
|
/**
|
|
|
|
* Injector that auto paragraphs text in the root node based on
|
|
|
|
* double-spacing.
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
|
|
|
{
|
|
|
|
|
2007-06-26 15:07:07 +00:00
|
|
|
function _pStart() {
|
|
|
|
$par = new HTMLPurifier_Token_Start('p');
|
|
|
|
$par->armor['MakeWellFormed_TagClosedError'] = true;
|
|
|
|
return $par;
|
|
|
|
}
|
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
function handleText(&$token) {
|
2007-06-23 19:39:03 +00:00
|
|
|
$text = $token->data;
|
2007-06-24 17:44:27 +00:00
|
|
|
if (empty($this->currentNesting)) {
|
|
|
|
if (!$this->allowsElement('p')) return;
|
2007-06-24 20:29:50 +00:00
|
|
|
// case 1: we're in root node (and it allows paragraphs)
|
2007-06-26 19:33:37 +00:00
|
|
|
$token = array($this->_pStart());
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->_splitText($text, $token);
|
|
|
|
} elseif ($this->currentNesting[count($this->currentNesting)-1]->name == 'p') {
|
2007-06-24 20:29:50 +00:00
|
|
|
// case 2: we're in a paragraph
|
2007-06-23 19:39:03 +00:00
|
|
|
$token = array();
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->_splitText($text, $token);
|
2007-06-24 20:29:50 +00:00
|
|
|
} elseif ($this->allowsElement('p')) {
|
|
|
|
// case 3: we're in an element that allows paragraphs
|
2007-06-25 19:18:55 +00:00
|
|
|
if (strpos($text, "\n\n") !== false) {
|
2007-06-24 20:29:50 +00:00
|
|
|
// case 3.1: this text node has a double-newline
|
2007-06-26 15:07:07 +00:00
|
|
|
$token = array($this->_pStart());
|
2007-06-24 20:29:50 +00:00
|
|
|
$this->_splitText($text, $token);
|
|
|
|
} else {
|
|
|
|
$ok = false;
|
|
|
|
// test if up-coming tokens are either block or have
|
|
|
|
// a double newline in them
|
|
|
|
for ($i = $this->inputIndex + 1; isset($this->inputTokens[$i]); $i++) {
|
|
|
|
if ($this->inputTokens[$i]->type == 'start'){
|
|
|
|
if (!$this->_isInline($this->inputTokens[$i])) {
|
|
|
|
$ok = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($this->inputTokens[$i]->type == 'end') break;
|
|
|
|
if ($this->inputTokens[$i]->type == 'text') {
|
2007-06-25 19:18:55 +00:00
|
|
|
if (strpos($this->inputTokens[$i]->data, "\n\n") !== false) {
|
2007-06-24 20:29:50 +00:00
|
|
|
$ok = true;
|
|
|
|
}
|
|
|
|
if (!$this->inputTokens[$i]->is_whitespace) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($ok) {
|
|
|
|
// case 3.2: this text node is next to another node
|
|
|
|
// that will start a paragraph
|
2007-06-26 15:07:07 +00:00
|
|
|
$token = array($this->_pStart(), $token);
|
2007-06-24 20:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
2007-06-24 20:29:50 +00:00
|
|
|
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
function handleStart(&$token) {
|
2007-06-24 20:29:50 +00:00
|
|
|
// check if we're inside a tag already
|
|
|
|
if (!empty($this->currentNesting)) {
|
|
|
|
if ($this->allowsElement('p')) {
|
|
|
|
// special case: we're in an element that allows paragraphs
|
|
|
|
|
|
|
|
// this token is already paragraph, abort
|
|
|
|
if ($token->name == 'p') return;
|
|
|
|
|
|
|
|
// check if this token is adjacent to the parent
|
|
|
|
if ($this->inputTokens[$this->inputIndex - 1]->type != 'start') {
|
|
|
|
// not adjacent, we can abort early
|
|
|
|
// add lead paragraph tag if our token is inline
|
|
|
|
if ($this->_isInline($token)) {
|
2007-06-26 15:07:07 +00:00
|
|
|
$token = array($this->_pStart(), $token);
|
2007-06-24 20:29:50 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this token is the first child of the element that allows
|
|
|
|
// paragraph. We have to peek ahead and see whether or not
|
|
|
|
// there is anything inside that suggests that a paragraph
|
|
|
|
// will be needed
|
|
|
|
$ok = false;
|
|
|
|
// maintain a mini-nesting counter, this lets us bail out
|
|
|
|
// early if possible
|
|
|
|
$j = 2; // current nesting, is two due to parent and this start
|
|
|
|
for ($i = $this->inputIndex + 1; isset($this->inputTokens[$i]); $i++) {
|
|
|
|
if ($this->inputTokens[$i]->type == 'start') $j++;
|
|
|
|
if ($this->inputTokens[$i]->type == 'end') $j--;
|
|
|
|
if ($this->inputTokens[$i]->type == 'text') {
|
2007-06-25 19:18:55 +00:00
|
|
|
if (strpos($this->inputTokens[$i]->data, "\n\n") !== false) {
|
2007-06-24 20:29:50 +00:00
|
|
|
$ok = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($j <= 0) break;
|
|
|
|
}
|
|
|
|
if ($ok) {
|
2007-06-26 15:07:07 +00:00
|
|
|
$token = array($this->_pStart(), $token);
|
2007-06-24 20:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2007-06-23 18:58:51 +00:00
|
|
|
|
|
|
|
// check if the start tag counts as a "block" element
|
2007-06-24 17:44:27 +00:00
|
|
|
if (!$this->_isInline($token)) return;
|
2007-06-23 18:58:51 +00:00
|
|
|
|
|
|
|
// append a paragraph tag before the token
|
2007-06-26 15:07:07 +00:00
|
|
|
$token = array($this->_pStart(), $token);
|
2007-06-23 18:58:51 +00:00
|
|
|
}
|
|
|
|
|
2007-06-23 18:50:41 +00:00
|
|
|
/**
|
2007-06-23 19:39:03 +00:00
|
|
|
* Splits up a text in paragraph tokens and appends them
|
|
|
|
* to the result stream that will replace the original
|
|
|
|
* @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
|
2007-06-23 18:50:41 +00:00
|
|
|
*/
|
2007-06-24 17:44:27 +00:00
|
|
|
function _splitText($data, &$result) {
|
2007-06-25 19:18:55 +00:00
|
|
|
$raw_paragraphs = explode("\n\n", $data);
|
2007-06-23 18:50:41 +00:00
|
|
|
|
|
|
|
// remove empty paragraphs
|
|
|
|
$paragraphs = array();
|
2007-06-24 20:29:50 +00:00
|
|
|
$needs_start = false;
|
2007-06-26 15:07:07 +00:00
|
|
|
$needs_end = false;
|
|
|
|
|
|
|
|
for ($i = 0, $c = count($raw_paragraphs); $i < $c; $i++) {
|
|
|
|
$par = $raw_paragraphs[$i];
|
2007-06-24 20:29:50 +00:00
|
|
|
if (trim($par) !== '') {
|
2007-06-26 15:07:07 +00:00
|
|
|
$paragraphs[] = $par;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($i == 0 && empty($result)) {
|
2007-06-24 20:29:50 +00:00
|
|
|
// The empty result indicates that the AutoParagraph
|
|
|
|
// injector did not add any start paragraph tokens.
|
|
|
|
// The fact that the first paragraph is empty indicates
|
|
|
|
// that there was a double-newline at the start of the
|
|
|
|
// data.
|
|
|
|
// Combined together, this means that we are in a paragraph,
|
|
|
|
// and the newline means we should start a new one.
|
|
|
|
$result[] = new HTMLPurifier_Token_End('p');
|
|
|
|
// However, the start token should only be added if
|
|
|
|
// there is more processing to be done (i.e. there are
|
|
|
|
// real paragraphs in here). If there are none, the
|
|
|
|
// next start paragraph tag will be handled by the
|
|
|
|
// next run-around the injector
|
|
|
|
$needs_start = true;
|
2007-06-26 15:07:07 +00:00
|
|
|
} elseif ($i + 1 == $c) {
|
|
|
|
// a double-paragraph at the end indicates that
|
|
|
|
// there is an overriding need to start a new paragraph
|
|
|
|
// for the next section. This has no effect until
|
|
|
|
// we've processed all of the other paragraphs though
|
|
|
|
$needs_end = true;
|
2007-06-24 20:29:50 +00:00
|
|
|
}
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
|
|
|
|
2007-06-23 19:39:03 +00:00
|
|
|
// check if there are no "real" paragraphs to be processed
|
2007-06-24 20:29:50 +00:00
|
|
|
if (empty($paragraphs)) {
|
2007-06-23 18:50:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-24 20:29:50 +00:00
|
|
|
// add a start tag if an end tag was added while processing
|
|
|
|
// the raw paragraphs (that happens if there's a leading double
|
|
|
|
// newline)
|
2007-06-26 15:07:07 +00:00
|
|
|
if ($needs_start) $result[] = $this->_pStart();
|
2007-06-24 20:29:50 +00:00
|
|
|
|
2007-06-23 19:39:03 +00:00
|
|
|
// append the paragraphs onto the result
|
|
|
|
foreach ($paragraphs as $par) {
|
|
|
|
$result[] = new HTMLPurifier_Token_Text($par);
|
2007-06-23 18:50:41 +00:00
|
|
|
$result[] = new HTMLPurifier_Token_End('p');
|
2007-06-26 15:07:07 +00:00
|
|
|
$result[] = $this->_pStart();
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
|
|
|
|
2007-06-24 20:29:50 +00:00
|
|
|
// remove trailing start token, if one is needed, it will
|
|
|
|
// be handled the next time this injector is called
|
|
|
|
array_pop($result);
|
2007-06-23 18:50:41 +00:00
|
|
|
|
2007-06-24 20:29:50 +00:00
|
|
|
// check the outside to determine whether or not the
|
|
|
|
// end paragraph tag should be removed. It should be removed
|
|
|
|
// unless the next non-whitespace token is a paragraph
|
|
|
|
// or a block element.
|
2007-06-23 19:39:03 +00:00
|
|
|
$remove_paragraph_end = true;
|
2007-06-26 15:07:07 +00:00
|
|
|
|
|
|
|
if (!$needs_end) {
|
|
|
|
// Start of the checks one after the current token's index
|
|
|
|
for ($i = $this->inputIndex + 1; isset($this->inputTokens[$i]); $i++) {
|
|
|
|
if ($this->inputTokens[$i]->type == 'start' || $this->inputTokens[$i]->type == 'empty') {
|
|
|
|
$remove_paragraph_end = $this->_isInline($this->inputTokens[$i]);
|
|
|
|
}
|
|
|
|
// check if we can abort early (whitespace means we carry-on!)
|
|
|
|
if ($this->inputTokens[$i]->type == 'text' && !$this->inputTokens[$i]->is_whitespace) break;
|
|
|
|
// end tags will automatically be handled by MakeWellFormed,
|
|
|
|
// so we don't have to worry about them
|
|
|
|
if ($this->inputTokens[$i]->type == 'end') break;
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
2007-06-26 15:07:07 +00:00
|
|
|
} else {
|
|
|
|
$remove_paragraph_end = false;
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
2007-06-24 20:29:50 +00:00
|
|
|
|
|
|
|
// check the outside to determine whether or not the
|
|
|
|
// end paragraph tag should be removed
|
|
|
|
if ($remove_paragraph_end) {
|
|
|
|
array_pop($result);
|
|
|
|
}
|
|
|
|
|
2007-06-23 19:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if passed token is inline (and, ergo, allowed in
|
|
|
|
* paragraph tags)
|
2007-06-24 17:44:27 +00:00
|
|
|
* @private
|
2007-06-23 19:39:03 +00:00
|
|
|
*/
|
2007-06-24 17:44:27 +00:00
|
|
|
function _isInline($token) {
|
|
|
|
return isset($this->htmlDefinition->info['p']->child->elements[$token->name]);
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|