page.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 983 行 · 第 1/2 页

PHP
983
字号
<?php
/**
 *  Base include file for SimpleTest
 *  @package    SimpleTest
 *  @subpackage WebTester
 *  @version    $Id: page.php 1672 2008-03-02 04:47:34Z edwardzyang $
 */

/**#@+
    *   include other SimpleTest class files
    */
require_once(dirname(__FILE__) . '/http.php');
require_once(dirname(__FILE__) . '/parser.php');
require_once(dirname(__FILE__) . '/tag.php');
require_once(dirname(__FILE__) . '/form.php');
require_once(dirname(__FILE__) . '/selector.php');
/**#@-*/

/**
 *    Creates tags and widgets given HTML tag
 *    attributes.
 *    @package SimpleTest
 *    @subpackage WebTester
 */
class SimpleTagBuilder {

    /**
     *    Factory for the tag objects. Creates the
     *    appropriate tag object for the incoming tag name
     *    and attributes.
     *    @param string $name        HTML tag name.
     *    @param hash $attributes    Element attributes.
     *    @return SimpleTag          Tag object.
     *    @access public
     */
    function createTag($name, $attributes) {
        static $map = array(
                'a' => 'SimpleAnchorTag',
                'title' => 'SimpleTitleTag',
                'base' => 'SimpleBaseTag',
                'button' => 'SimpleButtonTag',
                'textarea' => 'SimpleTextAreaTag',
                'option' => 'SimpleOptionTag',
                'label' => 'SimpleLabelTag',
                'form' => 'SimpleFormTag',
                'frame' => 'SimpleFrameTag');
        $attributes = $this->_keysToLowerCase($attributes);
        if (array_key_exists($name, $map)) {
            $tag_class = $map[$name];
            return new $tag_class($attributes);
        } elseif ($name == 'select') {
            return $this->_createSelectionTag($attributes);
        } elseif ($name == 'input') {
            return $this->_createInputTag($attributes);
        }
        return new SimpleTag($name, $attributes);
    }

    /**
     *    Factory for selection fields.
     *    @param hash $attributes    Element attributes.
     *    @return SimpleTag          Tag object.
     *    @access protected
     */
    function _createSelectionTag($attributes) {
        if (isset($attributes['multiple'])) {
            return new MultipleSelectionTag($attributes);
        }
        return new SimpleSelectionTag($attributes);
    }

    /**
     *    Factory for input tags.
     *    @param hash $attributes    Element attributes.
     *    @return SimpleTag          Tag object.
     *    @access protected
     */
    function _createInputTag($attributes) {
        if (! isset($attributes['type'])) {
            return new SimpleTextTag($attributes);
        }
        $type = strtolower(trim($attributes['type']));
        $map = array(
                'submit' => 'SimpleSubmitTag',
                'image' => 'SimpleImageSubmitTag',
                'checkbox' => 'SimpleCheckboxTag',
                'radio' => 'SimpleRadioButtonTag',
                'text' => 'SimpleTextTag',
                'hidden' => 'SimpleTextTag',
                'password' => 'SimpleTextTag',
                'file' => 'SimpleUploadTag');
        if (array_key_exists($type, $map)) {
            $tag_class = $map[$type];
            return new $tag_class($attributes);
        }
        return false;
    }

    /**
     *    Make the keys lower case for case insensitive look-ups.
     *    @param hash $map   Hash to convert.
     *    @return hash       Unchanged values, but keys lower case.
     *    @access private
     */
    function _keysToLowerCase($map) {
        $lower = array();
        foreach ($map as $key => $value) {
            $lower[strtolower($key)] = $value;
        }
        return $lower;
    }
}

/**
 *    SAX event handler. Maintains a list of
 *    open tags and dispatches them as they close.
 *    @package SimpleTest
 *    @subpackage WebTester
 */
class SimplePageBuilder extends SimpleSaxListener {
    var $_tags;
    var $_page;
    var $_private_content_tag;

    /**
     *    Sets the builder up empty.
     *    @access public
     */
    function SimplePageBuilder() {
        $this->SimpleSaxListener();
    }
    
    /**
     *    Frees up any references so as to allow the PHP garbage
     *    collection from unset() to work.
     *    @access public
     */
    function free() {
        unset($this->_tags);
        unset($this->_page);
        unset($this->_private_content_tags);
    }

