mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-11 01:36:20 +02:00
Refactor lexer instantiation logic with exceptions and forced line tracking.
Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
This commit is contained in:
@ -42,6 +42,12 @@
|
||||
class HTMLPurifier_Lexer
|
||||
{
|
||||
|
||||
/**
|
||||
* Whether or not this lexer implements line-number/column-number tracking.
|
||||
* If it does, set to true.
|
||||
*/
|
||||
public $tracksLineNumbers = false;
|
||||
|
||||
// -- STATIC ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
@ -70,46 +76,59 @@ class HTMLPurifier_Lexer
|
||||
$lexer = $config->get('Core', 'LexerImpl');
|
||||
}
|
||||
|
||||
$needs_tracking =
|
||||
$config->get('Core', 'MaintainLineNumbers') ||
|
||||
$config->get('Core', 'CollectErrors');
|
||||
|
||||
$inst = null;
|
||||
if (is_object($lexer)) {
|
||||
return $lexer;
|
||||
$inst = $lexer;
|
||||
} else {
|
||||
|
||||
if (is_null($lexer)) { do {
|
||||
// auto-detection algorithm
|
||||
|
||||
if ($needs_tracking) {
|
||||
$lexer = 'DirectLex';
|
||||
break;
|
||||
}
|
||||
|
||||
if (class_exists('DOMDocument') && method_exists('DOMDocument', 'loadHTML')) {
|
||||
// check for DOM support, because, surprisingly enough,
|
||||
// it's *not* part of the core!
|
||||
$lexer = 'DOMLex';
|
||||
} else {
|
||||
$lexer = 'DirectLex';
|
||||
}
|
||||
|
||||
} while(0); } // do..while so we can break
|
||||
|
||||
// instantiate recognized string names
|
||||
switch ($lexer) {
|
||||
case 'DOMLex':
|
||||
$inst = new HTMLPurifier_Lexer_DOMLex();
|
||||
break;
|
||||
case 'DirectLex':
|
||||
$inst = new HTMLPurifier_Lexer_DirectLex();
|
||||
break;
|
||||
case 'PH5P':
|
||||
$inst = new HTMLPurifier_Lexer_PH5P();
|
||||
break;
|
||||
default:
|
||||
throw new HTMLPurifier_Exception("Cannot instantiate unrecognized Lexer type " . htmlspecialchars($lexer));
|
||||
}
|
||||
}
|
||||
|
||||
if (is_null($lexer)) { do {
|
||||
// auto-detection algorithm
|
||||
|
||||
// once PHP DOM implements native line numbers, or we
|
||||
// hack out something using XSLT, remove this stipulation
|
||||
$line_numbers = $config->get('Core', 'MaintainLineNumbers');
|
||||
if (
|
||||
$line_numbers === true ||
|
||||
($line_numbers === null && $config->get('Core', 'CollectErrors'))
|
||||
) {
|
||||
$lexer = 'DirectLex';
|
||||
break;
|
||||
}
|
||||
|
||||
if (class_exists('DOMDocument') && method_exists('DOMDocument', 'loadHTML')) {
|
||||
// check for DOM support, because, surprisingly enough,
|
||||
// it's *not* part of the core!
|
||||
$lexer = 'DOMLex';
|
||||
} else {
|
||||
$lexer = 'DirectLex';
|
||||
}
|
||||
|
||||
} while(0); } // do..while so we can break
|
||||
if (!$inst) throw new HTMLPurifier_Exception('No lexer was instantiated');
|
||||
|
||||
// instantiate recognized string names
|
||||
switch ($lexer) {
|
||||
case 'DOMLex':
|
||||
return new HTMLPurifier_Lexer_DOMLex();
|
||||
case 'DirectLex':
|
||||
return new HTMLPurifier_Lexer_DirectLex();
|
||||
case 'PH5P':
|
||||
return new HTMLPurifier_Lexer_PH5P();
|
||||
default:
|
||||
trigger_error("Cannot instantiate unrecognized Lexer type " . htmlspecialchars($lexer), E_USER_ERROR);
|
||||
// once PHP DOM implements native line numbers, or we
|
||||
// hack out something using XSLT, remove this stipulation
|
||||
if ($needs_tracking && !$inst->tracksLineNumbers) {
|
||||
throw new HTMLPurifier_Exception('Cannot use lexer that does not support line numbers with Core.MaintainLineNumbers or Core.CollectErrors (use DirectLex instead)');
|
||||
}
|
||||
|
||||
return $inst;
|
||||
|
||||
}
|
||||
|
||||
// -- CONVENIENCE MEMBERS ---------------------------------------------
|
||||
|
@ -13,6 +13,8 @@
|
||||
class HTMLPurifier_Lexer_DirectLex extends HTMLPurifier_Lexer
|
||||
{
|
||||
|
||||
public $tracksLineNumbers = true;
|
||||
|
||||
/**
|
||||
* Whitespace characters for str(c)spn.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user