Example #1
0
 /**
  * Parse the node and its content into an HTML tag.
  *
  * @access public
  * @param array $tag
  * @param string $content
  * @return string
  */
 public function parse(array $tag, $content)
 {
     $setup = $this->tag($tag['tag']);
     $xhtml = $this->getParser()->config('xhtml');
     $content = trim($content);
     if (empty($setup)) {
         return;
     }
     // If content doesn't match the pattern, don't wrap in a tag
     if (!empty($setup['pattern'])) {
         if ($setup['testNoDefault'] && !isset($tag['attributes']['default']) && !preg_match($setup['pattern'], $content)) {
             return sprintf('(Invalid %s)', $tag['tag']);
         }
     }
     // Add linebreaks
     switch ($setup['lineBreaks']) {
         case self::NL_REMOVE:
             $content = str_replace(array("\n", "\r"), "", $content);
             break;
         case self::NL_CONVERT:
             $content = Decoda::nl2br($content, $xhtml);
             break;
     }
     // Format attributes
     $attributes = array();
     $attr = '';
     if (!empty($tag['attributes'])) {
         foreach ($tag['attributes'] as $key => $value) {
             if (isset($setup['map'][$key])) {
                 $key = $setup['map'][$key];
             }
             if ($key === 'default' || substr($value, 0, 11) === 'javascript:') {
                 continue;
             }
             if ($setup['escapeAttributes']) {
                 $attributes[$key] = htmlentities($value, ENT_QUOTES, 'UTF-8');
             } else {
                 $attributes[$key] = $value;
             }
         }
     }
     if (!empty($setup['html'])) {
         $attributes += $setup['html'];
     }
     // Use a template if it exists
     if (!empty($setup['template'])) {
         $tag['attributes'] = $attributes;
         $templateEngine = $this->getParser()->getTemplateEngine();
         $templateEngine->setFilter($this);
         $parsed = $templateEngine->render($tag, $content);
         if ($setup['lineBreaks'] !== self::NL_PRESERVE) {
             $parsed = str_replace(array("\n", "\r"), "", $parsed);
         }
         return $parsed;
     }
     foreach ($attributes as $key => $value) {
         $attr .= ' ' . $key . '="' . $value . '"';
     }
     // Build HTML tag
     $html = $setup['tag'];
     if (is_array($html)) {
         $html = $html[$xhtml];
     }
     $parsed = '<' . $html . $attr;
     if ($setup['autoClose']) {
         $parsed .= $xhtml ? '/>' : '>';
     } else {
         $parsed .= '>';
         if ($tag['tag'] == 'url') {
             $parsed .= '&nbsp;';
         }
         if (!empty($tag['content'])) {
             $parsed .= $tag['content'];
         } else {
             $parsed .= $content;
         }
         $parsed .= '</' . $html . '>';
     }
     return $parsed;
 }