Source for file page.php
Documentation is available at page.php
* Base include file for SimpleTest
* @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
* Factory for the tag objects. Creates the
* appropriate tag object for the incoming tag name
* @param string $name HTML tag name.
* @param hash $attributes Element attributes.
* @return SimpleTag Tag object.
'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') {
* Factory for selection fields.
* @param hash $attributes Element attributes.
* @return SimpleTag Tag object.
if (isset
($attributes['multiple'])) {
* Factory for input tags.
* @param hash $attributes Element attributes.
* @return SimpleTag Tag object.
if (! isset
($attributes['type'])) {
'submit' =>
'SimpleSubmitTag',
'image' =>
'SimpleImageSubmitTag',
'checkbox' =>
'SimpleCheckboxTag',
'radio' =>
'SimpleRadioButtonTag',
'text' =>
'SimpleTextTag',
'hidden' =>
'SimpleTextTag',
'password' =>
'SimpleTextTag',
'file' =>
'SimpleUploadTag');
$tag_class =
$map[$type];
return new $tag_class($attributes);
* Make the keys lower case for case insensitive look-ups.
* @param hash $map Hash to convert.
* @return hash Unchanged values, but keys lower case.
function _keysToLowerCase($map) {
foreach ($map as $key =>
$value) {
* SAX event handler. Maintains a list of
* open tags and dispatches them as they close.
* Sets the builder up empty.
* Frees up any references so as to allow the PHP garbage
* collection from unset() to work.
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.
function &parse($response) {
$parser->parse($response->getContent());
$this->_page->acceptPageEnd();
* @return SimplePage New unparsed page.
* Creates the parser used with the builder.
* @param $listener SimpleSaxListener Target of parser.
* @return SimpleSaxParser Parser to generate
* events for the builder.
* Start of element event. Opens a new tag.
* @param string $name Element name.
* @param hash $attributes Attributes without content
* @return boolean False on parse error.
$tag =
$factory->createTag($name, $attributes);
if ($tag->getTagName() ==
'label') {
$this->_page->acceptLabelStart($tag);
if ($tag->getTagName() ==
'form') {
$this->_page->acceptFormStart($tag);
if ($tag->getTagName() ==
'frameset') {
$this->_page->acceptFramesetStart($tag);
if ($tag->getTagName() ==
'frame') {
$this->_page->acceptFrame($tag);
if ($tag->expectEndTag()) {
$this->_page->acceptTag($tag);
* @param string $name Element name.
* @return boolean False on parse error.
$this->_page->acceptLabelEnd();
$this->_page->acceptFormEnd();
if ($name ==
'frameset') {
$this->_page->acceptFramesetEnd();
if ($this->_hasNamedTagOnOpenTagStack($name)) {
$this->_addContentTagToOpenTags($tag);
$this->_page->acceptTag($tag);
* 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.
function _hasNamedTagOnOpenTagStack($name) {
* Unparsed, but relevant data. The data is added
* @param string $text May include unparsed tags.
* @return boolean False on parse error.
$this->_addContentToAllOpenTags($text);
* Any content fills all currently open tags unless it
* is part of an option tag.
* @param string $text May include unparsed tags.
function _addContentToAllOpenTags($text) {
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
* @param SimpleTag $tag Option tags only.
function _addContentTagToOpenTags(&$tag) {
if ($tag->getTagName() !=
'option') {
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.
function _openTag(&$tag) {
$name =
$tag->getTagName();
$this->_tags[$name] =
array();
$this->_tags[$name][] =
&$tag;
* A wrapper for a web page.
* Parses a page ready to access it's contents.
* @param SimpleHttpResponse $response Result of HTTP fetch.
$this->_extractResponse($response);
* Extracts all of the response information.
* @param SimpleHttpResponse $response Response being parsed.
function _extractResponse($response) {
$this->_raw =
$response->getContent();
$this->_sent =
$response->getSent();
$this->_headers =
$response->getHeaders();
$this->_method =
$response->getMethod();
$this->_url =
$response->getUrl();
* Sets up a missing response.
* Original request as bytes sent down the wire.
* @return mixed Sent content.
* Accessor for raw text of page.
* @return string Raw unparsed content.
* Accessor for plain text of page as a text browser
* @return string Plain text of page.
* Accessor for raw headers of page.
* @return string Header block as text.
* Original request method.
* @return string GET, POST or HEAD.
* Original resource name.
* @return SimpleUrl Current url.
* Base URL if set via BASE tag page url otherwise
* @return SimpleUrl Base url.
* @return mixed Sent content.
* Accessor for last error.
* @return string Error from last response.
* Accessor for current MIME type.
* @return string MIME type as string; e.g. 'text/html'
* Accessor for HTTP response code.
* @return integer HTTP response code received.
return $this->_headers->getResponseCode();