HTMLPurifier_Lexer::create PHP Method

create() public static method

By default HTMLPurifier_Lexer_DOMLex will be returned. There are a few exceptions involving special features that only DirectLex implements.
public static create ( HTMLPurifier_Config $config ) : HTMLPurifier_Lexer
$config HTMLPurifier_Config
return HTMLPurifier_Lexer
    public static function create($config)
    {
        if (!$config instanceof HTMLPurifier_Config) {
            $lexer = $config;
            trigger_error("Passing a prototype to\n                HTMLPurifier_Lexer::create() is deprecated, please instead\n                use %Core.LexerImpl", E_USER_WARNING);
        } else {
            $lexer = $config->get('Core.LexerImpl');
        }
        $needs_tracking = $config->get('Core.MaintainLineNumbers') || $config->get('Core.CollectErrors');
        $inst = null;
        if (is_object($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') && !extension_loaded('domxml')) {
                        // check for DOM support, because while it's part of the
                        // core, it can be disabled compile time. Also, the PECL
                        // domxml extension overrides the default DOM, and is evil
                        // and nasty and we shan't bother to support it
                        $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 (!$inst) {
            throw new HTMLPurifier_Exception('No lexer was instantiated');
        }
        // 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;
    }

Usage Example

Beispiel #1
0
 function test_create_incompatibleLexer()
 {
     $this->config->set('Core.LexerImpl', 'DOMLex');
     $this->config->set('Core.MaintainLineNumbers', true);
     $this->expectException(new HTMLPurifier_Exception('Cannot use lexer that does not support line numbers with Core.MaintainLineNumbers or Core.CollectErrors (use DirectLex instead)'));
     HTMLPurifier_Lexer::create($this->config);
 }
All Usage Examples Of HTMLPurifier_Lexer::create