| [ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * Base include file for SimpleTest. 4 * @package SimpleTest 5 * @subpackage WebTester 6 * @version $Id: tag.php 1397 2006-09-07 07:55:53Z wei $ 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 protected $_name; 23 protected $_attributes; 24 protected $_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 protected $_value; 207 protected $_label; 208 protected $_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 protected $_options; 675 protected $_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 < $count; $i++) { 715 if ($this->_options[$i]->getAttribute('selected') !== false) { 716 return $this->_options[$i]->getDefault(); 717 } 718 } 719 if ($count > 0) { 720 return $this->_options[0]->getDefault(); 721 } 722 return ''; 723 } 724 725 /** 726 * Can only set allowed values. 727 * @param string $value New choice. 728 * @return boolean True if allowed. 729 * @access public 730 */ 731 function setValue($value) { 732 for ($i = 0, $count = count($this->_options); $i < $count; $i++) { 733 if ($this->_options[$i]->isValue($value)) { 734 $this->_choice = $i; 735 return true; 736 } 737 } 738 return false; 739 } 740 741 /** 742 * Accessor for current selection value. 743 * @return string Value attribute or 744 * content of opton. 745 * @access public 746 */ 747 function getValue() { 748 if ($this->_choice === false) { 749 return $this->getDefault(); 750 } 751 return $this->_options[$this->_choice]->getValue(); 752 } 753 } 754 755 /** 756 * Drop down widget. 757 * @package SimpleTest 758 * @subpackage WebTester 759 */ 760 class MultipleSelectionTag extends SimpleWidget { 761 protected $_options; 762 protected $_values; 763 764 /** 765 * Starts with attributes only. 766 * @param hash $attributes Attribute names and 767 * string values. 768 */ 769 function MultipleSelectionTag($attributes) { 770 $this->SimpleWidget('select', $attributes); 771 $this->_options = array(); 772 $this->_values = false; 773 } 774 775 /** 776 * Adds an option tag to a selection field. 777 * @param SimpleOptionTag $tag New option. 778 * @access public 779 */ 780 function addTag($tag) { 781 if ($tag->getTagName() == 'option') { 782 $this->_options[] = $tag; 783 } 784 } 785 786 /** 787 * Text within the selection element is ignored. 788 * @param string $content Ignored. 789 * @access public 790 */ 791 function addContent($content) { 792 } 793 794 /** 795 * Scans options for defaults to populate the 796 * value array(). 797 * @return array Selected fields. 798 * @access public 799 */ 800 function getDefault() { 801 $default = array(); 802 for ($i = 0, $count = count($this->_options); $i < $count; $i++) { 803 if ($this->_options[$i]->getAttribute('selected') !== false) { 804 $default[] = $this->_options[$i]->getDefault(); 805 } 806 } 807 return $default; 808 } 809 810 /** 811 * Can only set allowed values. Any illegal value 812 * will result in a failure, but all correct values 813 * will be set. 814 * @param array $desired New choices. 815 * @return boolean True if all allowed. 816 * @access public 817 */ 818 function setValue($desired) { 819 $achieved = array(); 820 foreach ($desired as $value) { 821 $success = false; 822 for ($i = 0, $count = count($this->_options); $i < $count; $i++) { 823 if ($this->_options[$i]->isValue($value)) { 824 $achieved[] = $this->_options[$i]->getValue(); 825 $success = true; 826 break; 827 } 828 } 829 if (! $success) { 830 return false; 831 } 832 } 833 $this->_values = $achieved; 834 return true; 835 } 836 837 /** 838 * Accessor for current selection value. 839 * @return array List of currently set options. 840 * @access public 841 */ 842 function getValue() { 843 if ($this->_values === false) { 844 return $this->getDefault(); 845 } 846 return $this->_values; 847 } 848 } 849 850 /** 851 * Option for selection field. 852 * @package SimpleTest 853 * @subpackage WebTester 854 */ 855 class SimpleOptionTag extends SimpleWidget { 856 857 /** 858 * Stashes the attributes. 859 */ 860 function SimpleOptionTag($attributes) { 861 $this->SimpleWidget('option', $attributes); 862 } 863 864 /** 865 * Does nothing. 866 * @param string $value Ignored. 867 * @return boolean Not allowed. 868 * @access public 869 */ 870 function setValue($value) { 871 return false; 872 } 873 874 /** 875 * Test to see if a value matches the option. 876 * @param string $compare Value to compare with. 877 * @return boolean True if possible match. 878 * @access public 879 */ 880 function isValue($compare) { 881 $compare = trim($compare); 882 if (trim($this->getValue()) == $compare) { 883 return true; 884 } 885 return trim($this->getContent()) == $compare; 886 } 887 888 /** 889 * Accessor for starting value. Will be set to 890 * the option label if no value exists. 891 * @return string Parsed value. 892 * @access public 893 */ 894 function getDefault() { 895 if ($this->getAttribute('value') === false) { 896 return $this->getContent(); 897 } 898 return $this->getAttribute('value'); 899 } 900 901 /** 902 * The content of options is not part of the page. 903 * @return boolean True. 904 * @access public 905 */ 906 function isPrivateContent() { 907 return true; 908 } 909 } 910 911 /** 912 * Radio button. 913 * @package SimpleTest 914 * @subpackage WebTester 915 */ 916 class SimpleRadioButtonTag extends SimpleWidget { 917 918 /** 919 * Stashes the attributes. 920 * @param array $attributes Hash of attributes. 921 */ 922 function SimpleRadioButtonTag($attributes) { 923 $this->SimpleWidget('input', $attributes); 924 if ($this->getAttribute('value') === false) { 925 $this->_setAttribute('value', 'on'); 926 } 927 } 928 929 /** 930 * Tag contains no content. 931 * @return boolean False. 932 * @access public 933 */ 934 function expectEndTag() { 935 return false; 936 } 937 938 /** 939 * The only allowed value sn the one in the 940 * "value" attribute. 941 * @param string $value New value. 942 * @return boolean True if allowed. 943 * @access public 944 */ 945 function setValue($value) { 946 if ($value === false) { 947 return parent::setValue($value); 948 } 949 if ($value !== $this->getAttribute('value')) { 950 return false; 951 } 952 return parent::setValue($value); 953 } 954 955 /** 956 * Accessor for starting value. 957 * @return string Parsed value. 958 * @access public 959 */ 960 function getDefault() { 961 if ($this->getAttribute('checked') !== false) { 962 return $this->getAttribute('value'); 963 } 964 return false; 965 } 966 } 967 968 /** 969 * Checkbox widget. 970 * @package SimpleTest 971 * @subpackage WebTester 972 */ 973 class SimpleCheckboxTag extends SimpleWidget { 974 975 /** 976 * Starts with attributes only. 977 * @param hash $attributes Attribute names and 978 * string values. 979 */ 980 function SimpleCheckboxTag($attributes) { 981 $this->SimpleWidget('input', $attributes); 982 if ($this->getAttribute('value') === false) { 983 $this->_setAttribute('value', 'on'); 984 } 985 } 986 987 /** 988 * Tag contains no content. 989 * @return boolean False. 990 * @access public 991 */ 992 function expectEndTag() { 993 return false; 994 } 995 996 /** 997 * The only allowed value in the one in the 998 * "value" attribute. The default for this 999 * attribute is "on". If this widget is set to 1000 * true, then the usual value will be taken. 1001 * @param string $value New value. 1002 * @return boolean True if allowed. 1003 * @access public 1004 */ 1005 function setValue($value) { 1006 if ($value === false) { 1007 return parent::setValue($value); 1008 } 1009 if ($value === true) { 1010 return parent::setValue($this->getAttribute('value')); 1011 } 1012 if ($value != $this->getAttribute('value')) { 1013 return false; 1014 } 1015 return parent::setValue($value); 1016 } 1017 1018 /** 1019 * Accessor for starting value. The default 1020 * value is "on". 1021 * @return string Parsed value. 1022 * @access public 1023 */ 1024 function getDefault() { 1025 if ($this->getAttribute('checked') !== false) { 1026 return $this->getAttribute('value'); 1027 } 1028 return false; 1029 } 1030 } 1031 1032 /** 1033 * A group of multiple widgets with some shared behaviour. 1034 * @package SimpleTest 1035 * @subpackage WebTester 1036 */ 1037 class SimpleTagGroup { 1038 protected $_widgets = array(); 1039 1040 /** 1041 * Adds a tag to the group. 1042 * @param SimpleWidget $widget 1043 * @access public 1044 */ 1045 function addWidget($widget) { 1046 $this->_widgets[] = $widget; 1047 } 1048 1049 /** 1050 * Accessor to widget set. 1051 * @return array All widgets. 1052 * @access protected 1053 */ 1054 function &_getWidgets() { 1055 return $this->_widgets; 1056 } 1057 1058 /** 1059 * Accessor for an attribute. 1060 * @param string $label Attribute name. 1061 * @return boolean Always false. 1062 * @access public 1063 */ 1064 function getAttribute($label) { 1065 return false; 1066 } 1067 1068 /** 1069 * Fetches the name for the widget from the first 1070 * member. 1071 * @return string Name of widget. 1072 * @access public 1073 */ 1074 function getName() { 1075 if (count($this->_widgets) > 0) { 1076 return $this->_widgets[0]->getName(); 1077 } 1078 } 1079 1080 /** 1081 * Scans the widgets for one with the appropriate 1082 * ID field. 1083 * @param string $id ID value to try. 1084 * @return boolean True if matched. 1085 * @access public 1086 */ 1087 function isId($id) { 1088 for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) { 1089 if ($this->_widgets[$i]->isId($id)) { 1090 return true; 1091 } 1092 } 1093 return false; 1094 } 1095 1096 /** 1097 * Scans the widgets for one with the appropriate 1098 * attached label. 1099 * @param string $label Attached label to try. 1100 * @return boolean True if matched. 1101 * @access public 1102 */ 1103 function isLabel($label) { 1104 for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) { 1105 if ($this->_widgets[$i]->isLabel($label)) { 1106 return true; 1107 } 1108 } 1109 return false; 1110 } 1111 1112 /** 1113 * Dispatches the value into the form encoded packet. 1114 * @param SimpleEncoding $encoding Form packet. 1115 * @access public 1116 */ 1117 function write($encoding) { 1118 $encoding->add($this->getName(), $this->getValue()); 1119 } 1120 } 1121 1122 /** 1123 * A group of tags with the same name within a form. 1124 * @package SimpleTest 1125 * @subpackage WebTester 1126 */ 1127 class SimpleCheckboxGroup extends SimpleTagGroup { 1128 1129 /** 1130 * Accessor for current selected widget or false 1131 * if none. 1132 * @return string/array Widget values or false if none. 1133 * @access public 1134 */ 1135 function getValue() { 1136 $values = array(); 1137 $widgets = $this->_getWidgets(); 1138 for ($i = 0, $count = count($widgets); $i < $count; $i++) { 1139 if ($widgets[$i]->getValue() !== false) { 1140 $values[] = $widgets[$i]->getValue(); 1141 } 1142 } 1143 return $this->_coerceValues($values); 1144 } 1145 1146 /** 1147 * Accessor for starting value that is active. 1148 * @return string/array Widget values or false if none. 1149 * @access public 1150 */ 1151 function getDefault() { 1152 $values = array(); 1153 $widgets = $this->_getWidgets(); 1154 for ($i = 0, $count = count($widgets); $i < $count; $i++) { 1155 if ($widgets[$i]->getDefault() !== false) { 1156 $values[] = $widgets[$i]->getDefault(); 1157 } 1158 } 1159 return $this->_coerceValues($values); 1160 } 1161 1162 /** 1163 * Accessor for current set values. 1164 * @param string/array/boolean $values Either a single string, a 1165 * hash or false for nothing set. 1166 * @return boolean True if all values can be set. 1167 * @access public 1168 */ 1169 function setValue($values) { 1170 $values = $this->_makeArray($values); 1171 if (! $this->_valuesArePossible($values)) { 1172 return false; 1173 } 1174 $widgets = $this->_getWidgets(); 1175 for ($i = 0, $count = count($widgets); $i < $count; $i++) { 1176 $possible = $widgets[$i]->getAttribute('value'); 1177 if (in_array($widgets[$i]->getAttribute('value'), $values)) { 1178 $widgets[$i]->setValue($possible); 1179 } else { 1180 $widgets[$i]->setValue(false); 1181 } 1182 } 1183 return true; 1184 } 1185 1186 /** 1187 * Tests to see if a possible value set is legal. 1188 * @param string/array/boolean $values Either a single string, a 1189 * hash or false for nothing set. 1190 * @return boolean False if trying to set a 1191 * missing value. 1192 * @access private 1193 */ 1194 function _valuesArePossible($values) { 1195 $matches = array(); 1196 $widgets = $this->_getWidgets(); 1197 for ($i = 0, $count = count($widgets); $i < $count; $i++) { 1198 $possible = $widgets[$i]->getAttribute('value'); 1199 if (in_array($possible, $values)) { 1200 $matches[] = $possible; 1201 } 1202 } 1203 return ($values == $matches); 1204 } 1205 1206 /** 1207 * Converts the output to an appropriate format. This means 1208 * that no values is false, a single value is just that 1209 * value and only two or more are contained in an array. 1210 * @param array $values List of values of widgets. 1211 * @return string/array/boolean Expected format for a tag. 1212 * @access private 1213 */ 1214 function _coerceValues($values) { 1215 if (count($values) == 0) { 1216 return false; 1217 } elseif (count($values) == 1) { 1218 return $values[0]; 1219 } else { 1220 return $values; 1221 } 1222 } 1223 1224 /** 1225 * Converts false or string into array. The opposite of 1226 * the coercian method. 1227 * @param string/array/boolean $value A single item is converted 1228 * to a one item list. False 1229 * gives an empty list. 1230 * @return array List of values, possibly empty. 1231 * @access private 1232 */ 1233 function _makeArray($value) { 1234 if ($value === false) { 1235 return array(); 1236 } 1237 if (is_string($value)) { 1238 return array($value); 1239 } 1240 return $value; 1241 } 1242 } 1243 1244 /** 1245 * A group of tags with the same name within a form. 1246 * Used for radio buttons. 1247 * @package SimpleTest 1248 * @subpackage WebTester 1249 */ 1250 class SimpleRadioGroup extends SimpleTagGroup { 1251 1252 /** 1253 * Each tag is tried in turn until one is 1254 * successfully set. The others will be 1255 * unchecked if successful. 1256 * @param string $value New value. 1257 * @return boolean True if any allowed. 1258 * @access public 1259 */ 1260 function setValue($value) { 1261 if (! $this->_valueIsPossible($value)) { 1262 return false; 1263 } 1264 $index = false; 1265 $widgets = $this->_getWidgets(); 1266 for ($i = 0, $count = count($widgets); $i < $count; $i++) { 1267 if (! $widgets[$i]->setValue($value)) { 1268 $widgets[$i]->setValue(false); 1269 } 1270 } 1271 return true; 1272 } 1273 1274 /** 1275 * Tests to see if a value is allowed. 1276 * @param string Attempted value. 1277 * @return boolean True if a valid value. 1278 * @access private 1279 */ 1280 function _valueIsPossible($value) { 1281 $widgets = $this->_getWidgets(); 1282 for ($i = 0, $count = count($widgets); $i < $count; $i++) { 1283 if ($widgets[$i]->getAttribute('value') == $value) { 1284 return true; 1285 } 1286 } 1287 return false; 1288 } 1289 1290 /** 1291 * Accessor for current selected widget or false 1292 * if none. 1293 * @return string/boolean Value attribute or 1294 * content of opton. 1295 * @access public 1296 */ 1297 function getValue() { 1298 $widgets = $this->_getWidgets(); 1299 for ($i = 0, $count = count($widgets); $i < $count; $i++) { 1300 if ($widgets[$i]->getValue() !== false) { 1301 return $widgets[$i]->getValue(); 1302 } 1303 } 1304 return false; 1305 } 1306 1307 /** 1308 * Accessor for starting value that is active. 1309 * @return string/boolean Value of first checked 1310 * widget or false if none. 1311 * @access public 1312 */ 1313 function getDefault() { 1314 $widgets = $this->_getWidgets(); 1315 for ($i = 0, $count = count($widgets); $i < $count; $i++) { 1316 if ($widgets[$i]->getDefault() !== false) { 1317 return $widgets[$i]->getDefault(); 1318 } 1319 } 1320 return false; 1321 } 1322 } 1323 1324 /** 1325 * Tag to keep track of labels. 1326 * @package SimpleTest 1327 * @subpackage WebTester 1328 */ 1329 class SimpleLabelTag extends SimpleTag { 1330 1331 /** 1332 * Starts with a named tag with attributes only. 1333 * @param hash $attributes Attribute names and 1334 * string values. 1335 */ 1336 function SimpleLabelTag($attributes) { 1337 $this->SimpleTag('label', $attributes); 1338 } 1339 1340 /** 1341 * Access for the ID to attach the label to. 1342 * @return string For attribute. 1343 * @access public 1344 */ 1345 function getFor() { 1346 return $this->getAttribute('for'); 1347 } 1348 } 1349 1350 /** 1351 * Tag to aid parsing the form. 1352 * @package SimpleTest 1353 * @subpackage WebTester 1354 */ 1355 class SimpleFormTag extends SimpleTag { 1356 1357 /** 1358 * Starts with a named tag with attributes only. 1359 * @param hash $attributes Attribute names and 1360 * string values. 1361 */ 1362 function SimpleFormTag($attributes) { 1363 $this->SimpleTag('form', $attributes); 1364 } 1365 } 1366 1367 /** 1368 * Tag to aid parsing the frames in a page. 1369 * @package SimpleTest 1370 * @subpackage WebTester 1371 */ 1372 class SimpleFrameTag extends SimpleTag { 1373 1374 /** 1375 * Starts with a named tag with attributes only. 1376 * @param hash $attributes Attribute names and 1377 * string values. 1378 */ 1379 function SimpleFrameTag($attributes) { 1380 $this->SimpleTag('frame', $attributes); 1381 } 1382 1383 /** 1384 * Tag contains no content. 1385 * @return boolean False. 1386 * @access public 1387 */ 1388 function expectEndTag() { 1389 return false; 1390 } 1391 } 1392 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |