Add support for full document parsing, aka discard everything that's not in-between...
[htmlpurifier.git] / library / HTMLPurifier / Lexer / DOMLex.php
blob0df13ae582275a8552414a1f2b02745bd19dddda
1 <?php
3 require_once 'HTMLPurifier/Lexer.php';
5 /**
6 * Parser that uses PHP 5's DOM extension (part of the core).
7 *
8 * In PHP 5, the DOM XML extension was revamped into DOM and added to the core.
9 * It gives us a forgiving HTML parser, which we use to transform the HTML
10 * into a DOM, and then into the tokens. It is blazingly fast (for large
11 * documents, it performs twenty times faster than
12 * HTMLPurifier_Lexer_DirectLex,and is the default choice for PHP 5.
14 * @notice
15 * 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
17 * comes into play.
19 * @todo Determine DOM's entity parsing behavior, point to local entity files
20 * if necessary.
21 * @todo Make div access less fragile, and refrain from preprocessing when
22 * HTML tag and friends are already present.
25 class HTMLPurifier_Lexer_DOMLex extends HTMLPurifier_Lexer
28 public function tokenizeHTML($string, $config = null) {
29 if (!$config) $config = HTMLPurifier_Config::createDefault();
31 if ($config->get('Core', 'AcceptFullDocuments')) {
32 $is_full = $this->extractBody($string, true);
35 $doc = new DOMDocument();
36 $doc->encoding = 'UTF-8'; // technically does nothing, but whatever
38 // replace and escape the CDATA sections, since parsing under HTML
39 // mode won't get 'em.
40 $string = $this->escapeCDATA($string);
42 if (!$is_full) {
43 // preprocess string, essential for UTF-8
44 $string =
45 '<html><head>'.
46 '<meta http-equiv="Content-Type" content="text/html;'.
47 ' charset=utf-8" />'.
48 '</head><body>'.$string.'</body></html>';
51 @$doc->loadHTML($string); // mute all errors, handle it transparently
53 return $this->tokenizeDOM(
54 $doc->childNodes->item(1)-> // html
55 getElementsByTagName('body')->item(0) // body
59 /**
60 * Recursive function that tokenizes a node, putting it into an accumulator.
62 * @param $node DOMNode to be tokenized.
63 * @param $tokens Array-list of already tokenized tokens.
64 * @param $collect Says whether or start and close are collected, set to
65 * false at first recursion because it's the implicit DIV
66 * tag you're dealing with.
67 * @returns Tokens of node appended to previously passed tokens.
69 protected function tokenizeDOM($node, $tokens = array(), $collect = false) {
70 // recursive goodness!
72 // intercept non element nodes
74 if ( !($node instanceof DOMElement) ) {
75 if ($node instanceof DOMComment) {
76 $tokens[] = new HTMLPurifier_Token_Comment($node->data);
77 } elseif ($node instanceof DOMText ||
78 $node instanceof DOMCharacterData) {
79 $tokens[] = new HTMLPurifier_Token_Text($node->data);
81 // quite possibly, the object wasn't handled, that's fine
82 return $tokens;
85 // We still have to make sure that the element actually IS empty
86 if (!$node->hasChildNodes()) {
87 if ($collect) {
88 $tokens[] = new HTMLPurifier_Token_Empty(
89 $node->tagName,
90 $this->transformAttrToAssoc($node->attributes)
93 } else {
94 if ($collect) { // don't wrap on first iteration
95 $tokens[] = new HTMLPurifier_Token_Start(
96 $tag_name = $node->tagName, // somehow, it get's dropped
97 $this->transformAttrToAssoc($node->attributes)
100 foreach ($node->childNodes as $node) {
101 // remember, it's an accumulator. Otherwise, we'd have
102 // to use array_merge
103 $tokens = $this->tokenizeDOM($node, $tokens, true);
105 if ($collect) {
106 $tokens[] = new HTMLPurifier_Token_End($tag_name);
110 return $tokens;
115 * Converts a DOMNamedNodeMap of DOMAttr objects into an assoc array.
117 * @param $attribute_list DOMNamedNodeMap of DOMAttr objects.
118 * @returns Associative array of attributes.
120 protected function transformAttrToAssoc($attribute_list) {
121 $attribute_array = array();
122 // undocumented behavior
123 foreach ($attribute_list as $key => $attr) {
124 $attribute_array[$key] = $attr->value;
126 return $attribute_array;