ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
HTMLPurifier_Lexer_DOMLex Class Reference

Parser that uses PHP 5's DOM extension (part of the core). More...

+ Inheritance diagram for HTMLPurifier_Lexer_DOMLex:
+ Collaboration diagram for HTMLPurifier_Lexer_DOMLex:

Public Member Functions

 __construct ()
 tokenizeHTML ($html, $config, $context)
 Lexes an HTML string into tokens.
 muteErrorHandler ($errno, $errstr)
 An error handler that mutes all errors.
 callbackUndoCommentSubst ($matches)
 Callback function for undoing escaping of stray angled brackets in comments.
 callbackArmorCommentEntities ($matches)
 Callback function that entity-izes ampersands in comments so that callbackUndoCommentSubst doesn't clobber them.
- Public Member Functions inherited from HTMLPurifier_Lexer
 parseData ($string)
 Parses special entities into the proper characters.
 normalize ($html, $config, $context)
 Takes a piece of HTML and normalizes it by converting entities, fixing encoding, extracting bits, and other good stuff.
 extractBody ($html)
 Takes a string of HTML (fragment or document) and returns the content.

Protected Member Functions

 tokenizeDOM ($node, &$tokens, $collect=false)
 Recursive function that tokenizes a node, putting it into an accumulator.
 transformAttrToAssoc ($node_map)
 Converts a DOMNamedNodeMap of DOMAttr objects into an assoc array.
 wrapHTML ($html, $config, $context)
 Wraps an HTML fragment in the necessary HTML.

Private Attributes

 $factory

Additional Inherited Members

- Static Public Member Functions inherited from HTMLPurifier_Lexer
static create ($config)
 Retrieves or sets the default Lexer as a Prototype Factory.
- Data Fields inherited from HTMLPurifier_Lexer
 $tracksLineNumbers = false
 Whether or not this lexer implements line-number/column-number tracking.
- Static Protected Member Functions inherited from HTMLPurifier_Lexer
static escapeCDATA ($string)
 Translates CDATA sections into regular sections (through escaping).
static escapeCommentedCDATA ($string)
 Special CDATA case that is especially convoluted for <script>
static CDATACallback ($matches)
 Callback function for escapeCDATA() that does the work.
- Protected Attributes inherited from HTMLPurifier_Lexer
 $_special_entity2str
 Most common entity to raw value conversion table for special entities.

Detailed Description

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.

Note
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.
PHP's DOM extension does not actually parse any entities, we use our own function to do that.
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 edited and you are unable to get a parser cache that caches the 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

Definition at line 27 of file DOMLex.php.

Constructor & Destructor Documentation

HTMLPurifier_Lexer_DOMLex::__construct ( )

Reimplemented from HTMLPurifier_Lexer.

Definition at line 32 of file DOMLex.php.

{
// setup the factory
$this->factory = new HTMLPurifier_TokenFactory();
}

Member Function Documentation

HTMLPurifier_Lexer_DOMLex::callbackArmorCommentEntities (   $matches)

Callback function that entity-izes ampersands in comments so that callbackUndoCommentSubst doesn't clobber them.

Definition at line 186 of file DOMLex.php.

{
return '<!--' . str_replace('&', '&amp;', $matches[1]) . $matches[2];
}
HTMLPurifier_Lexer_DOMLex::callbackUndoCommentSubst (   $matches)

Callback function for undoing escaping of stray angled brackets in comments.

Definition at line 178 of file DOMLex.php.

{
return '<!--' . strtr($matches[1], array('&amp;'=>'&','&lt;'=>'<')) . $matches[2];
}
HTMLPurifier_Lexer_DOMLex::muteErrorHandler (   $errno,
  $errstr 
)

An error handler that mutes all errors.

Definition at line 172 of file DOMLex.php.

{}
HTMLPurifier_Lexer_DOMLex::tokenizeDOM (   $node,
$tokens,
  $collect = false 
)
protected

Recursive function that tokenizes a node, putting it into an accumulator.

Parameters
$nodeDOMNode to be tokenized.
$tokensArray-list of already tokenized tokens.
$collectSays 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.

Definition at line 84 of file DOMLex.php.

References $data, HTMLPurifier_Lexer\parseData(), and transformAttrToAssoc().

Referenced by HTMLPurifier_Lexer_PH5P\tokenizeHTML(), and tokenizeHTML().

