[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/tests_runner/lib/simpletest/ -> tag.php (source)

   1  <?php
   2      /**

   3       *    Base include file for SimpleTest.

   4       *    @package    SimpleTest

   5       *    @subpackage    WebTester

   6       *    @version    $Id: tag.php 5999 2007-06-18 13:13:08Z pachanga $

   7       */
   8       
   9      /**#@+

  10       * include SimpleTest files

  11       */
  12      require_once(dirname(__FILE__) . '/parser.php');
  13      require_once(dirname(__FILE__) . '/encoding.php');
  14      /**#@-*/

  15     
  16      /**

  17       *    HTML or XML tag.

  18       *    @package SimpleTest

  19       *    @subpackage WebTester

  20       */
  21      class SimpleTag {
  22          var $_name;
  23          var $_attributes;
  24          var $_content;
  25          
  26          /**

  27           *    Starts with a named tag with attributes only.

  28           *    @param string $name        Tag name.

  29           *    @param hash $attributes    Attribute names and

  30           *                               string values. Note that

  31           *                               the keys must have been

  32           *                               converted to lower case.

  33           */
  34          function SimpleTag($name, $attributes) {
  35              $this->_name = strtolower(trim($name));
  36              $this->_attributes = $attributes;
  37              $this->_content = '';
  38          }
  39          
  40          /**

  41           *    Check to see if the tag can have both start and

  42           *    end tags with content in between.

  43           *    @return boolean        True if content allowed.

  44           *    @access public

  45           */
  46          function expectEndTag() {
  47              return true;
  48          }
  49          
  50          /**

  51           *    The current tag should not swallow all content for

  52           *    itself as it's searchable page content. Private

  53           *    content tags are usually widgets that contain default

  54           *    values.

  55           *    @return boolean        False as content is available

  56           *                           to other tags by default.

  57           *    @access public

  58           */
  59          function isPrivateContent() {
  60              return false;
  61          }
  62  
  63          /**

  64           *    Appends string content to the current content.

  65           *    @param string $content        Additional text.

  66           *    @access public

  67           */
  68          function addContent($content) {
  69              $this->_content .= (string)$content;
  70          }
  71          
  72          /**

  73           *    Adds an enclosed tag to the content.

  74           *    @param SimpleTag $tag    New tag.

  75           *    @access public

  76           */
  77          function addTag(&$tag) {
  78          }
  79          
  80          /**

  81           *    Accessor for tag name.

  82           *    @return string       Name of tag.

  83           *    @access public

  84           */
  85          function getTagName() {
  86              return $this->_name;
  87          }
  88          
  89          /**

  90           *    List of legal child elements.

  91           *    @return array        List of element names.

  92           *    @access public

  93           */
  94          function getChildElements() {
  95              return array();
  96          }
  97          
  98          /**

  99           *    Accessor for an attribute.

 100           *    @param string $label    Attribute name.

 101           *    @return string          Attribute value.

 102           *    @access public

 103           */
 104          function getAttribute($label) {
 105              $label = strtolower($label);
 106              if (! isset($this->_attributes[$label])) {
 107                  return false;
 108              }
 109              return (string)$this->_attributes[$label];
 110          }
 111          
 112          /**

 113           *    Sets an attribute.

 114           *    @param string $label    Attribute name.

 115           *    @return string $value   New attribute value.

 116           *    @access protected

 117           */
 118          function _setAttribute($label, $value) {
 119              $this->_attributes[strtolower($label)] = $value;
 120          }
 121          
 122          /**

 123           *    Accessor for the whole content so far.

 124           *    @return string       Content as big raw string.

 125           *    @access public

 126           */
 127          function getContent() {
 128              return $this->_content;
 129          }
 130          
 131          /**

 132           *    Accessor for content reduced to visible text. Acts

 133           *    like a text mode browser, normalising space and

 134           *    reducing images to their alt text.

 135           *    @return string       Content as plain text.

 136           *    @access public

 137           */
 138          function getText() {
 139              return SimpleHtmlSaxParser::normalise($this->_content);
 140          }
 141          
 142          /**

 143           *    Test to see if id attribute matches.

 144           *    @param string $id        ID to test against.

 145           *    @return boolean          True on match.

 146           *    @access public

 147           */
 148          function isId($id) {
 149              return ($this->getAttribute('id') == $id);
 150          }
 151      }
 152      
 153      /**

 154       *    Page title.

 155       *    @package SimpleTest

 156       *    @subpackage WebTester

 157       */
 158      class SimpleTitleTag extends SimpleTag {
 159          
 160          /**

 161           *    Starts with a named tag with attributes only.

 162           *    @param hash $attributes    Attribute names and

 163           *                               string values.

 164           */
 165          function SimpleTitleTag($attributes) {
 166              $this->SimpleTag('title', $attributes);
 167          }
 168      }
 169      
 170      /**

 171       *    Link.

 172       *    @package SimpleTest

 173       *    @subpackage WebTester

 174       */
 175      class SimpleAnchorTag extends SimpleTag {
 176          
 177          /**

 178           *    Starts with a named tag with attributes only.

 179           *    @param hash $attributes    Attribute names and

 180           *                               string values.

 181           */
 182          function SimpleAnchorTag($attributes) {
 183              $this->SimpleTag('a', $attributes);
 184          }
 185          
 186          /**

 187           *    Accessor for URL as string.

 188           *    @return string    Coerced as string.

 189           *    @access public

 190           */
 191          function getHref() {
 192              $url = $this->getAttribute('href');
 193              if (is_bool($url)) {
 194                  $url = '';
 195              }
 196              return $url;
 197          }
 198      }
 199      
 200      /**

 201       *    Form element.

 202       *    @package SimpleTest

 203       *    @subpackage WebTester

 204       */
 205      class SimpleWidget extends SimpleTag {
 206          var $_value;
 207          var $_label;
 208          var $_is_set;
 209          
 210          /**

 211           *    Starts with a named tag with attributes only.

 212           *    @param string $name        Tag name.

 213           *    @param hash $attributes    Attribute names and

 214           *                               string values.

 215           */
 216          function SimpleWidget($name, $attributes) {
 217              $this->SimpleTag($name, $attributes);
 218              $this->_value = false;
 219              $this->_label = false;
 220              $this->_is_set = false;
 221          }
 222          
 223          /**

 224           *    Accessor for name submitted as the key in

 225           *    GET/POST variables hash.

 226           *    @return string        Parsed value.

 227           *    @access public

 228           */
 229          function getName() {
 230              return $this->getAttribute('name');
 231          }
 232          
 233          /**

 234           *    Accessor for default value parsed with the tag.

 235           *    @return string        Parsed value.

 236           *    @access public

 237           */
 238          function getDefault() {
 239              return $this->getAttribute('value');
 240          }
 241          
 242          /**

 243           *    Accessor for currently set value or default if

 244           *    none.

 245           *    @return string      Value set by form or default

 246           *                        if none.

 247           *    @access public

 248           */
 249          function getValue() {
 250              if (! $this->_is_set) {
 251                  return $this->getDefault();
 252              }
 253              return $this->_value;
 254          }
 255          
 256          /**

 257           *    Sets the current form element value.

 258           *    @param string $value       New value.

 259           *    @return boolean            True if allowed.

 260           *    @access public

 261           */
 262          function setValue($value) {
 263              $this->_value = $value;
 264              $this->_is_set = true;
 265              return true;
 266          }
 267          
 268          /**

 269           *    Resets the form element value back to the

 270           *    default.

 271           *    @access public

 272           */
 273          function resetValue() {
 274              $this->_is_set = false;
 275          }
 276          
 277          /**

 278           *    Allows setting of a label externally, say by a

 279           *    label tag.

 280           *    @param string $label    Label to attach.

 281           *    @access public

 282           */
 283          function setLabel($label) {
 284              $this->_label = trim($label);
 285          }
 286          
 287          /**

 288           *    Reads external or internal label.

 289           *    @param string $label    Label to test.

 290           *    @return boolean         True is match.

 291           *    @access public

 292           */
 293          function isLabel($label) {
 294              return $this->_label == trim($label);
 295          }
 296          
 297          /**

 298           *    Dispatches the value into the form encoded packet.

 299           *    @param SimpleEncoding $encoding    Form packet.

 300           *    @access public

 301           */
 302          function write(&$encoding) {
 303              if ($this->getName()) {
 304                  $encoding->add($this->getName(), $this->getValue());
 305              }
 306          }
 307      }
 308      
 309      /**

 310       *    Text, password and hidden field.

 311       *    @package SimpleTest

 312       *    @subpackage WebTester

 313       */
 314      class SimpleTextTag extends SimpleWidget {
 315          
 316          /**

 317           *    Starts with a named tag with attributes only.

 318           *    @param hash $attributes    Attribute names and

 319           *                               string values.

 320           */
 321          function SimpleTextTag($attributes) {
 322              $this->SimpleWidget('input', $attributes);
 323              if ($this->getAttribute('value') === false) {
 324                  $this->_setAttribute('value', '');
 325              }
 326          }
 327          
 328          /**

 329           *    Tag contains no content.

 330           *    @return boolean        False.

 331           *    @access public

 332           */
 333          function expectEndTag() {
 334              return false;
 335          }
 336          
 337          /**

 338           *    Sets the current form element value. Cannot

 339           *    change the value of a hidden field.

 340           *    @param string $value       New value.

 341           *    @return boolean            True if allowed.

 342           *    @access public

 343           */
 344          function setValue($value) {
 345              if ($this->getAttribute('type') == 'hidden') {
 346                  return false;
 347              }
 348              return parent::setValue($value);
 349          }
 350      }
 351      
 352      /**

 353       *    Submit button as input tag.

 354       *    @package SimpleTest

 355       *    @subpackage WebTester

 356       */
 357      class SimpleSubmitTag extends SimpleWidget {
 358          
 359          /**

 360           *    Starts with a named tag with attributes only.

 361           *    @param hash $attributes    Attribute names and

 362           *                               string values.

 363           */
 364          function SimpleSubmitTag($attributes) {
 365              $this->SimpleWidget('input', $attributes);
 366              if ($this->getAttribute('value') === false) {
 367                  $this->_setAttribute('value', 'Submit');
 368              }
 369          }
 370          
 371          /**

 372           *    Tag contains no end element.

 373           *    @return boolean        False.

 374           *    @access public

 375           */
 376          function expectEndTag() {
 377              return false;
 378          }
 379          
 380          /**

 381           *    Disables the setting of the button value.

 382           *    @param string $value       Ignored.

 383           *    @return boolean            True if allowed.

 384           *    @access public

 385           */
 386          function setValue($value) {
 387              return false;
 388          }
 389          
 390          /**

 391           *    Value of browser visible text.

 392           *    @return string        Visible label.

 393           *    @access public

 394           */
 395          function getLabel() {
 396              return $this->getValue();
 397          }
 398          
 399          /**

 400           *    Test for a label match when searching.

 401           *    @param string $label     Label to test.

 402           *    @return boolean          True on match.

 403           *    @access public

 404           */
 405          function isLabel($label) {
 406              return trim($label) == trim($this->getLabel());
 407          }
 408      }
 409        
 410      /**

 411       *    Image button as input tag.

 412       *    @package SimpleTest

 413       *    @subpackage WebTester

 414       */
 415      class SimpleImageSubmitTag extends SimpleWidget {
 416          
 417          /**

 418           *    Starts with a named tag with attributes only.

 419           *    @param hash $attributes    Attribute names and

 420           *                               string values.

 421           */
 422          function SimpleImageSubmitTag($attributes) {
 423              $this->SimpleWidget('input', $attributes);
 424          }
 425          
 426          /**

 427           *    Tag contains no end element.

 428           *    @return boolean        False.

 429           *    @access public

 430           */
 431          function expectEndTag() {
 432              return false;
 433          }
 434          
 435          /**

 436           *    Disables the setting of the button value.

 437           *    @param string $value       Ignored.

 438           *    @return boolean            True if allowed.

 439           *    @access public

 440           */
 441          function setValue($value) {
 442              return false;
 443          }
 444          
 445          /**

 446           *    Value of browser visible text.

 447           *    @return string        Visible label.

 448           *    @access public

 449           */
 450          function getLabel() {
 451              if ($this->getAttribute('title')) {
 452                  return $this->getAttribute('title');
 453              }
 454              return $this->getAttribute('alt');
 455          }
 456          
 457          /**

 458           *    Test for a label match when searching.

 459           *    @param string $label     Label to test.

 460           *    @return boolean          True on match.

 461           *    @access public

 462           */
 463          function isLabel($label) {
 464              return trim($label) == trim($this->getLabel());
 465          }
 466          
 467          /**

 468           *    Dispatches the value into the form encoded packet.

 469           *    @param SimpleEncoding $encoding    Form packet.

 470           *    @param integer $x                  X coordinate of click.

 471           *    @param integer $y                  Y coordinate of click.

 472           *    @access public

 473           */
 474          function write(&$encoding, $x, $y) {
 475              if ($this->getName()) {
 476                  $encoding->add($this->getName() . '.x', $x);
 477                  $encoding->add($this->getName() . '.y', $y);
 478              } else {
 479                  $encoding->add('x', $x);
 480                  $encoding->add('y', $y);
 481              }
 482          }
 483      }
 484        
 485      /**

 486       *    Submit button as button tag.

 487       *    @package SimpleTest

 488       *    @subpackage WebTester

 489       */
 490      class SimpleButtonTag extends SimpleWidget {
 491          
 492          /**

 493           *    Starts with a named tag with attributes only.

 494           *    Defaults are very browser dependent.

 495           *    @param hash $attributes    Attribute names and

 496           *                               string values.

 497           */
 498          function SimpleButtonTag($attributes) {
 499              $this->SimpleWidget('button', $attributes);
 500          }
 501          
 502          /**

 503           *    Check to see if the tag can have both start and

 504           *    end tags with content in between.

 505           *    @return boolean        True if content allowed.

 506           *    @access public

 507           */
 508          function expectEndTag() {
 509              return true;
 510          }
 511          
 512          /**

 513           *    Disables the setting of the button value.

 514           *    @param string $value       Ignored.

 515           *    @return boolean            True if allowed.

 516           *    @access public

 517           */
 518          function setValue($value) {
 519              return false;
 520          }
 521          
 522          /**

 523           *    Value of browser visible text.

 524           *    @return string        Visible label.

 525           *    @access public

 526           */
 527          function getLabel() {
 528              return $this->getContent();
 529          }
 530          
 531          /**

 532           *    Test for a label match when searching.

 533           *    @param string $label     Label to test.

 534           *    @return boolean          True on match.

 535           *    @access public

 536           */
 537          function isLabel($label) {
 538              return trim($label) == trim($this->getLabel());
 539          }
 540      }
 541    
 542      /**

 543       *    Content tag for text area.

 544       *    @package SimpleTest

 545       *    @subpackage WebTester

 546       */
 547      class SimpleTextAreaTag extends SimpleWidget {
 548          
 549          /**

 550           *    Starts with a named tag with attributes only.

 551           *    @param hash $attributes    Attribute names and

 552           *                               string values.

 553           */
 554          function SimpleTextAreaTag($attributes) {
 555              $this->SimpleWidget('textarea', $attributes);
 556          }
 557          
 558          /**

 559           *    Accessor for starting value.

 560           *    @return string        Parsed value.

 561           *    @access public

 562           */
 563          function getDefault() {
 564              return $this->_wrap(SimpleHtmlSaxParser::decodeHtml($this->getContent()));
 565          }
 566          
 567          /**

 568           *    Applies word wrapping if needed.

 569           *    @param string $value      New value.

 570           *    @return boolean            True if allowed.

 571           *    @access public

 572           */
 573          function setValue($value) {
 574              return parent::setValue($this->_wrap($value));
 575          }
 576          
 577          /**

 578           *    Test to see if text should be wrapped.

 579           *    @return boolean        True if wrapping on.

 580           *    @access private

 581           */
 582          function _wrapIsEnabled() {
 583              if ($this->getAttribute('cols')) {
 584                  $wrap = $this->getAttribute('wrap');
 585                  if (($wrap == 'physical') || ($wrap == 'hard')) {
 586                      return true;
 587                  }
 588              }
 589              return false;
 590          }
 591          
 592          /**

 593           *    Performs the formatting that is peculiar to

 594           *    this tag. There is strange behaviour in this

 595           *    one, including stripping a leading new line.

 596           *    Go figure. I am using Firefox as a guide.

 597           *    @param string $text    Text to wrap.

 598           *    @return string         Text wrapped with carriage

 599           *                           returns and line feeds

 600           *    @access private

 601           */
 602          function _wrap($text) {
 603              $text = str_replace("\r\r\n", "\r\n", str_replace("\n", "\r\n", $text));
 604              $text = str_replace("\r\n\n", "\r\n", str_replace("\r", "\r\n", $text));
 605              if (strncmp($text, "\r\n", strlen("\r\n")) == 0) {
 606                  $text = substr($text, strlen("\r\n"));
 607              }
 608              if ($this->_wrapIsEnabled()) {
 609                  return wordwrap(
 610                          $text,
 611                          (integer)$this->getAttribute('cols'),
 612                          "\r\n");
 613              }
 614              return $text;
 615          }
 616          
 617          /**

 618           *    The content of textarea is not part of the page.

 619           *    @return boolean        True.

 620           *    @access public

 621           */
 622          function isPrivateContent() {
 623              return true;
 624          }
 625      }
 626      
 627      /**

 628       *    File upload widget.

 629       *    @package SimpleTest

 630       *    @subpackage WebTester

 631       */
 632      class SimpleUploadTag extends SimpleWidget {
 633          
 634          /**

 635           *    Starts with attributes only.

 636           *    @param hash $attributes    Attribute names and

 637           *                               string values.

 638           */
 639          function SimpleUploadTag($attributes) {
 640              $this->SimpleWidget('input', $attributes);
 641          }
 642          
 643          /**

 644           *    Tag contains no content.

 645           *    @return boolean        False.

 646           *    @access public

 647           */
 648          function expectEndTag() {
 649              return false;
 650          }
 651          
 652          /**

 653           *    Dispatches the value into the form encoded packet.

 654           *    @param SimpleEncoding $encoding    Form packet.

 655           *    @access public

 656           */
 657          function write(&$encoding) {
 658              if (! file_exists($this->getValue())) {
 659                  return;
 660              }
 661              $encoding->attach(
 662                      $this->getName(),
 663                      implode('', file($this->getValue())),
 664                      basename($this->getValue()));
 665          }
 666      }
 667      
 668      /**

 669       *    Drop down widget.

 670       *    @package SimpleTest

 671       *    @subpackage WebTester

 672       */
 673      class SimpleSelectionTag extends SimpleWidget {
 674          var $_options;
 675          var $_choice;
 676          
 677          /**

 678           *    Starts with attributes only.

 679           *    @param hash $attributes    Attribute names and

 680           *                               string values.

 681           */
 682          function SimpleSelectionTag($attributes) {
 683              $this->SimpleWidget('select', $attributes);
 684              $this->_options = array();
 685              $this->_choice = false;
 686          }
 687          
 688          /**

 689           *    Adds an option tag to a selection field.

 690           *    @param SimpleOptionTag $tag     New option.

 691           *    @access public

 692           */
 693          function addTag(&$tag) {
 694              if ($tag->getTagName() == 'option') {
 695                  $this->_options[] = &$tag;
 696              }
 697          }
 698          
 699          /**

 700           *    Text within the selection element is ignored.

 701           *    @param string $content        Ignored.

 702           *    @access public

 703           */
 704          function addContent($content) {
 705          }
 706          
 707          /**

 708           *    Scans options for defaults. If none, then

 709           *    the first option is selected.

 710           *    @return string        Selected field.

 711           *    @access public

 712           */
 713          function getDefault() {
 714              for ($i = 0, $count = count($this->_options); $i <