Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / Lexer / PEARSax3.php
blob3888229b075b154ca3a0ed7a7d2fc0c75f2aeae5
1 <?php
3 require_once 'XML/HTMLSax3.php'; // PEAR
4 require_once 'HTMLPurifier/Lexer.php';
6 /**
7 * Proof-of-concept lexer that uses the PEAR package XML_HTMLSax3 to parse HTML.
8 *
9 * PEAR, not suprisingly, also has a SAX parser for HTML. I don't know
10 * very much about implementation, but it's fairly well written. However, that
11 * abstraction comes at a price: performance. You need to have it installed,
12 * and if the API changes, it might break our adapter. Not sure whether or not
13 * it's UTF-8 aware, but it has some entity parsing trouble (in all areas,
14 * text and attributes).
16 * Quite personally, I don't recommend using the PEAR class, and the defaults
17 * don't use it. The unit tests do perform the tests on the SAX parser too, but
18 * whatever it does for poorly formed HTML is up to it.
20 * @todo Generalize so that XML_HTMLSax is also supported.
22 * @warning Entity-resolution inside attributes is broken.
25 class HTMLPurifier_Lexer_PEARSax3 extends HTMLPurifier_Lexer
28 /**
29 * Internal accumulator array for SAX parsers.
30 * @protected
32 var $tokens = array();
34 function tokenizeHTML($string, $config, &$context) {
36 $this->tokens = array();
38 $string = $this->normalize($string, $config, $context);
40 $parser = new XML_HTMLSax3();
41 $parser->set_object($this);
42 $parser->set_element_handler('openHandler','closeHandler');
43 $parser->set_data_handler('dataHandler');
44 $parser->set_escape_handler('escapeHandler');
46 // doesn't seem to work correctly for attributes
47 $parser->set_option('XML_OPTION_ENTITIES_PARSED', 1);
49 $parser->parse($string);
51 return $this->tokens;
55 /**
56 * Open tag event handler, interface is defined by PEAR package.
58 function openHandler(&$parser, $name, $attrs, $closed) {
59 // entities are not resolved in attrs
60 foreach ($attrs as $key => $attr) {
61 $attrs[$key] = $this->parseData($attr);
63 if ($closed) {
64 $this->tokens[] = new HTMLPurifier_Token_Empty($name, $attrs);
65 } else {
66 $this->tokens[] = new HTMLPurifier_Token_Start($name, $attrs);
68 return true;
71 /**
72 * Close tag event handler, interface is defined by PEAR package.
74 function closeHandler(&$parser, $name) {
75 // HTMLSax3 seems to always send empty tags an extra close tag
76 // check and ignore if you see it:
77 // [TESTME] to make sure it doesn't overreach
78 if ($this->tokens[count($this->tokens)-1]->type == 'empty') {
79 return true;
81 $this->tokens[] = new HTMLPurifier_Token_End($name);
82 return true;
85 /**
86 * Data event handler, interface is defined by PEAR package.
88 function dataHandler(&$parser, $data) {
89 $this->tokens[] = new HTMLPurifier_Token_Text($data);
90 return true;
93 /**
94 * Escaped text handler, interface is defined by PEAR package.
96 function escapeHandler(&$parser, $data) {
97 if (strpos($data, '--') === 0) {
98 $this->tokens[] = new HTMLPurifier_Token_Comment($data);
100 // CDATA is handled elsewhere, but if it was handled here:
101 //if (strpos($data, '[CDATA[') === 0) {
102 // $this->tokens[] = new HTMLPurifier_Token_Text(
103 // substr($data, 7, strlen($data) - 9) );
105 return true;