2006-07-22 13:50:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Lexer.php';
|
2006-08-15 20:19:16 +00:00
|
|
|
require_once 'HTMLPurifier/TokenFactory.php';
|
2006-07-22 13:50:05 +00:00
|
|
|
|
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
|
2006-08-29 19:36:40 +00:00
|
|
|
parent::HTMLPurifier_Lexer();
|
2006-08-15 20:19:16 +00:00
|
|
|
$this->factory = new HTMLPurifier_TokenFactory();
|
|
|
|
}
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
public function tokenizeHTML($string, $config, &$context) {
|
2006-08-15 00:31:12 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$string = $this->normalize($string, $config, $context);
|
2006-08-19 17:53:59 +00:00
|
|
|
|
2006-08-14 13:27:18 +00:00
|
|
|
// preprocess string, essential for UTF-8
|
2006-08-27 22:06:58 +00:00
|
|
|
$string =
|
2006-08-19 16:24:17 +00:00
|
|
|
'<!DOCTYPE html '.
|
|
|
|
'PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
|
|
|
|
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.
|
2006-08-15 00:53:24 +00:00
|
|
|
'<html><head>'.
|
|
|
|
'<meta http-equiv="Content-Type" content="text/html;'.
|
|
|
|
' charset=utf-8" />'.
|
2006-08-19 16:24:17 +00:00
|
|
|
'</head><body><div>'.$string.'</div></body></html>';
|
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'));
|
|
|
|
$doc->loadHTML($string);
|
|
|
|
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) {}
|
|
|
|
|
2006-07-22 13:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|