mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-08-01 11:50:28 +02:00
Remove trailing whitespace.
Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
This commit is contained in:
@@ -8,16 +8,16 @@
|
||||
*/
|
||||
class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
{
|
||||
|
||||
|
||||
public $name = 'AutoParagraph';
|
||||
public $needed = array('p');
|
||||
|
||||
|
||||
private function _pStart() {
|
||||
$par = new HTMLPurifier_Token_Start('p');
|
||||
$par->armor['MakeWellFormed_TagClosedError'] = true;
|
||||
return $par;
|
||||
}
|
||||
|
||||
|
||||
public function handleText(&$token) {
|
||||
$text = $token->data;
|
||||
// Does the current parent allow <p> tags?
|
||||
@@ -27,7 +27,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
// in the anonymous root node, or a node inside the document.
|
||||
// If the text as a double-newline, the treatment is the same;
|
||||
// if it doesn't, see the next if-block if you're in the document.
|
||||
|
||||
|
||||
$i = $nesting = null;
|
||||
if (!$this->forwardUntilEndToken($i, $current, $nesting) && $token->is_whitespace) {
|
||||
// State 1.1: ... ^ (whitespace, then document end)
|
||||
@@ -36,10 +36,10 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
} else {
|
||||
// State 1.2: PAR1
|
||||
// ----
|
||||
|
||||
|
||||
// State 1.3: PAR1\n\nPAR2
|
||||
// ------------
|
||||
|
||||
|
||||
// State 1.4: <div>PAR1\n\nPAR2 (see State 2)
|
||||
// ------------
|
||||
$token = array($this->_pStart());
|
||||
@@ -48,7 +48,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
} else {
|
||||
// State 2: <div>PAR1... (similar to 1.4)
|
||||
// ----
|
||||
|
||||
|
||||
// We're in an element that allows paragraph tags, but we're not
|
||||
// sure if we're going to need them.
|
||||
if ($this->_pLookAhead()) {
|
||||
@@ -62,7 +62,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
} else {
|
||||
// State 2.2.1: <div>PAR1<div>
|
||||
// ----
|
||||
|
||||
|
||||
// State 2.2.2: <div>PAR1<b>PAR1</b></div>
|
||||
// ----
|
||||
}
|
||||
@@ -74,7 +74,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
) {
|
||||
// State 3.1: ...<p>PAR1
|
||||
// ----
|
||||
|
||||
|
||||
// State 3.2: ...<p>PAR1\n\nPAR2
|
||||
// ------------
|
||||
$token = array();
|
||||
@@ -83,12 +83,12 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
} else {
|
||||
// State 4.1: ...<b>PAR1
|
||||
// ----
|
||||
|
||||
|
||||
// State 4.2: ...<b>PAR1\n\nPAR2
|
||||
// ------------
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function handleElement(&$token) {
|
||||
// We don't have to check if we're already in a <p> tag for block
|
||||
// tokens, because the tag would have been autoclosed by MakeWellFormed.
|
||||
@@ -97,39 +97,39 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
if ($this->_isInline($token)) {
|
||||
// State 1: <div>...<b>
|
||||
// ---
|
||||
|
||||
|
||||
// Check if this token is adjacent to the parent token
|
||||
// (seek backwards until token isn't whitespace)
|
||||
$i = null;
|
||||
$this->backward($i, $prev);
|
||||
|
||||
|
||||
if (!$prev instanceof HTMLPurifier_Token_Start) {
|
||||
// Token wasn't adjacent
|
||||
|
||||
|
||||
if (
|
||||
$prev instanceof HTMLPurifier_Token_Text &&
|
||||
substr($prev->data, -2) === "\n\n"
|
||||
) {
|
||||
// State 1.1.4: <div><p>PAR1</p>\n\n<b>
|
||||
// ---
|
||||
|
||||
|
||||
// Quite frankly, this should be handled by splitText
|
||||
$token = array($this->_pStart(), $token);
|
||||
} else {
|
||||
// State 1.1.1: <div><p>PAR1</p><b>
|
||||
// ---
|
||||
|
||||
|
||||
// State 1.1.2: <div><br /><b>
|
||||
// ---
|
||||
|
||||
|
||||
// State 1.1.3: <div>PAR<b>
|
||||
// ---
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
// State 1.2.1: <div><b>
|
||||
// ---
|
||||
|
||||
|
||||
// Lookahead to see if <p> is needed.
|
||||
if ($this->_pLookAhead()) {
|
||||
// State 1.3.1: <div><b>PAR1\n\nPAR2
|
||||
@@ -138,7 +138,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
} else {
|
||||
// State 1.3.2: <div><b>PAR1</b></div>
|
||||
// ---
|
||||
|
||||
|
||||
// State 1.3.3: <div><b>PAR1</b><div></div>\n\n</div>
|
||||
// ---
|
||||
}
|
||||
@@ -158,7 +158,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
// State 3.2: <div>
|
||||
// -----
|
||||
}
|
||||
|
||||
|
||||
$i = null;
|
||||
if ($this->backward($i, $prev)) {
|
||||
if (
|
||||
@@ -166,19 +166,19 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
) {
|
||||
// State 3.1.1: ...</p>{p}<b>
|
||||
// ---
|
||||
|
||||
|
||||
// State 3.2.1: ...</p><div>
|
||||
// -----
|
||||
|
||||
|
||||
if (!is_array($token)) $token = array($token);
|
||||
array_unshift($token, new HTMLPurifier_Token_Text("\n\n"));
|
||||
} else {
|
||||
// State 3.1.2: ...</p>\n\n{p}<b>
|
||||
// ---
|
||||
|
||||
|
||||
// State 3.2.2: ...</p>\n\n<div>
|
||||
// -----
|
||||
|
||||
|
||||
// Note: PAR<ELEM> cannot occur because PAR would have been
|
||||
// wrapped in <p> tags.
|
||||
}
|
||||
@@ -187,12 +187,12 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
} else {
|
||||
// State 2.2: <ul><li>
|
||||
// ----
|
||||
|
||||
|
||||
// State 2.4: <p><b>
|
||||
// ---
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Splits up a text in paragraph tokens and appends them
|
||||
* to the result stream that will replace the original
|
||||
@@ -208,7 +208,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
$paragraphs = array(); // without empty paragraphs
|
||||
$needs_start = false;
|
||||
$needs_end = false;
|
||||
|
||||
|
||||
$c = count($raw_paragraphs);
|
||||
if ($c == 1) {
|
||||
// There were no double-newlines, abort quickly. In theory this
|
||||
@@ -230,7 +230,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
// a while, and the newline means we should start a new one.
|
||||
$result[] = new HTMLPurifier_Token_End('p');
|
||||
$result[] = new HTMLPurifier_Token_Text("\n\n");
|
||||
// However, the start token should only be added if
|
||||
// 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
|
||||
@@ -249,18 +249,18 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Check if this was just a giant blob of whitespace. Move this earlier,
|
||||
// perhaps?
|
||||
if (empty($paragraphs)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Add the start tag indicated by \n\n at the beginning of $data
|
||||
if ($needs_start) {
|
||||
$result[] = $this->_pStart();
|
||||
}
|
||||
|
||||
|
||||
// Append the paragraphs onto the result
|
||||
foreach ($paragraphs as $par) {
|
||||
$result[] = new HTMLPurifier_Token_Text($par);
|
||||
@@ -268,21 +268,21 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
$result[] = new HTMLPurifier_Token_Text("\n\n");
|
||||
$result[] = $this->_pStart();
|
||||
}
|
||||
|
||||
|
||||
// Remove trailing start token; Injector will handle this later if
|
||||
// it was indeed needed. This prevents from needing to do a lookahead,
|
||||
// at the cost of a lookbehind later.
|
||||
array_pop($result);
|
||||
|
||||
// If there is no need for an end tag, remove all of it and let
|
||||
|
||||
// If there is no need for an end tag, remove all of it and let
|
||||
// MakeWellFormed close it later.
|
||||
if (!$needs_end) {
|
||||
array_pop($result); // removes \n\n
|
||||
array_pop($result); // removes </p>
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if passed token is inline (and, ergo, allowed in
|
||||
* paragraph tags)
|
||||
@@ -290,7 +290,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
private function _isInline($token) {
|
||||
return isset($this->htmlDefinition->info['p']->child->elements[$token->name]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Looks ahead in the token list and determines whether or not we need
|
||||
* to insert a <p> tag.
|
||||
@@ -309,7 +309,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
}
|
||||
return $ok;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines if a particular token requires an earlier inline token
|
||||
* to get a paragraph. This should be used with _forwardUntilEndToken
|
||||
@@ -334,6 +334,6 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@@ -5,13 +5,13 @@
|
||||
*/
|
||||
class HTMLPurifier_Injector_DisplayLinkURI extends HTMLPurifier_Injector
|
||||
{
|
||||
|
||||
|
||||
public $name = 'DisplayLinkURI';
|
||||
public $needed = array('a');
|
||||
|
||||
|
||||
public function handleElement(&$token) {
|
||||
}
|
||||
|
||||
|
||||
public function handleEnd(&$token) {
|
||||
if (isset($token->start->attr['href'])){
|
||||
$url = $token->start->attr['href'];
|
||||
|
@@ -5,26 +5,26 @@
|
||||
*/
|
||||
class HTMLPurifier_Injector_Linkify extends HTMLPurifier_Injector
|
||||
{
|
||||
|
||||
|
||||
public $name = 'Linkify';
|
||||
public $needed = array('a' => array('href'));
|
||||
|
||||
|
||||
public function handleText(&$token) {
|
||||
if (!$this->allowsElement('a')) return;
|
||||
|
||||
|
||||
if (strpos($token->data, '://') === false) {
|
||||
// our really quick heuristic failed, abort
|
||||
// this may not work so well if we want to match things like
|
||||
// "google.com", but then again, most people don't
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// there is/are URL(s). Let's split the string:
|
||||
// Note: this regex is extremely permissive
|
||||
$bits = preg_split('#((?:https?|ftp)://[^\s\'"<>()]+)#S', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);
|
||||
|
||||
|
||||
$token = array();
|
||||
|
||||
|
||||
// $i = index
|
||||
// $c = count
|
||||
// $l = is link
|
||||
@@ -38,8 +38,8 @@ class HTMLPurifier_Injector_Linkify extends HTMLPurifier_Injector
|
||||
$token[] = new HTMLPurifier_Token_End('a');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@@ -6,23 +6,23 @@
|
||||
*/
|
||||
class HTMLPurifier_Injector_PurifierLinkify extends HTMLPurifier_Injector
|
||||
{
|
||||
|
||||
|
||||
public $name = 'PurifierLinkify';
|
||||
public $docURL;
|
||||
public $needed = array('a' => array('href'));
|
||||
|
||||
|
||||
public function prepare($config, $context) {
|
||||
$this->docURL = $config->get('AutoFormatParam', 'PurifierLinkifyDocURL');
|
||||
return parent::prepare($config, $context);
|
||||
}
|
||||
|
||||
|
||||
public function handleText(&$token) {
|
||||
if (!$this->allowsElement('a')) return;
|
||||
if (strpos($token->data, '%') === false) return;
|
||||
|
||||
|
||||
$bits = preg_split('#%([a-z0-9]+\.[a-z0-9]+)#Si', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);
|
||||
$token = array();
|
||||
|
||||
|
||||
// $i = index
|
||||
// $c = count
|
||||
// $l = is link
|
||||
@@ -37,8 +37,8 @@ class HTMLPurifier_Injector_PurifierLinkify extends HTMLPurifier_Injector
|
||||
$token[] = new HTMLPurifier_Token_End('a');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@@ -2,16 +2,16 @@
|
||||
|
||||
class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
|
||||
{
|
||||
|
||||
|
||||
private $context, $config;
|
||||
|
||||
|
||||
public function prepare($config, $context) {
|
||||
parent::prepare($config, $context);
|
||||
$this->config = $config;
|
||||
$this->context = $context;
|
||||
$this->attrValidator = new HTMLPurifier_AttrValidator();
|
||||
}
|
||||
|
||||
|
||||
public function handleElement(&$token) {
|
||||
if (!$token instanceof HTMLPurifier_Token_Start) return;
|
||||
$next = false;
|
||||
@@ -36,5 +36,5 @@ class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -8,10 +8,10 @@ class HTMLPurifier_Injector_SafeObject extends HTMLPurifier_Injector
|
||||
{
|
||||
public $name = 'SafeObject';
|
||||
public $needed = array('object', 'param');
|
||||
|
||||
|
||||
protected $objectStack = array();
|
||||
protected $paramStack = array();
|
||||
|
||||
|
||||
// Keep this synchronized with AttrTransform/SafeParam.php
|
||||
protected $addParam = array(
|
||||
'allowScriptAccess' => 'never',
|
||||
@@ -21,11 +21,11 @@ class HTMLPurifier_Injector_SafeObject extends HTMLPurifier_Injector
|
||||
'wmode' => true,
|
||||
'movie' => true,
|
||||
);
|
||||
|
||||
|
||||
public function prepare($config, $context) {
|
||||
parent::prepare($config, $context);
|
||||
}
|
||||
|
||||
|
||||
public function handleElement(&$token) {
|
||||
if ($token->name == 'object') {
|
||||
$this->objectStack[] = $token;
|
||||
@@ -71,7 +71,7 @@ class HTMLPurifier_Injector_SafeObject extends HTMLPurifier_Injector
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function handleEnd(&$token) {
|
||||
// This is the WRONG way of handling the object and param stacks;
|
||||
// we should be inserting them directly on the relevant object tokens
|
||||
@@ -81,6 +81,6 @@ class HTMLPurifier_Injector_SafeObject extends HTMLPurifier_Injector
|
||||
array_pop($this->paramStack);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user