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();
* Accessor for last Authentication type. Only valid
* straight after a challenge (401).
* @return string Description of challenge type.
return $this->_headers->getAuthentication();
* Accessor for last Authentication realm. Only valid
* straight after a challenge (401).
* @return string Name of security realm.
* Accessor for current frame focus. Will be
* @return array Always empty.
* Sets the focus by index. The integer index starts from 1.
* @param integer $choice Chosen frame.
* @return boolean Always false.
* Sets the focus by name. Always fails for a leaf page.
* @param string $name Chosen frame.
* @return boolean False as no frames.
* Clears the frame focus. Does nothing for a leaf page.
* Adds a tag to the page.
* @param SimpleTag $tag Tag to accept.
if ($tag->getTagName() ==
"a") {
} elseif ($tag->getTagName() ==
"base") {
} elseif ($tag->getTagName() ==
"title") {
} elseif ($this->_isFormElement($tag->getTagName())) {
* Opens a label for a described widget.
* @param SimpleFormTag $tag Tag to accept.
* Closes the most recently opened label.
* Tests to see if a tag is a possible form
* @param string $name HTML element name.
* @return boolean True if form element.
function _isFormElement($name) {
return in_array($name, array('input', 'button', 'textarea', 'select'));
* Opens a form. New widgets go here.
* @param SimpleFormTag $tag Tag to accept.
* Closes the most recently opened form.
* Opens a frameset. A frameset may contain nested
* @param SimpleFramesetTag $tag Tag to accept.
if (! $this->_isLoadingFrames()) {
* Closes the most recently opened frameset.
if ($this->_isLoadingFrames()) {
* Takes a single frame tag and stashes it in
* @param SimpleFrameTag $tag Tag to accept.
if ($this->_isLoadingFrames()) {
if ($tag->getAttribute('src')) {
* Test to see if in the middle of reading
* @return boolean True if inframeset.
function _isLoadingFrames() {
* Test to see if link is an absolute one.
* @param string $url Url to test.
* @return boolean True if absolute.
return (boolean)
($parsed->getScheme() &&
$parsed->getHost());
* Adds a link to the page.
* @param SimpleAnchorTag $tag Link to accept.
* Marker for end of complete page. Any work in
* progress can now be closed.
* Test for the presence of a frameset.
* @return boolean True if frameset.
* Accessor for frame name and source URL for every frame that
* will need to be loaded. Immediate children only.
* @return boolean/array False if no frameset or
* otherwise a hash of frame URLs.
* The key is either a numerical
* base one index or the name attribute.
$name =
$this->_frames[$i]->getAttribute('name');
$urls[$name ?
$name :
$i +
1] =
$this->expandUrl($url);
* Fetches a list of loaded frames.
* @return array/string Just the URL for a single page.
* Accessor for a list of all links.
* @return array List of urls with scheme of
* http or https and hostname.
foreach ($this->_links as $link) {
$url =
$this->_getUrlFromLink($link);
$all[] =
$url->asString();
* Accessor for URLs by the link label. Label will match
* regardess of whitespace issues and case.
* @param string $label Text of link.
* @return array List of links with that label.
foreach ($this->_links as $link) {
if ($link->getText() ==
$label) {
$matches[] =
$this->_getUrlFromLink($link);
* Accessor for a URL by the id attribute.
* @param string $id Id attribute of link.
* @return SimpleUrl URL with that id of false if none.
foreach ($this->_links as $link) {
if ($link->getAttribute('id') === (string)
$id) {
return $this->_getUrlFromLink($link);
* Converts a link tag into a target URL.
* @param SimpleAnchor $link Parsed link.
* @return SimpleUrl URL with frame target if any.
function _getUrlFromLink($link) {
if ($link->getAttribute('target')) {
$url->setTarget($link->getAttribute('target'));
* Expands expandomatic URLs into fully qualified
* @param SimpleUrl $url Relative URL.
* @return SimpleUrl Absolute URL.
return $url->makeAbsolute($location->makeAbsolute($this->getUrl()));
* Sets the base url for the page.
* @param SimpleTag $tag Base URL for page.
$url =
$tag->getAttribute('href');
* Sets the title tag contents.
* @param SimpleTitleTag $tag Title of page.
* Accessor for parsed title.
* @return string Title or false if no title is present.
return $this->_title->getText();
* Finds a held form by button label. Will only
* search correctly built forms.
* @param SimpleSelector $selector Button finder.
* @return SimpleForm Form object containing
* Finds a held form by image using a selector.
* Will only search correctly built forms.
* @param SimpleSelector $selector Image finder.
* @return SimpleForm Form object containing
* Finds a held form by the form ID. A way of
* identifying a specific form when we have control
* @param string $id Form label.
* @return SimpleForm Form object containing the matching ID.
* Sets a field on each form in which the field is
* @param SimpleSelector $selector Field finder.
* @param string $value Value to set field to.
* @return boolean True if value is valid.
function setField($selector, $value, $position=
false) {
* Accessor for a form element value within a page.
* @param SimpleSelector $selector Field finder.
* @return string/boolean A string if the field is
* present, false if unchecked
Documentation generated on Sun, 04 May 2008 09:21:53 -0500 by phpDocumentor 1.3.0