Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletestlib/tag.php
Go to the documentation of this file.
00001 <?php
00012 require_once(dirname(__FILE__) . '/parser.php');
00013 require_once(dirname(__FILE__) . '/encoding.php');
00021 class SimpleTag {
00022     var $_name;
00023     var $_attributes;
00024     var $_content;
00025     
00034     function SimpleTag($name, $attributes) {
00035         $this->_name = strtolower(trim($name));
00036         $this->_attributes = $attributes;
00037         $this->_content = '';
00038     }
00039     
00046     function expectEndTag() {
00047         return true;
00048     }
00049     
00059     function isPrivateContent() {
00060         return false;
00061     }
00062 
00068     function addContent($content) {
00069         $this->_content .= (string)$content;
00070     }
00071     
00077     function addTag(&$tag) {
00078     }
00079     
00085     function getTagName() {
00086         return $this->_name;
00087     }
00088     
00094     function getChildElements() {
00095         return array();
00096     }
00097     
00104     function getAttribute($label) {
00105         $label = strtolower($label);
00106         if (! isset($this->_attributes[$label])) {
00107             return false;
00108         }
00109         return (string)$this->_attributes[$label];
00110     }
00111     
00118     function _setAttribute($label, $value) {
00119         $this->_attributes[strtolower($label)] = $value;
00120     }
00121     
00127     function getContent() {
00128         return $this->_content;
00129     }
00130     
00138     function getText() {
00139         return SimpleHtmlSaxParser::normalise($this->_content);
00140     }
00141     
00148     function isId($id) {
00149         return ($this->getAttribute('id') == $id);
00150     }
00151 }
00152 
00158 class SimpleBaseTag extends SimpleTag {
00159     
00165     function SimpleBaseTag($attributes) {
00166         $this->SimpleTag('base', $attributes);
00167     }
00168 
00174     function expectEndTag() {
00175         return false;
00176     }
00177 }
00178 
00184 class SimpleTitleTag extends SimpleTag {
00185     
00191     function SimpleTitleTag($attributes) {
00192         $this->SimpleTag('title', $attributes);
00193     }
00194 }
00195 
00201 class SimpleAnchorTag extends SimpleTag {
00202     
00208     function SimpleAnchorTag($attributes) {
00209         $this->SimpleTag('a', $attributes);
00210     }
00211     
00217     function getHref() {
00218         $url = $this->getAttribute('href');
00219         if (is_bool($url)) {
00220             $url = '';
00221         }
00222         return $url;
00223     }
00224 }
00225 
00231 class SimpleWidget extends SimpleTag {
00232     var $_value;
00233     var $_label;
00234     var $_is_set;
00235     
00242     function SimpleWidget($name, $attributes) {
00243         $this->SimpleTag($name, $attributes);
00244         $this->_value = false;
00245         $this->_label = false;
00246         $this->_is_set = false;
00247     }
00248     
00255     function getName() {
00256         return $this->getAttribute('name');
00257     }
00258     
00264     function getDefault() {
00265         return $this->getAttribute('value');
00266     }
00267     
00275     function getValue() {
00276         if (! $this->_is_set) {
00277             return $this->getDefault();
00278         }
00279         return $this->_value;
00280     }
00281     
00288     function setValue($value) {
00289         $this->_value = $value;
00290         $this->_is_set = true;
00291         return true;
00292     }
00293     
00299     function resetValue() {
00300         $this->_is_set = false;
00301     }
00302     
00309     function setLabel($label) {
00310         $this->_label = trim($label);
00311     }
00312     
00319     function isLabel($label) {
00320         return $this->_label == trim($label);
00321     }
00322     
00328     function write(&$encoding) {
00329         if ($this->getName()) {
00330             $encoding->add($this->getName(), $this->getValue());
00331         }
00332     }
00333 }
00334 
00340 class SimpleTextTag extends SimpleWidget {
00341     
00347     function SimpleTextTag($attributes) {
00348         $this->SimpleWidget('input', $attributes);
00349         if ($this->getAttribute('value') === false) {
00350             $this->_setAttribute('value', '');
00351         }
00352     }
00353     
00359     function expectEndTag() {
00360         return false;
00361     }
00362     
00370     function setValue($value) {
00371         if ($this->getAttribute('type') == 'hidden') {
00372             return false;
00373         }
00374         return parent::setValue($value);
00375     }
00376 }
00377 
00383 class SimpleSubmitTag extends SimpleWidget {
00384     
00390     function SimpleSubmitTag($attributes) {
00391         $this->SimpleWidget('input', $attributes);
00392         if ($this->getAttribute('value') === false) {
00393             $this->_setAttribute('value', 'Submit');
00394         }
00395     }
00396     
00402     function expectEndTag() {
00403         return false;
00404     }
00405     
00412     function setValue($value) {
00413         return false;
00414     }
00415     
00421     function getLabel() {
00422         return $this->getValue();
00423     }
00424     
00431     function isLabel($label) {
00432         return trim($label) == trim($this->getLabel());
00433     }
00434 }
00435     
00441 class SimpleImageSubmitTag extends SimpleWidget {
00442     
00448     function SimpleImageSubmitTag($attributes) {
00449         $this->SimpleWidget('input', $attributes);
00450     }
00451     
00457     function expectEndTag() {
00458         return false;
00459     }
00460     
00467     function setValue($value) {
00468         return false;
00469     }
00470     
00476     function getLabel() {
00477         if ($this->getAttribute('title')) {
00478             return $this->getAttribute('title');
00479         }
00480         return $this->getAttribute('alt');
00481     }
00482     
00489     function isLabel($label) {
00490         return trim($label) == trim($this->getLabel());
00491     }
00492     
00500     function write(&$encoding, $x, $y) {
00501         if ($this->getName()) {
00502             $encoding->add($this->getName() . '.x', $x);
00503             $encoding->add($this->getName() . '.y', $y);
00504         } else {
00505             $encoding->add('x', $x);
00506             $encoding->add('y', $y);
00507         }
00508     }
00509 }
00510     
00516 class SimpleButtonTag extends SimpleWidget {
00517     
00524     function SimpleButtonTag($attributes) {
00525         $this->SimpleWidget('button', $attributes);
00526     }
00527     
00534     function expectEndTag() {
00535         return true;
00536     }
00537     
00544     function setValue($value) {
00545         return false;
00546     }
00547     
00553     function getLabel() {
00554         return $this->getContent();
00555     }
00556     
00563     function isLabel($label) {
00564         return trim($label) == trim($this->getLabel());
00565     }
00566 }
00567 
00573 class SimpleTextAreaTag extends SimpleWidget {
00574     
00580     function SimpleTextAreaTag($attributes) {
00581         $this->SimpleWidget('textarea', $attributes);
00582     }
00583     
00589     function getDefault() {
00590         return $this->_wrap(SimpleHtmlSaxParser::decodeHtml($this->getContent()));
00591     }
00592     
00599     function setValue($value) {
00600         return parent::setValue($this->_wrap($value));
00601     }
00602     
00608     function _wrapIsEnabled() {
00609         if ($this->getAttribute('cols')) {
00610             $wrap = $this->getAttribute('wrap');
00611             if (($wrap == 'physical') || ($wrap == 'hard')) {
00612                 return true;
00613             }
00614         }
00615         return false;
00616     }
00617     
00628     function _wrap($text) {
00629         $text = str_replace("\r\r\n", "\r\n", str_replace("\n", "\r\n", $text));
00630         $text = str_replace("\r\n\n", "\r\n", str_replace("\r", "\r\n", $text));
00631         if (strncmp($text, "\r\n", strlen("\r\n")) == 0) {
00632             $text = substr($text, strlen("\r\n"));
00633         }
00634         if ($this->_wrapIsEnabled()) {
00635             return wordwrap(
00636                     $text,
00637                     (integer)$this->getAttribute('cols'),
00638                     "\r\n");
00639         }
00640         return $text;
00641     }
00642     
00648     function isPrivateContent() {
00649         return true;
00650     }
00651 }
00652 
00658 class SimpleUploadTag extends SimpleWidget {
00659     
00665     function SimpleUploadTag($attributes) {
00666         $this->SimpleWidget('input', $attributes);
00667     }
00668     
00674     function expectEndTag() {
00675         return false;
00676     }
00677     
00683     function write(&$encoding) {
00684         if (! file_exists($this->getValue())) {
00685             return;
00686         }
00687         $encoding->attach(
00688                 $this->getName(),
00689                 implode('', file($this->getValue())),
00690                 basename($this->getValue()));
00691     }
00692 }
00693 
00699 class SimpleSelectionTag extends SimpleWidget {
00700     var $_options;
00701     var $_choice;
00702     
00708     function SimpleSelectionTag($attributes) {
00709         $this->SimpleWidget('select', $attributes);
00710         $this->_options = array();
00711         $this->_choice = false;
00712     }
00713     
00719     function addTag(&$tag) {
00720         if ($tag->getTagName() == 'option') {
00721             $this->_options[] = &$tag;
00722         }
00723     }
00724     
00730     function addContent($content) {
00731     }
00732     
00739     function getDefault() {
00740         for ($i = 0, $count = count($this->_options); $i < $count; $i++) {
00741             if ($this->_options[$i]->getAttribute('selected') !== false) {
00742                 return $this->_options[$i]->getDefault();
00743             }
00744         }
00745         if ($count > 0) {
00746             return $this->_options[0]->getDefault();
00747         }
00748         return '';
00749     }
00750     
00757     function setValue($value) {
00758         for ($i = 0, $count = count($this->_options); $i < $count; $i++) {
00759             if ($this->_options[$i]->isValue($value)) {
00760                 $this->_choice = $i;
00761                 return true;
00762             }
00763         }
00764         return false;
00765     }
00766     
00773     function getValue() {
00774         if ($this->_choice === false) {
00775             return $this->getDefault();
00776         }
00777         return $this->_options[$this->_choice]->getValue();
00778     }
00779 }
00780 
00786 class MultipleSelectionTag extends SimpleWidget {
00787     var $_options;
00788     var $_values;
00789     
00795     function MultipleSelectionTag($attributes) {
00796         $this->SimpleWidget('select', $attributes);
00797         $this->_options = array();
00798         $this->_values = false;
00799     }
00800     
00806     function addTag(&$tag) {
00807         if ($tag->getTagName() == 'option') {
00808             $this->_options[] = &$tag;
00809         }
00810     }
00811     
00817     function addContent($content) {
00818     }
00819     
00826     function getDefault() {
00827         $default = array();
00828         for ($i = 0, $count = count($this->_options); $i < $count; $i++) {
00829             if ($this->_options[$i]->getAttribute('selected') !== false) {
00830                 $default[] = $this->_options[$i]->getDefault();
00831             }
00832         }
00833         return $default;
00834     }
00835     
00844     function setValue($desired) {
00845         $achieved = array();
00846         foreach ($desired as $value) {
00847             $success = false;
00848             for ($i = 0, $count = count($this->_options); $i < $count; $i++) {
00849                 if ($this->_options[$i]->isValue($value)) {
00850                     $achieved[] = $this->_options[$i]->getValue();
00851                     $success = true;
00852                     break;
00853                 }
00854             }
00855             if (! $success) {
00856                 return false;
00857             }
00858         }
00859         $this->_values = $achieved;
00860         return true;
00861     }
00862     
00868     function getValue() {
00869         if ($this->_values === false) {
00870             return $this->getDefault();
00871         }
00872         return $this->_values;
00873     }
00874 }
00875 
00881 class SimpleOptionTag extends SimpleWidget {
00882     
00886     function SimpleOptionTag($attributes) {
00887         $this->SimpleWidget('option', $attributes);
00888     }
00889     
00896     function setValue($value) {
00897         return false;
00898     }
00899     
00906     function isValue($compare) {
00907         $compare = trim($compare);
00908         if (trim($this->getValue()) == $compare) {
00909             return true;
00910         }
00911         return trim($this->getContent()) == $compare;
00912     }
00913     
00920     function getDefault() {
00921         if ($this->getAttribute('value') === false) {
00922             return $this->getContent();
00923         }
00924         return $this->getAttribute('value');
00925     }
00926     
00932     function isPrivateContent() {
00933         return true;
00934     }
00935 }
00936 
00942 class SimpleRadioButtonTag extends SimpleWidget {
00943     
00948     function SimpleRadioButtonTag($attributes) {
00949         $this->SimpleWidget('input', $attributes);
00950         if ($this->getAttribute('value') === false) {
00951             $this->_setAttribute('value', 'on');
00952         }
00953     }
00954     
00960     function expectEndTag() {
00961         return false;
00962     }
00963     
00971     function setValue($value) {
00972         if ($value === false) {
00973             return parent::setValue($value);
00974         }
00975         if ($value != $this->getAttribute('value')) {
00976             return false;
00977         }
00978         return parent::setValue($value);
00979     }
00980     
00986     function getDefault() {
00987         if ($this->getAttribute('checked') !== false) {
00988             return $this->getAttribute('value');
00989         }
00990         return false;
00991     }
00992 }
00993 
00999 class SimpleCheckboxTag extends SimpleWidget {
01000     
01006     function SimpleCheckboxTag($attributes) {
01007         $this->SimpleWidget('input', $attributes);
01008         if ($this->getAttribute('value') === false) {
01009             $this->_setAttribute('value', 'on');
01010         }
01011     }
01012     
01018     function expectEndTag() {
01019         return false;
01020     }
01021     
01031     function setValue($value) {
01032         if ($value === false) {
01033             return parent::setValue($value);
01034         }
01035         if ($value === true) {
01036             return parent::setValue($this->getAttribute('value'));
01037         }
01038         if ($value != $this->getAttribute('value')) {
01039             return false;
01040         }
01041         return parent::setValue($value);
01042     }
01043     
01050     function getDefault() {
01051         if ($this->getAttribute('checked') !== false) {
01052             return $this->getAttribute('value');
01053         }
01054         return false;
01055     }
01056 }
01057 
01063 class SimpleTagGroup {
01064     var $_widgets = array();
01065 
01071     function addWidget(&$widget) {
01072         $this->_widgets[] = &$widget;
01073     }
01074     
01080     function &_getWidgets() {
01081         return $this->_widgets;
01082     }
01083 
01090     function getAttribute($label) {
01091         return false;
01092     }
01093     
01100     function getName() {
01101         if (count($this->_widgets) > 0) {
01102             return $this->_widgets[0]->getName();
01103         }
01104     }
01105     
01113     function isId($id) {
01114         for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
01115             if ($this->_widgets[$i]->isId($id)) {
01116                 return true;
01117             }
01118         }
01119         return false;
01120     }
01121     
01129     function isLabel($label) {
01130         for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
01131             if ($this->_widgets[$i]->isLabel($label)) {
01132                 return true;
01133             }
01134         }
01135         return false;
01136     }
01137     
01143     function write(&$encoding) {
01144         $encoding->add($this->getName(), $this->getValue());
01145     }
01146 }
01147 
01153 class SimpleCheckboxGroup extends SimpleTagGroup {
01154     
01161     function getValue() {
01162         $values = array();
01163         $widgets = &$this->_getWidgets();
01164         for ($i = 0, $count = count($widgets); $i < $count; $i++) {
01165             if ($widgets[$i]->getValue() !== false) {
01166                 $values[] = $widgets[$i]->getValue();
01167             }
01168         }
01169         return $this->_coerceValues($values);
01170     }
01171     
01177     function getDefault() {
01178         $values = array();
01179         $widgets = &$this->_getWidgets();
01180         for ($i = 0, $count = count($widgets); $i < $count; $i++) {
01181             if ($widgets[$i]->getDefault() !== false) {
01182                 $values[] = $widgets[$i]->getDefault();
01183             }
01184         }
01185         return $this->_coerceValues($values);
01186     }
01187     
01195     function setValue($values) {
01196         $values = $this->_makeArray($values);
01197         if (! $this->_valuesArePossible($values)) {
01198             return false;
01199         }
01200         $widgets = &$this->_getWidgets();
01201         for ($i = 0, $count = count($widgets); $i < $count; $i++) {
01202             $possible = $widgets[$i]->getAttribute('value');
01203             if (in_array($widgets[$i]->getAttribute('value'), $values)) {
01204                 $widgets[$i]->setValue($possible);
01205             } else {
01206                 $widgets[$i]->setValue(false);
01207             }
01208         }
01209         return true;
01210     }
01211     
01220     function _valuesArePossible($values) {
01221         $matches = array();
01222         $widgets = &$this->_getWidgets();
01223         for ($i = 0, $count = count($widgets); $i < $count; $i++) {
01224             $possible = $widgets[$i]->getAttribute('value');
01225             if (in_array($possible, $values)) {
01226                 $matches[] = $possible;
01227             }
01228         }
01229         return ($values == $matches);
01230     }
01231     
01240     function _coerceValues($values) {
01241         if (count($values) == 0) {
01242             return false;
01243         } elseif (count($values) == 1) {
01244             return $values[0];
01245         } else {
01246             return $values;
01247         }
01248     }
01249     
01259     function _makeArray($value) {
01260         if ($value === false) {
01261             return array();
01262         }
01263         if (is_string($value)) {
01264             return array($value);
01265         }
01266         return $value;
01267     }
01268 }
01269 
01276 class SimpleRadioGroup extends SimpleTagGroup {
01277     
01286     function setValue($value) {
01287         if (! $this->_valueIsPossible($value)) {
01288             return false;
01289         }
01290         $index = false;
01291         $widgets = &$this->_getWidgets();
01292         for ($i = 0, $count = count($widgets); $i < $count; $i++) {
01293             if (! $widgets[$i]->setValue($value)) {
01294                 $widgets[$i]->setValue(false);
01295             }
01296         }
01297         return true;
01298     }
01299     
01306     function _valueIsPossible($value) {
01307         $widgets = &$this->_getWidgets();
01308         for ($i = 0, $count = count($widgets); $i < $count; $i++) {
01309             if ($widgets[$i]->getAttribute('value') == $value) {
01310                 return true;
01311             }
01312         }
01313         return false;
01314     }
01315     
01323     function getValue() {
01324         $widgets = &$this->_getWidgets();
01325         for ($i = 0, $count = count($widgets); $i < $count; $i++) {
01326             if ($widgets[$i]->getValue() !== false) {
01327                 return $widgets[$i]->getValue();
01328             }
01329         }
01330         return false;
01331     }
01332     
01339     function getDefault() {
01340         $widgets = &$this->_getWidgets();
01341         for ($i = 0, $count = count($widgets); $i < $count; $i++) {
01342             if ($widgets[$i]->getDefault() !== false) {
01343                 return $widgets[$i]->getDefault();
01344             }
01345         }
01346         return false;
01347     }
01348 }
01349 
01355 class SimpleLabelTag extends SimpleTag {
01356     
01362     function SimpleLabelTag($attributes) {
01363         $this->SimpleTag('label', $attributes);
01364     }
01365     
01371     function getFor() {
01372         return $this->getAttribute('for');
01373     }
01374 }
01375 
01381 class SimpleFormTag extends SimpleTag {
01382     
01388     function SimpleFormTag($attributes) {
01389         $this->SimpleTag('form', $attributes);
01390     }
01391 }
01392 
01398 class SimpleFrameTag extends SimpleTag {
01399     
01405     function SimpleFrameTag($attributes) {
01406         $this->SimpleTag('frame', $attributes);
01407     }
01408     
01414     function expectEndTag() {
01415         return false;
01416     }
01417 }
01418 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations