parser.php

来自「一款可以和GOOGLE媲美的开源统计系统,运用AJAX.功能强大. 无色提示:」· PHP 代码 · 共 767 行 · 第 1/2 页

PHP
767
字号
     *    @return boolean        True if this is the exit mode.     *    @access private     */    function _isSpecialMode($mode) {        return (strncmp($mode, "_", 1) == 0);    }        /**     *    Strips the magic underscore marking single token     *    modes.     *    @param string $mode    Mode to decode.     *    @return string         Underlying mode name.     *    @access private     */    function _decodeSpecial($mode) {        return substr($mode, 1);    }        /**     *    Calls the parser method named after the current     *    mode. Empty content will be ignored. The lexer     *    has a parser handler for each mode in the lexer.     *    @param string $content        Text parsed.     *    @param boolean $is_match      Token is recognised rather     *                                  than unparsed data.     *    @access private     */    function _invokeParser($content, $is_match) {        if (($content === '') || ($content === false)) {            return true;        }        $handler = $this->_mode_handlers[$this->_mode->getCurrent()];        return $this->_parser->$handler($content, $is_match);    }        /**     *    Tries to match a chunk of text and if successful     *    removes the recognised chunk and any leading     *    unparsed data. Empty strings will not be matched.     *    @param string $raw         The subject to parse. This is the     *                               content that will be eaten.     *    @return array/boolean      Three item list of unparsed     *                               content followed by the     *                               recognised token and finally the     *                               action the parser is to take.     *                               True if no match, false if there     *                               is a parsing error.     *    @access private     */    function _reduce($raw) {        if ($action = $this->_regexes[$this->_mode->getCurrent()]->match($raw, $match)) {            $unparsed_character_count = strpos($raw, $match);            $unparsed = substr($raw, 0, $unparsed_character_count);            $raw = substr($raw, $unparsed_character_count + strlen($match));            return array($raw, $unparsed, $match, $action);        }        return true;    }}/** *    Breaks HTML into SAX events. *    @package SimpleTest *    @subpackage WebTester */class SimpleHtmlLexer extends SimpleLexer {        /**     *    Sets up the lexer with case insensitive matching     *    and adds the HTML handlers.     *    @param SimpleSaxParser $parser  Handling strategy by     *                                    reference.     *    @access public     */    function SimpleHtmlLexer(&$parser) {        $this->SimpleLexer($parser, 'text');        $this->mapHandler('text', 'acceptTextToken');        $this->_addSkipping();        foreach ($this->_getParsedTags() as $tag) {            $this->_addTag($tag);        }        $this->_addInTagTokens();    }        /**     *    List of parsed tags. Others are ignored.     *    @return array        List of searched for tags.     *    @access private     */    function _getParsedTags() {        return array('a', 'base', 'title', 'form', 'input', 'button', 'textarea', 'select',                'option', 'frameset', 'frame', 'label');    }        /**     *    The lexer has to skip certain sections such     *    as server code, client code and styles.     *    @access private     */    function _addSkipping() {        $this->mapHandler('css', 'ignore');        $this->addEntryPattern('<style', 'text', 'css');        $this->addExitPattern('</style>', 'css');        $this->mapHandler('js', 'ignore');        $this->addEntryPattern('<script', 'text', 'js');        $this->addExitPattern('</script>', 'js');        $this->mapHandler('comment', 'ignore');        $this->addEntryPattern('<!--', 'text', 'comment');        $this->addExitPattern('-->', 'comment');    }        /**     *    Pattern matches to start and end a tag.     *    @param string $tag          Name of tag to scan for.     *    @access private     */    function _addTag($tag) {        $this->addSpecialPattern("</$tag>", 'text', 'acceptEndToken');        $this->addEntryPattern("<$tag", 'text', 'tag');    }        /**     *    Pattern matches to parse the inside of a tag     *    including the attributes and their quoting.     *    @access private     */    function _addInTagTokens() {        $this->mapHandler('tag', 'acceptStartToken');        $this->addSpecialPattern('\s+', 'tag', 'ignore');        $this->_addAttributeTokens();        $this->addExitPattern('/>', 'tag');        $this->addExitPattern('>', 'tag');    }        /**     *    Matches attributes that are either single quoted,     *    double quoted or unquoted.     *    @access private     */    function _addAttributeTokens() {        $this->mapHandler('dq_attribute', 'acceptAttributeToken');        $this->addEntryPattern('=\s*"', 'tag', 'dq_attribute');        $this->addPattern("\\\\\"", 'dq_attribute');        $this->addExitPattern('"', 'dq_attribute');        $this->mapHandler('sq_attribute', 'acceptAttributeToken');        $this->addEntryPattern("=\s*'", 'tag', 'sq_attribute');        $this->addPattern("\\\\'", 'sq_attribute');        $this->addExitPattern("'", 'sq_attribute');        $this->mapHandler('uq_attribute', 'acceptAttributeToken');        $this->addSpecialPattern('=\s*[^>\s]*', 'tag', 'uq_attribute');    }}/** *    Converts HTML tokens into selected SAX events. *    @package SimpleTest *    @subpackage WebTester */class SimpleHtmlSaxParser {    var $_lexer;    var $_listener;    var $_tag;    var $_attributes;    var $_current_attribute;        /**     *    Sets the listener.     *    @param SimpleSaxListener $listener    SAX event handler.     *    @access public     */    function SimpleHtmlSaxParser(&$listener) {        $this->_listener = &$listener;        $this->_lexer = &$this->createLexer($this);        $this->_tag = '';        $this->_attributes = array();        $this->_current_attribute = '';    }        /**     *    Runs the content through the lexer which     *    should call back to the acceptors.     *    @param string $raw      Page text to parse.     *    @return boolean         False if parse error.     *    @access public     */    function parse($raw) {        return $this->_lexer->parse($raw);    }        /**     *    Sets up the matching lexer. Starts in 'text' mode.     *    @param SimpleSaxParser $parser    Event generator, usually $self.     *    @return SimpleLexer               Lexer suitable for this parser.     *    @access public     *    @static     */    function &createLexer(&$parser) {        $lexer = &new SimpleHtmlLexer($parser);        return $lexer;    }        /**     *    Accepts a token from the tag mode. If the     *    starting element completes then the element     *    is dispatched and the current attributes     *    set back to empty. The element or attribute     *    name is converted to lower case.     *    @param string $token     Incoming characters.     *    @param integer $event    Lexer event type.     *    @return boolean          False if parse error.     *    @access public     */    function acceptStartToken($token, $event) {        if ($event == LEXER_ENTER) {            $this->_tag = strtolower(substr($token, 1));            return true;        }        if ($event == LEXER_EXIT) {            $success = $this->_listener->startElement(                    $this->_tag,                    $this->_attributes);            $this->_tag = '';            $this->_attributes = array();            return $success;        }        if ($token != '=') {            $this->_current_attribute = strtolower(SimpleHtmlSaxParser::decodeHtml($token));            $this->_attributes[$this->_current_attribute] = '';        }        return true;    }        /**     *    Accepts a token from the end tag mode.     *    The element name is converted to lower case.     *    @param string $token     Incoming characters.     *    @param integer $event    Lexer event type.     *    @return boolean          False if parse error.     *    @access public     */    function acceptEndToken($token, $event) {        if (! preg_match('/<\/(.*)>/', $token, $matches)) {            return false;        }        return $this->_listener->endElement(strtolower($matches[1]));    }        /**     *    Part of the tag data.     *    @param string $token     Incoming characters.     *    @param integer $event    Lexer event type.     *    @return boolean          False if parse error.     *    @access public     */    function acceptAttributeToken($token, $event) {        if ($this->_current_attribute) {            if ($event == LEXER_UNMATCHED) {                $this->_attributes[$this->_current_attribute] .=                        SimpleHtmlSaxParser::decodeHtml($token);            }            if ($event == LEXER_SPECIAL) {                $this->_attributes[$this->_current_attribute] .=                        preg_replace('/^=\s*/' , '', SimpleHtmlSaxParser::decodeHtml($token));            }        }        return true;    }        /**     *    A character entity.     *    @param string $token    Incoming characters.     *    @param integer $event   Lexer event type.     *    @return boolean         False if parse error.     *    @access public     */    function acceptEntityToken($token, $event) {    }        /**     *    Character data between tags regarded as     *    important.     *    @param string $token     Incoming characters.     *    @param integer $event    Lexer event type.     *    @return boolean          False if parse error.     *    @access public     */    function acceptTextToken($token, $event) {        return $this->_listener->addContent($token);    }        /**     *    Incoming data to be ignored.     *    @param string $token     Incoming characters.     *    @param integer $event    Lexer event type.     *    @return boolean          False if parse error.     *    @access public     */    function ignore($token, $event) {        return true;    }        /**     *    Decodes any HTML entities.     *    @param string $html    Incoming HTML.     *    @return string         Outgoing plain text.     *    @access public     *    @static     */    function decodeHtml($html) {        static $translations;        if (! isset($translations)) {            $translations = array_flip(get_html_translation_table(HTML_ENTITIES));        }        return strtr($html, $translations);    }        /**     *    Turns HTML into text browser visible text. Images     *    are converted to their alt text and tags are supressed.     *    Entities are converted to their visible representation.     *    @param string $html        HTML to convert.     *    @return string             Plain text.     *    @access public     *    @static     */    function normalise($html) {        $text = preg_replace('|<!--.*?-->|', '', $html);        $text = preg_replace('|<img.*?alt\s*=\s*"(.*?)".*?>|', ' \1 ', $text);        $text = preg_replace('|<img.*?alt\s*=\s*\'(.*?)\'.*?>|', ' \1 ', $text);        $text = preg_replace('|<img.*?alt\s*=\s*([a-zA-Z_]+).*?>|', ' \1 ', $text);        $text = preg_replace('|<.*?>|', '', $text);        $text = SimpleHtmlSaxParser::decodeHtml($text);        $text = preg_replace('|\s+|', ' ', $text);        return trim($text);    }}/** *    SAX event handler. *    @package SimpleTest *    @subpackage WebTester *    @abstract */class SimpleSaxListener {        /**     *    Sets the document to write to.     *    @access public     */    function SimpleSaxListener() {    }        /**     *    Start of element event.     *    @param string $name        Element name.     *    @param hash $attributes    Name value pairs.     *                               Attributes without content     *                               are marked as true.     *    @return boolean            False on parse error.     *    @access public     */    function startElement($name, $attributes) {    }        /**     *    End of element event.     *    @param string $name        Element name.     *    @return boolean            False on parse error.     *    @access public     */    function endElement($name) {    }        /**     *    Unparsed, but relevant data.     *    @param string $text        May include unparsed tags.     *    @return boolean            False on parse error.     *    @access public     */    function addContent($text) {    }}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?