2007-06-23 18:50:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Injects tokens into the document while parsing for well-formedness.
|
|
|
|
* This enables "formatter-like" functionality such as auto-paragraphing,
|
|
|
|
* smiley-ification and linkification to take place.
|
2007-09-09 01:27:09 +00:00
|
|
|
*
|
|
|
|
* @todo Allow injectors to request a re-run on their output. This
|
|
|
|
* would help if an operation is recursive.
|
2007-06-23 18:50:41 +00:00
|
|
|
*/
|
2007-12-05 01:26:28 +00:00
|
|
|
abstract class HTMLPurifier_Injector
|
2007-06-23 18:50:41 +00:00
|
|
|
{
|
|
|
|
|
2007-06-28 13:06:15 +00:00
|
|
|
/**
|
|
|
|
* Advisory name of injector, this is for friendly error messages
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $name;
|
2007-06-28 13:06:15 +00:00
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
/**
|
|
|
|
* Amount of tokens the injector needs to skip + 1. Because
|
|
|
|
* the decrement is the first thing that happens, this needs to
|
|
|
|
* be one greater than the "real" skip count.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $skip = 1;
|
2007-06-24 17:44:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of HTMLPurifier_HTMLDefinition
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $htmlDefinition;
|
2007-06-24 17:44:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to CurrentNesting variable in Context. This is an array
|
|
|
|
* list of tokens that we are currently "inside"
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $currentNesting;
|
2007-06-24 17:44:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to InputTokens variable in Context. This is an array
|
|
|
|
* list of the input tokens that are being processed.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $inputTokens;
|
2007-06-24 17:44:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to InputIndex variable in Context. This is an integer
|
|
|
|
* array index for $this->inputTokens that indicates what token
|
|
|
|
* is currently being processed.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $inputIndex;
|
2007-06-24 17:44:27 +00:00
|
|
|
|
|
|
|
/**
|
2007-06-28 13:06:15 +00:00
|
|
|
* Array of elements and attributes this injector creates and therefore
|
|
|
|
* need to be allowed by the definition. Takes form of
|
|
|
|
* array('element' => array('attr', 'attr2'), 'element2')
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $needed = array();
|
2007-06-28 13:06:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the injector by giving it the config and context objects:
|
|
|
|
* this allows references to important variables to be made within
|
|
|
|
* the injector. This function also checks if the HTML environment
|
|
|
|
* will work with the Injector: if p tags are not allowed, the
|
|
|
|
* Auto-Paragraphing injector should not be enabled.
|
|
|
|
* @param $config Instance of HTMLPurifier_Config
|
|
|
|
* @param $context Instance of HTMLPurifier_Context
|
|
|
|
* @return Boolean false if success, string of missing needed element/attribute if failure
|
2007-06-24 17:44:27 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function prepare($config, &$context) {
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->htmlDefinition = $config->getHTMLDefinition();
|
2007-06-28 13:06:15 +00:00
|
|
|
// perform $needed checks
|
|
|
|
foreach ($this->needed as $element => $attributes) {
|
|
|
|
if (is_int($element)) $element = $attributes;
|
|
|
|
if (!isset($this->htmlDefinition->info[$element])) return $element;
|
|
|
|
if (!is_array($attributes)) continue;
|
|
|
|
foreach ($attributes as $name) {
|
|
|
|
if (!isset($this->htmlDefinition->info[$element]->attr[$name])) return "$element.$name";
|
|
|
|
}
|
|
|
|
}
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->currentNesting =& $context->get('CurrentNesting');
|
|
|
|
$this->inputTokens =& $context->get('InputTokens');
|
|
|
|
$this->inputIndex =& $context->get('InputIndex');
|
2007-06-28 13:06:15 +00:00
|
|
|
return false;
|
2007-06-24 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if the context node allows a certain element
|
|
|
|
* @param $name Name of element to test for
|
|
|
|
* @return True if element is allowed, false if it is not
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function allowsElement($name) {
|
2007-06-24 17:44:27 +00:00
|
|
|
if (!empty($this->currentNesting)) {
|
|
|
|
$parent_token = array_pop($this->currentNesting);
|
|
|
|
$this->currentNesting[] = $parent_token;
|
|
|
|
$parent = $this->htmlDefinition->info[$parent_token->name];
|
|
|
|
} else {
|
|
|
|
$parent = $this->htmlDefinition->info_parent_def;
|
|
|
|
}
|
|
|
|
if (!isset($parent->child->elements[$name]) || isset($parent->excludes[$name])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-06-23 18:50:41 +00:00
|
|
|
/**
|
|
|
|
* Handler that is called when a text token is processed
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function handleText(&$token) {}
|
2007-06-23 18:50:41 +00:00
|
|
|
|
|
|
|
/**
|
2007-06-29 00:24:59 +00:00
|
|
|
* Handler that is called when a start or empty token is processed
|
2007-06-23 18:50:41 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function handleElement(&$token) {}
|
2007-06-23 18:50:41 +00:00
|
|
|
|
2007-10-02 22:50:59 +00:00
|
|
|
/**
|
|
|
|
* Notifier that is called when an end token is processed
|
|
|
|
* @note This differs from handlers in that the token is read-only
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function notifyEnd($token) {}
|
2007-10-02 22:50:59 +00:00
|
|
|
|
|
|
|
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
|
|
|
|