Exemple #1
0
 /**
  * Generates a string from an attribute array
  * @method attributes
  * @static
  * @protected
  * @param {array} $attributes Associative array of name => value pairs.
  * @param {string} [$between=' '] The text to insert between the attribute="value"
  * @param {string} [$escape=true] Whether to escape the attribute names and values.
  * @param {string} [$tag=null]
  * @return {string}
  */
 public static function attributes(array $attributes, $between = ' ', $escape = true, $tag = null)
 {
     $cacheBust = null;
     if (isset($attributes['cacheBust'])) {
         $cacheBust = $attributes['cacheBust'];
         unset($attributes['cacheBust']);
     }
     if (Q_Config::get('Q', 'html', 'w3c', true)) {
         $defaults = array('img' => array('src' => '', 'alt' => 'image'), 'a' => array('href' => '#missing_href'), 'form' => array('action' => ''), 'textarea' => array('rows' => 5, 'cols' => 20), 'meta' => array('content' => ''), 'applet' => array('width' => '300px', 'height' => '100px'), 'optgroup' => array('label' => 'group'), 'map' => array('name' => 'map'), 'param' => array('name' => '_Q_missing'), 'basefont' => array('size' => '100'), 'bdo' => array('dir' => '/'), 'script' => array('type' => 'text/javascript'), 'style' => array('type' => 'text/css'), 'object' => array('classid' => "_Q_missing", 'codebase' => "http://download.macromedia.com/pub/shockwave /cabs/flash/swflash.cab#version=9,0,115,0", 'width' => '550', 'height' => '400'));
         if (isset($defaults[$tag]) and is_array($defaults[$tag])) {
             $attributes = array_merge($defaults[$tag], $attributes);
         }
     }
     $result = '';
     $i = 0;
     foreach ($attributes as $name => $value) {
         if (!isset($value)) {
             continue;
             // skip null attributes
         }
         $name2 = $name;
         if (strpos(strtolower($tag), 'frame') !== false and strtolower($name) == 'src') {
             $name2 = 'href';
             // treat the src as href
         }
         if (strtolower($tag) == 'link' and strtolower($name) == 'href') {
             $name2 = 'src';
             // treat the href as src
         }
         $isUrl = false;
         switch (strtolower($name2)) {
             case 'href':
                 // Automatic unrouting of this attribute
                 $href = true;
             case 'action':
                 // Automatic unrouting of this attribute
                 $value = Q_Uri::url($value);
                 if ($value === false) {
                     $value = '#_Q_bad_url';
                 }
                 $isUrl = true;
                 break;
             case 'src':
                 // Automatically prefixes theme url if any
                 list($value, $filename) = self::themedUrlAndFilename($value);
                 $isUrl = true;
                 break;
             case 'id':
                 // Automatic prefixing of this attribute
             // Automatic prefixing of this attribute
             case 'for':
                 // For labels, too
                 if ($prefix = self::getIdPrefix()) {
                     $value = $prefix . $value;
                 }
                 $isUrl = true;
                 break;
         }
         if ($isUrl and isset($cacheBust)) {
             $value = Q_Uri::cacheBust($value, $cacheBust);
         }
         if ($escape) {
             $name = self::text($name);
             $value = self::text($value);
         }
         $result .= ($i > 0 ? $between : '') . $name . '="' . $value . '"';
         ++$i;
     }
     return $result;
 }