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.
|
|
|
|
*
|
|
|
|
* @notice
|
|
|
|
* Any empty elements will have empty tokens associated with them, even if
|
|
|
|
* this is prohibited by the spec. This is cannot be fixed until the spec
|
|
|
|
* comes into play.
|
|
|
|
*
|
|
|
|
* @todo Determine DOM's entity parsing behavior, point to local entity files
|
|
|
|
* if necessary.
|
|
|
|
* @todo Make div access less fragile, and refrain from preprocessing when
|
|
|
|
* HTML tag and friends are already present.
|
|
|
|
*/
|
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-08-15 00:31:12 +00:00
|
|
|
public function tokenizeHTML($string, $config = null) {
|
|
|
|
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
|
|
|
|
2006-08-29 20:05:26 +00:00
|
|
|
$string = $this->normalize($string, $config);
|
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();
|
|
|
|
$doc->encoding = 'UTF-8'; // technically does nothing, but whatever
|
2006-07-22 13:50:05 +00:00
|
|
|
@$doc->loadHTML($string); // mute all errors, handle it transparently
|
2006-08-15 00:53:24 +00:00
|
|
|
|
2006-08-15 20:19:16 +00:00
|
|
|
$tokens = array();
|
|
|
|
$this->tokenizeDOM(
|
2006-08-19 16:24:17 +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
|
|
|
// recursive goodness!
|
|
|
|
|
|
|
|
// intercept non element nodes
|
|
|
|
|
2006-08-15 21:19:45 +00:00
|
|
|
if ( isset($node->data) ) {
|
|
|
|
if ($node->nodeType === XML_TEXT_NODE ||
|
|
|
|
$node->nodeType === XML_CDATA_SECTION_NODE) {
|
2006-08-15 20:19:16 +00:00
|
|
|
$tokens[] = $this->factory->createText($node->data);
|
2006-08-15 21:19:45 +00:00
|
|
|
} elseif ($node->nodeType === XML_COMMENT_NODE) {
|
|
|
|
$tokens[] = $this->factory->createComment($node->data);
|
2006-07-22 13:50:05 +00:00
|
|
|
}
|
|
|
|
// quite possibly, the object wasn't handled, that's fine
|
2006-08-15 20:19:16 +00:00
|
|
|
return;
|
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-08-15 20:19:16 +00:00
|
|
|
$tokens[] = $this->factory->createEmpty(
|
2006-07-22 13:50:05 +00:00
|
|
|
$node->tagName,
|
|
|
|
$this->transformAttrToAssoc($node->attributes)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} 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
|
|
|
|
$this->transformAttrToAssoc($node->attributes)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|