    /**
     *    Reads the raw content and send events
     *    into the page to be built.
     *    @param $response SimpleHttpResponse  Fetched response.
     *    @return SimplePage                   Newly parsed page.
     *    @access public
     */
    function &parse($response) {
        $this->_tags = array();
        $this->_page = &$this->_createPage($response);
        $parser = &$this->_createParser($this);
        $parser->parse($response->getContent());
        $this->_page->acceptPageEnd();
        return $this->_page;
    }

    /**
     *    Creates an empty page.
     *    @return SimplePage        New unparsed page.
     *    @access protected
     */
    function &_createPage($response) {
        $page = &new SimplePage($response);
        return $page;
    }

    /**
     *    Creates the parser used with the builder.
     *    @param $listener SimpleSaxListener   Target of parser.
     *    @return SimpleSaxParser              Parser to generate
     *                                         events for the builder.
     *    @access protected
     */
    function &_createParser(&$listener) {
        $parser = &new SimpleHtmlSaxParser($listener);
        return $parser;
    }
    
    /**
     *    Start of element event. Opens a new tag.
     *    @param string $name         Element name.
     *    @param hash $attributes     Attributes without content
     *                                are marked as true.
     *    @return boolean             False on parse error.
     *    @access public
     */
    function startElement($name, $attributes) {
        $factory = &new SimpleTagBuilder();
        $tag = $factory->createTag($name, $attributes);
        if (! $tag) {
            return true;
        }
        if ($tag->getTagName() == 'label') {
            $this->_page->acceptLabelStart($tag);
            $this->_openTag($tag);
            return true;
        }
        if ($tag->getTagName() == 'form') {
            $this->_page->acceptFormStart($tag);
            return true;
        }
        if ($tag->getTagName() == 'frameset') {
            $this->_page->acceptFramesetStart($tag);
            return true;
        }
        if ($tag->getTagName() == 'frame') {
            $this->_page->acceptFrame($tag);
            return true;
        }
        if ($tag->isPrivateContent() && ! isset($this->_private_content_tag)) {
            $this->_private_content_tag = &$tag;
        }
        if ($tag->expectEndTag()) {
            $this->_openTag($tag);
            return true;
        }
        $this->_page->acceptTag($tag);
        return true;
    }

    /**
     *    End of element event.
     *    @param string $name        Element name.
     *    @return boolean            False on parse error.
     *    @access public
     */
    function endElement($name) {
        if ($name == 'label') {
            $this->_page->acceptLabelEnd();
            return true;
        }
        if ($name == 'form') {
            $this->_page->acceptFormEnd();
            return true;
        }
        if ($name == 'frameset') {
            $this->_page->acceptFramesetEnd();
            return true;
        }
        if ($this->_hasNamedTagOnOpenTagStack($name)) {
            $tag = array_pop($this->_tags[$name]);
            if ($tag->isPrivateContent() && $this->_private_content_tag->getTagName() == $name) {
                unset($this->_private_content_tag);
            }
            $this->_addContentTagToOpenTags($tag);
            $this->_page->acceptTag($tag);
            return true;
        }
        return true;
    }

    /**
     *    Test to see if there are any open tags awaiting
     *    closure that match the tag name.
     *    @param string $name        Element name.
     *    @return boolean            True if any are still open.
     *    @access private
     */
    function _hasNamedTagOnOpenTagStack($name) {
        return isset($this->_tags[$name]) && (count($this->_tags[$name]) > 0);
    }

    /**
     *    Unparsed, but relevant data. The data is added
     *    to every open tag.
     *    @param string $text        May include unparsed tags.
     *    @return boolean            False on parse error.
     *    @access public
     */
    function addContent($text) {
        if (isset($this->_private_content_tag)) {
            $this->_private_content_tag->addContent($text);
        } else {
            $this->_addContentToAllOpenTags($text);
        }
        return true;
    }

    /**
     *    Any content fills all currently open tags unless it
     *    is part of an option tag.
     *    @param string $text        May include unparsed tags.
     *    @access private
     */
    function _addContentToAllOpenTags($text) {
        foreach (array_keys($this->_tags) as $name) {
            for ($i = 0, $count = count($this->_tags[$name]); $i < $count; $i++) {
                $this->_tags[$name][$i]->addContent($text);
            }
        }
    }