{
// 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
if ($node->nodeType === XML_TEXT_NODE) {
$tokens[] = $this->factory->createText($node->data);
return;
} elseif ($node->nodeType === XML_CDATA_SECTION_NODE) {
// undo libxml's special treatment of <script> and <style> tags
$last = end($tokens);
$data = $node->data;
// (note $node->tagname is already normalized)
if ($last instanceof HTMLPurifier_Token_Start && ($last->name == 'script' || $last->name == 'style')) {
$new_data = trim($data);
if (substr($new_data, 0, 4) === '<!--') {
$data = substr($new_data, 4);
if (substr($data, -3) === '-->') {
$data = substr($data, 0, -3);
} else {
// Highly suspicious! Not sure what to do...
}
}
}
$tokens[] = $this->factory->createText($this->parseData($data));
return;
} elseif ($node->nodeType === XML_COMMENT_NODE) {
// this is code is only invoked for comments in script/style in versions
// of libxml pre-2.6.28 (regular comments, of course, are still
// handled regularly)
$tokens[] = $this->factory->createComment($node->data);
return;
} elseif (
// not-well tested: there may be other nodes we have to grab
$node->nodeType !== XML_ELEMENT_NODE
) {
return;
}
$attr = $node->hasAttributes() ?
$this->transformAttrToAssoc($node->attributes) :
array();
// We still have to make sure that the element actually IS empty
if (!$node->childNodes->length) {
if ($collect) {
$tokens[] = $this->factory->createEmpty($node->tagName, $attr);
}
} else {
if ($collect) { // don't wrap on first iteration
$tokens[] = $this->factory->createStart(
$tag_name = $node->tagName, // somehow, it get's dropped
$attr
);
}
foreach ($node->childNodes as $node) {
// remember, it's an accumulator. Otherwise, we'd have
// to use array_merge
$this->tokenizeDOM($node, $tokens, true);
}
if ($collect) {
$tokens[] = $this->factory->createEnd($tag_name);
}
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

HTMLPurifier_Lexer_DOMLex::tokenizeHTML (   $string,
  $config,
  $context 
)

Lexes an HTML string into tokens.

Parameters
$stringString HTML.
Returns
HTMLPurifier_Token array representation of HTML.

Reimplemented from HTMLPurifier_Lexer.

Reimplemented in HTMLPurifier_Lexer_PH5P.

Definition at line 38 of file DOMLex.php.

References $comment, $config, HTMLPurifier_Lexer\normalize(), tokenizeDOM(), and wrapHTML().

{
$html = $this->normalize($html, $config, $context);
// 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";
$html = preg_replace_callback($comment, array($this, 'callbackArmorCommentEntities'), $html);
do {
$old = $html;
$html = preg_replace("/<($char)/i", '&lt;\\1', $html);
} while ($html !== $old);
$html = preg_replace_callback($comment, array($this, 'callbackUndoCommentSubst'), $html); // fix comments
}
// preprocess html, essential for UTF-8
$html = $this->wrapHTML($html, $config, $context);
$doc = new DOMDocument();
$doc->encoding = 'UTF-8'; // theoretically, the above has this covered
set_error_handler(array($this, 'muteErrorHandler'));
$doc->loadHTML($html);
restore_error_handler();
$tokens = array();
$this->tokenizeDOM(
$doc->getElementsByTagName('html')->item(0)-> // <html>
getElementsByTagName('body')->item(0)-> // <body>
getElementsByTagName('div')->item(0) // <div>
, $tokens);
return $tokens;
}

+ Here is the call graph for this function:

HTMLPurifier_Lexer_DOMLex::transformAttrToAssoc (   $node_map)
protected

Converts a DOMNamedNodeMap of DOMAttr objects into an assoc array.

Parameters
$attribute_listDOMNamedNodeMap of DOMAttr objects.
Returns
Associative array of attributes.

Definition at line 157 of file DOMLex.php.

Referenced by tokenizeDOM().

{
// 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;
}
return $array;
}

+ Here is the caller graph for this function:

HTMLPurifier_Lexer_DOMLex::wrapHTML (   $html,
  $config,
  $context 
)
protected

Wraps an HTML fragment in the necessary HTML.

Definition at line 193 of file DOMLex.php.

References $config, and $ret.

Referenced by HTMLPurifier_Lexer_PH5P\tokenizeHTML(), and tokenizeHTML().

{
$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" />';
// No protection if $html contains a stray </div>!
$ret .= '</head><body><div>'.$html.'</div></body></html>';
return $ret;
}

+ Here is the caller graph for this function:

Field Documentation

HTMLPurifier_Lexer_DOMLex::$factory
private

Definition at line 30 of file DOMLex.php.


The documentation for this class was generated from the following file: