Example #1
0
File: csml.php Project: soska/dte
 /**
  * Generate HTML tags using CSS selectors
  *
  * @param string $selector 
  * @param string $comment 
  * @return void
  * @author Armando Sosa
  */
 function tag($selector = "div", $attr = '', $format = BREAK_AND_TABS, $comment = '')
 {
     // initialize variables
     $element = $id = $classes = '';
     $tagMask = '<%1$s%4$s%2$s%3$s%5$s>';
     // ugly, I know. Translation 1:element, 4: attributes, 2:id, 3:classes, 5:auto close tag
     $autoClose = '';
     $tabs = '';
     if (is_array($attr)) {
         $attr = self::attributes($attr);
     }
     // parse bracked attributes;
     list($selector, $attr) = self::getBracketAttributes($selector, $attr);
     // separate selector into parts
     list($element, $id, $classes) = self::getSelectorParts($selector);
     // defaul $element to 'div'
     $element = empty($element) ? 'div' : $element;
     // if there's a closing slash in element name
     $slashPosition = strpos($element, "/");
     // if the slash is at the start,  we skip the attributes processing
     if ($slashPosition === 0) {
         $idclasses = "{$id}{$classes}";
         if (empty($comment) && !empty($idclasses)) {
             $comment = "/{$idclasses}";
         }
         $id = $classes = $attr = '';
     } else {
         // if the slash is at the end of the element, then set the autoclose variable
         $length = strlen($element) - 1;
         if ($slashPosition == $length && $length != 0) {
             $element = str_replace('/', '', $element);
             $autoClose = '/';
         }
         $id = !empty($id) ? " id=\"" . str_replace('#', '', $id) . "\"" : $id;
         $classes = !empty($classes) ? " class=\"" . trim(str_replace('.', ' ', $classes)) . "\"" : $classes;
     }
     // not a closing element
     if ($slashPosition === false) {
         self::$chain[] = $element;
         self::$tabsCount = count(self::$chain);
     } else {
         self::$tabsCount = count(self::$chain);
         $chain = (array) self::$chain;
         self::$chain = array_slice($chain, 0, -1);
     }
     $comment = !empty($comment) ? "<!-- {$comment} -->" : $comment;
     $tag = sprintf($tagMask, $element, $id, $classes, $attr, $autoClose);
     $tag = html_entity_decode($tag);
     // formatting options
     return self::format($tag . $comment, $format, $slashPosition !== false);
 }