3 require_once 'HTMLPurifier/Lexer.php';
4 require_once 'HTMLPurifier/TokenFactory.php';
7 * Parser that uses PHP 5's DOM extension (part of the core).
9 * In PHP 5, the DOM XML extension was revamped into DOM and added to the core.
10 * It gives us a forgiving HTML parser, which we use to transform the HTML
11 * into a DOM, and then into the tokens. It is blazingly fast (for large
12 * documents, it performs twenty times faster than
13 * HTMLPurifier_Lexer_DirectLex,and is the default choice for PHP 5.
15 * @note Any empty elements will have empty tokens associated with them, even if
16 * this is prohibited by the spec. This is cannot be fixed until the spec
19 * @note PHP's DOM extension does not actually parse any entities, we use
20 * our own function to do that.
22 * @warning DOM tends to drop whitespace, which may wreak havoc on indenting.
23 * If this is a huge problem, due to the fact that HTML is hand
24 * edited and you are unable to get a parser cache that caches the
25 * the output of HTML Purifier while keeping the original HTML lying
26 * around, you may want to run Tidy on the resulting output or use
27 * HTMLPurifier_DirectLex
30 class HTMLPurifier_Lexer_DOMLex
extends HTMLPurifier_Lexer
35 public function __construct() {
37 parent
::HTMLPurifier_Lexer();
38 $this->factory
= new HTMLPurifier_TokenFactory();
41 public function tokenizeHTML($string, $config, &$context) {
43 $string = $this->normalize($string, $config, $context);
45 // preprocess string, essential for UTF-8
48 'PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
49 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.
51 '<meta http-equiv="Content-Type" content="text/html;'.
53 '</head><body><div>'.$string.'</div></body></html>';
55 $doc = new DOMDocument();
56 $doc->encoding
= 'UTF-8'; // technically does nothing, but whatever
58 // DOM will toss errors if the HTML its parsing has really big
59 // problems, so we're going to mute them. This can cause problems
60 // if a custom error handler that doesn't implement error_reporting
61 // is set, as noted by a Drupal plugin of HTML Purifier. Consider
62 // making our own error reporter to temporarily load in
63 @$doc->loadHTML($string);
67 $doc->getElementsByTagName('html')->item(0)-> // html
68 getElementsByTagName('body')->item(0)-> // body
69 getElementsByTagName('div')->item(0) // div
75 * Recursive function that tokenizes a node, putting it into an accumulator.
77 * @param $node DOMNode to be tokenized.
78 * @param $tokens Array-list of already tokenized tokens.
79 * @param $collect Says whether or start and close are collected, set to
80 * false at first recursion because it's the implicit DIV
81 * tag you're dealing with.
82 * @returns Tokens of node appended to previously passed tokens.
84 protected function tokenizeDOM($node, &$tokens, $collect = false) {
85 // recursive goodness!
87 // intercept non element nodes. WE MUST catch all of them,
88 // but we're not getting the character reference nodes because
89 // those should have been preprocessed
90 if ($node->nodeType
=== XML_TEXT_NODE ||
91 $node->nodeType
=== XML_CDATA_SECTION_NODE
) {
92 $tokens[] = $this->factory
->createText($node->data
);
94 } elseif ($node->nodeType
=== XML_COMMENT_NODE
) {
95 $tokens[] = $this->factory
->createComment($node->data
);
98 // not-well tested: there may be other nodes we have to grab
99 $node->nodeType
!== XML_ELEMENT_NODE
104 $attr = $node->hasAttributes() ?
105 $this->transformAttrToAssoc($node->attributes
) :
108 // We still have to make sure that the element actually IS empty
109 if (!$node->childNodes
->length
) {
111 $tokens[] = $this->factory
->createEmpty($node->tagName
, $attr);
114 if ($collect) { // don't wrap on first iteration
115 $tokens[] = $this->factory
->createStart(
116 $tag_name = $node->tagName
, // somehow, it get's dropped
120 foreach ($node->childNodes
as $node) {
121 // remember, it's an accumulator. Otherwise, we'd have
122 // to use array_merge
123 $this->tokenizeDOM($node, $tokens, true);
126 $tokens[] = $this->factory
->createEnd($tag_name);
133 * Converts a DOMNamedNodeMap of DOMAttr objects into an assoc array.
135 * @param $attribute_list DOMNamedNodeMap of DOMAttr objects.
136 * @returns Associative array of attributes.
138 protected function transformAttrToAssoc($node_map) {
139 // NamedNodeMap is documented very well, so we're using undocumented
140 // features, namely, the fact that it implements Iterator and
141 // has a ->length attribute
142 if ($node_map->length
=== 0) return array();
144 foreach ($node_map as $attr) {
145 $array[$attr->name
] = $attr->value
;