2006-07-22 13:50:05 +00:00
|
|
|
<?php
|
|
|
|
|
2006-07-23 18:56:00 +00:00
|
|
|
/**
|
|
|
|
* Parser that uses PHP 5's DOM extension (part of the core).
|
|
|
|
*
|
|
|
|
* In PHP 5, the DOM XML extension was revamped into DOM and added to the core.
|
|
|
|
* It gives us a forgiving HTML parser, which we use to transform the HTML
|
|
|
|
* into a DOM, and then into the tokens. It is blazingly fast (for large
|
|
|
|
* documents, it performs twenty times faster than
|
|
|
|
* HTMLPurifier_Lexer_DirectLex,and is the default choice for PHP 5.
|
|
|
|
*
|
2006-09-17 21:59:40 +00:00
|
|
|
* @note Any empty elements will have empty tokens associated with them, even if
|
2006-07-23 18:56:00 +00:00
|
|
|
* this is prohibited by the spec. This is cannot be fixed until the spec
|
|
|
|
* comes into play.
|
|
|
|
*
|
2006-09-17 21:59:40 +00:00
|
|
|
* @note PHP's DOM extension does not actually parse any entities, we use
|
|
|
|
* our own function to do that.
|
2006-09-24 02:06:12 +00:00
|
|
|
*
|
|
|
|
* @warning DOM tends to drop whitespace, which may wreak havoc on indenting.
|
|
|
|
* If this is a huge problem, due to the fact that HTML is hand
|
2007-01-28 22:19:05 +00:00
|
|
|
* edited and you are unable to get a parser cache that caches the
|
2006-09-24 02:06:12 +00:00
|
|
|
* the output of HTML Purifier while keeping the original HTML lying
|
|
|
|
* around, you may want to run Tidy on the resulting output or use
|
|
|
|
* HTMLPurifier_DirectLex
|
2006-07-23 18:56:00 +00:00
|
|
|
*/
|
2006-07-22 13:50:05 +00:00
|
|
|
|
|
|
|
class HTMLPurifier_Lexer_DOMLex extends HTMLPurifier_Lexer
|
|
|
|
{
|
|
|
|
|
2006-08-15 20:19:16 +00:00
|
|
|
private $factory;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
// setup the factory
|
2007-11-29 04:29:51 +00:00
|
|
|
parent::__construct();
|
2006-08-15 20:19:16 +00:00
|
|
|
$this->factory = new HTMLPurifier_TokenFactory();
|
|
|
|
}
|
|
|
|
|
2008-01-05 00:10:43 +00:00
|
|
|
public function tokenizeHTML($html, $config, $context) {
|
2006-08-15 00:31:12 +00:00
|
|
|
|
2007-06-21 23:56:19 +00:00
|
|
|
$html = $this->normalize($html, $config, $context);
|
2006-08-19 17:53:59 +00:00
|
|
|
|
2007-06-27 16:40:18 +00:00
|
|
|
// attempt to armor stray angled brackets that cannot possibly
|
|
|
|
// form tags and thus are probably being used as emoticons
|
|
|
|
if ($config->get('Core', 'AggressivelyFixLt')) {
|
|
|
|
$char = '[^a-z!\/]';
|
|
|
|
$comment = "/<!--(.*?)(-->|\z)/is";
|
2007-11-25 02:24:39 +00:00
|
|
|
$html = preg_replace_callback($comment, array($this, 'callbackArmorCommentEntities'), $html);
|
2007-06-27 16:40:18 +00:00
|
|
|
$html = preg_replace("/<($char)/i", '<\\1', $html);
|
2007-11-25 02:24:39 +00:00
|
|
|
$html = preg_replace_callback($comment, array($this, 'callbackUndoCommentSubst'), $html); // fix comments
|
2007-06-27 16:40:18 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 23:56:19 +00:00
|
|
|
// preprocess html, essential for UTF-8
|
2007-08-19 18:49:35 +00:00
|
|
|
$html = $this->wrapHTML($html, $config, $context);
|
2006-08-14 13:27:18 +00:00
|
|
|
|
2006-08-29 20:05:26 +00:00
|
|
|
$doc = new DOMDocument();
|
2007-06-17 20:36:29 +00:00
|
|
|
$doc->encoding = 'UTF-8'; // theoretically, the above has this covered
|
2007-01-28 22:19:05 +00:00
|
|
|
|
2007-06-17 20:36:29 +00:00
|
|
|
set_error_handler(array($this, 'muteErrorHandler'));
|
2007-06-21 23:56:19 +00:00
|
|
|
$doc->loadHTML($html);
|
2007-06-17 20:36:29 +00:00
|
|
|
restore_error_handler();
|
2006-08-15 00:53:24 +00:00
|
|
|
|
2006-08-15 20:19:16 +00:00
|
|
|
$tokens = array();
|
|
|
|
$this->tokenizeDOM(
|
2007-06-17 20:36:29 +00:00
|
|
|
$doc->getElementsByTagName('html')->item(0)-> // <html>
|
|
|
|
getElementsByTagName('body')->item(0)-> // <body>
|
|
|
|
getElementsByTagName('div')->item(0) // <div>
|
2006-08-15 20:19:16 +00:00
|
|
|
, $tokens);
|
|
|
|
return $tokens;
|
2006-07-22 13:50:05 +00:00
|
|
|
}
|
|
|
|
|
2006-07-23 18:56:00 +00:00
|
|
|
/**
|
|
|
|
* Recursive function that tokenizes a node, putting it into an accumulator.
|
|
|
|
*
|
|
|
|
* @param $node DOMNode to be tokenized.
|
|
|
|
* @param $tokens Array-list of already tokenized tokens.
|
|
|
|
* @param $collect Says whether or start and close are collected, set to
|
|
|
|
* false at first recursion because it's the implicit DIV
|
|
|
|
* tag you're dealing with.
|
|
|
|
* @returns Tokens of node appended to previously passed tokens.
|
|
|
|
*/
|
2006-08-15 20:19:16 +00:00
|
|
|
protected function tokenizeDOM($node, &$tokens, $collect = false) {
|
2006-07-22 13:50:05 +00:00
|
|
|
|
2006-09-01 14:44:50 +00:00
|
|
|
// intercept non element nodes. WE MUST catch all of them,
|
|
|
|
// but we're not getting the character reference nodes because
|
|
|
|
// those should have been preprocessed
|
2007-06-21 14:44:26 +00:00
|
|
|
if ($node->nodeType === XML_TEXT_NODE) {
|
2006-09-01 14:44:50 +00:00
|
|
|
$tokens[] = $this->factory->createText($node->data);
|
|
|
|
return;
|
2007-06-21 14:44:26 +00:00
|
|
|
} elseif ($node->nodeType === XML_CDATA_SECTION_NODE) {
|
|
|
|
// undo DOM's special treatment of <script> tags
|
|
|
|
$tokens[] = $this->factory->createText($this->parseData($node->data));
|
|
|
|
return;
|
2006-09-01 14:44:50 +00:00
|
|
|
} elseif ($node->nodeType === XML_COMMENT_NODE) {
|
|
|
|
$tokens[] = $this->factory->createComment($node->data);
|
2006-08-15 20:19:16 +00:00
|
|
|
return;
|
2007-01-16 22:22:08 +00:00
|
|
|
} elseif (
|
|
|
|
// not-well tested: there may be other nodes we have to grab
|
|
|
|
$node->nodeType !== XML_ELEMENT_NODE
|
|
|
|
) {
|
|
|
|
return;
|
2006-07-22 13:50:05 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 16:17:56 +00:00
|
|
|
$attr = $node->hasAttributes() ?
|
|
|
|
$this->transformAttrToAssoc($node->attributes) :
|
|
|
|
array();
|
|
|
|
|
2006-07-22 13:50:05 +00:00
|
|
|
// We still have to make sure that the element actually IS empty
|
2006-08-15 21:19:45 +00:00
|
|
|
if (!$node->childNodes->length) {
|
2006-07-22 13:50:05 +00:00
|
|
|
if ($collect) {
|
2006-09-01 16:17:56 +00:00
|
|
|
$tokens[] = $this->factory->createEmpty($node->tagName, $attr);
|
2006-07-22 13:50:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($collect) { // don't wrap on first iteration
|
2006-08-15 20:19:16 +00:00
|
|
|
$tokens[] = $this->factory->createStart(
|
2006-07-22 13:50:05 +00:00
|
|
|
$tag_name = $node->tagName, // somehow, it get's dropped
|
2006-09-01 16:17:56 +00:00
|
|
|
$attr
|
2006-07-22 13:50:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
foreach ($node->childNodes as $node) {
|
|
|
|
// remember, it's an accumulator. Otherwise, we'd have
|
|
|
|
// to use array_merge
|
2006-08-15 20:19:16 +00:00
|
|
|
$this->tokenizeDOM($node, $tokens, true);
|
2006-07-22 13:50:05 +00:00
|
|
|
}
|
|
|
|
if ($collect) {
|
2006-08-15 20:19:16 +00:00
|
|
|
$tokens[] = $this->factory->createEnd($tag_name);
|
2006-07-22 13:50:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-07-23 18:56:00 +00:00
|
|
|
/**
|
|
|
|
* Converts a DOMNamedNodeMap of DOMAttr objects into an assoc array.
|
|
|
|
*
|
|
|
|
* @param $attribute_list DOMNamedNodeMap of DOMAttr objects.
|
|
|
|
* @returns Associative array of attributes.
|
|
|
|
*/
|
2006-08-15 21:19:45 +00:00
|
|
|
protected function transformAttrToAssoc($node_map) {
|
|
|
|
// NamedNodeMap is documented very well, so we're using undocumented
|
|
|
|
// features, namely, the fact that it implements Iterator and
|
|
|
|
// has a ->length attribute
|
|
|
|
if ($node_map->length === 0) return array();
|
|
|
|
$array = array();
|
|
|
|
foreach ($node_map as $attr) {
|
|
|
|
$array[$attr->name] = $attr->value;
|
2006-07-22 13:50:05 +00:00
|
|
|
}
|
2006-08-15 21:19:45 +00:00
|
|
|
return $array;
|
2006-07-22 13:50:05 +00:00
|
|
|
}
|
|
|
|
|
2007-06-17 20:36:29 +00:00
|
|
|
/**
|
|
|
|
* An error handler that mutes all errors
|
|
|
|
*/
|
|
|
|
public function muteErrorHandler($errno, $errstr) {}
|
|
|
|
|
2007-06-27 16:40:18 +00:00
|
|
|
/**
|
|
|
|
* Callback function for undoing escaping of stray angled brackets
|
|
|
|
* in comments
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function callbackUndoCommentSubst($matches) {
|
2007-06-27 16:40:18 +00:00
|
|
|
return '<!--' . strtr($matches[1], array('&'=>'&','<'=>'<')) . $matches[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function that entity-izes ampersands in comments so that
|
|
|
|
* callbackUndoCommentSubst doesn't clobber them
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function callbackArmorCommentEntities($matches) {
|
2007-06-27 16:40:18 +00:00
|
|
|
return '<!--' . str_replace('&', '&', $matches[1]) . $matches[2];
|
|
|
|
}
|
|
|
|
|
2007-08-19 18:49:35 +00:00
|
|
|
/**
|
|
|
|
* Wraps an HTML fragment in the necessary HTML
|
|
|
|
*/
|
2008-01-05 00:10:43 +00:00
|
|
|
protected function wrapHTML($html, $config, $context) {
|
2007-08-19 18:49:35 +00:00
|
|
|
$def = $config->getDefinition('HTML');
|
|
|
|
$ret = '';
|
|
|
|
|
|
|
|
if (!empty($def->doctype->dtdPublic) || !empty($def->doctype->dtdSystem)) {
|
|
|
|
$ret .= '<!DOCTYPE html ';
|
|
|
|
if (!empty($def->doctype->dtdPublic)) $ret .= 'PUBLIC "' . $def->doctype->dtdPublic . '" ';
|
|
|
|
if (!empty($def->doctype->dtdSystem)) $ret .= '"' . $def->doctype->dtdSystem . '" ';
|
|
|
|
$ret .= '>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret .= '<html><head>';
|
|
|
|
$ret .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
|
2007-12-12 03:29:12 +00:00
|
|
|
// No protection if $html contains a stray </div>!
|
2007-08-19 18:49:35 +00:00
|
|
|
$ret .= '</head><body><div>'.$html.'</div></body></html>';
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2006-07-22 13:50:05 +00:00
|
|
|
}
|
|
|
|
|