    /**
     *    Parsed data in tag form. The parsed tag is added
     *    to every open tag. Used for adding options to select
     *    fields only.
     *    @param SimpleTag $tag        Option tags only.
     *    @access private
     */
    function _addContentTagToOpenTags(&$tag) {
        if ($tag->getTagName() != 'option') {
            return;
        }
        foreach (array_keys($this->_tags) as $name) {
            for ($i = 0, $count = count($this->_tags[$name]); $i < $count; $i++) {
                $this->_tags[$name][$i]->addTag($tag);
            }
        }
    }

    /**
     *    Opens a tag for receiving content. Multiple tags
     *    will be receiving input at the same time.
     *    @param SimpleTag $tag        New content tag.
     *    @access private
     */
    function _openTag(&$tag) {
        $name = $tag->getTagName();
        if (! in_array($name, array_keys($this->_tags))) {
            $this->_tags[$name] = array();
        }
        $this->_tags[$name][] = &$tag;
    }
}

/**
 *    A wrapper for a web page.
 *    @package SimpleTest
 *    @subpackage WebTester
 */
class SimplePage {
    var $_links;
    var $_title;
    var $_last_widget;
    var $_label;
    var $_left_over_labels;
    var $_open_forms;
    var $_complete_forms;
    var $_frameset;
    var $_frames;
    var $_frameset_nesting_level;
    var $_transport_error;
    var $_raw;
    var $_text;
    var $_sent;
    var $_headers;
    var $_method;
    var $_url;
    var $_base = false;
    var $_request_data;

    /**
     *    Parses a page ready to access it's contents.
     *    @param SimpleHttpResponse $response     Result of HTTP fetch.
     *    @access public
     */
    function SimplePage($response = false) {
        $this->_links = array();
        $this->_title = false;
        $this->_left_over_labels = array();
        $this->_open_forms = array();
        $this->_complete_forms = array();
        $this->_frameset = false;
        $this->_frames = array();
        $this->_frameset_nesting_level = 0;
        $this->_text = false;
        if ($response) {
            $this->_extractResponse($response);
        } else {
            $this->_noResponse();
        }
    }

    /**
     *    Extracts all of the response information.
     *    @param SimpleHttpResponse $response    Response being parsed.
     *    @access private
     */
    function _extractResponse($response) {
        $this->_transport_error = $response->getError();
        $this->_raw = $response->getContent();
        $this->_sent = $response->getSent();
        $this->_headers = $response->getHeaders();
        $this->_method = $response->getMethod();
        $this->_url = $response->getUrl();
        $this->_request_data = $response->getRequestData();
    }

    /**
     *    Sets up a missing response.
     *    @access private
     */
    function _noResponse() {
        $this->_transport_error = 'No page fetched yet';
        $this->_raw = false;
        $this->_sent = false;
        $this->_headers = false;
        $this->_method = 'GET';
        $this->_url = false;
        $this->_request_data = false;
    }

    /**
     *    Original request as bytes sent down the wire.
     *    @return mixed              Sent content.
     *    @access public
     */
    function getRequest() {
        return $this->_sent;
    }

    /**
     *    Accessor for raw text of page.
     *    @return string        Raw unparsed content.
     *    @access public
     */
    function getRaw() {
        return $this->_raw;
    }

    /**
     *    Accessor for plain text of page as a text browser
     *    would see it.
     *    @return string        Plain text of page.
     *    @access public
     */
    function getText() {
        if (! $this->_text) {
            $this->_text = SimpleHtmlSaxParser::normalise($this->_raw);
        }
        return $this->_text;
    }

    /**
     *    Accessor for raw headers of page.
     *    @return string       Header block as text.
     *    @access public
     */
    function getHeaders() {
        if ($this->_headers) {
            return $this->_headers->getRaw();
        }
        return false;
    }

    /**
     *    Original request method.
     *    @return string        GET, POST or HEAD.
     *    @access public
     */
    function getMethod() {
        return $this->_method;
    }

    /**
     *    Original resource name.
     *    @return SimpleUrl        Current url.
     *    @access public
     */
    function getUrl() {
        return $this->_url;
    }

    /**
     *    Base URL if set via BASE tag page url otherwise
     *    @return SimpleUrl        Base url.
     *    @access public
     */
    function getBaseUrl() {
        return $this->_base;
    }

    /**
     *    Original request data.
     *    @return mixed              Sent content.
     *    @access public
     */
    function getRequestData() {
        return $this->_request_data;
    }

    /**
     *    Accessor for last error.
     *    @return string        Error from last response.
     *    @access public
     */
    function getTransportError() {
        return $this->_transport_error;
    }

⌨️ 快捷键说明

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