/** * Convert the token * * @param \Zend\Markup\Token $token * @param string $text * * @return string */ public function __invoke(Token $token, $text) { $uri = $text; if (!preg_match('/^([a-z][a-z+\\-.]*):/i', $uri)) { $uri = 'http://' . $uri; } // check if the URL is valid // TODO: use \Zend\Uri for this if (!\Zend\Markup\Renderer\HTML::isValidUri($uri)) { return $text; } if ($token->hasAttribute('alt')) { $alt = $token->getAttribute('alt'); } else { // try to get the alternative from the URL $alt = rtrim($text, '/'); $alt = strrchr($alt, '/'); if (false !== strpos($alt, '.')) { $alt = substr($alt, 1, strpos($alt, '.') - 1); } } // run the URI and alt through htmlentities $uri = htmlentities($uri, ENT_QUOTES, $this->getEncoding()); $alt = htmlentities($alt, ENT_QUOTES, $this->getEncoding()); return "<img src=\"{$uri}\" alt=\"{$alt}\"" . $this->renderAttributes($token) . " />"; }
/** * Convert the token * * @param \Zend\Markup\Token $token * @param string $text * * @return string */ public function __invoke(Token $token, $text) { if ($token->hasAttribute('url')) { $uri = $token->getAttribute('url'); } else { $uri = $text; } if (!preg_match('/^([a-z][a-z+\-.]*):/i', $uri)) { $uri = 'http://' . $uri; } // check if the URL is valid // TODO: re-implement this (probably with the new \Zend\Uri) //if (!\Zend\Markup\Renderer\Html::isValidUri($uri)) { // return $text; //} $attributes = $this->renderAttributes($token); // run the URI through htmlentities $uri = htmlentities($uri, ENT_QUOTES, $this->getEncoding()); return "<a href=\"{$uri}\"{$attributes}>{$text}</a>"; }
/** * Convert the token * * @param Zend_Markup_Token $token * @param string $text * * @return string */ public function __invoke(\Zend\Markup\Token $token, $text) { $bar = $token->getAttribute('bar'); if (!empty($bar)) { $bar = '=' . $bar; } return "[foo{$bar}]" . $text . '[/foo]'; }
/** * Test the HTML code markup * * @return void */ public function testDefaultRendering() { $code = new CodeMarkup(); $token = new Token('codestart', Token::TYPE_MARKUP, 'code', array()); $token->setStopper('codeend'); $this->assertEquals(highlight_string('foo', true), $code($token, 'foo')); $this->assertEquals(highlight_string('<?= "bar" ?>', true), $code($token, '<?= "bar" ?>')); $this->assertEquals(highlight_string('<?php foobar(); ?>', true), $code($token, '<?php foobar(); ?>')); }
/** * Test the default HTML filtering * * @return void */ public function testFilter() { $markup = new ReplaceMarkup('strong'); $token = new Token('strong', Token::TYPE_MARKUP, 'strong', array()); $token->setStopper('endstrong'); $this->assertEquals('foo<bar>', $markup->filter('foo<bar>')); $this->assertEquals("foo<br />\nbar", $markup->filter("foo\nbar")); $this->assertEquals("foo<br />\n<bar>", $markup->filter("foo\n<bar>")); }
/** * Convert the token * * @param \Zend\Markup\Token $token * @param string $text * * @return string */ public function __invoke(Markup\Token $token, $text) { $type = null; if ($token->hasAttribute('list')) { // because '01' == '1' if ($token->getAttribute('list') === '01') { $type = 'decimal-leading-zero'; } else { switch ($token->getAttribute('list')) { case '1': $type = 'decimal'; break; case 'i': $type = 'lower-roman'; break; case 'I': $type = 'upper-roman'; break; case 'a': $type = 'lower-alpha'; break; case 'A': $type = 'upper-alpha'; break; // the following type is unsupported by IE (including IE8) case 'alpha': $type = 'lower-greek'; break; // the CSS names itself case 'armenian': // unsupported by IE (including IE8) case 'decimal': case 'decimal-leading-zero': // unsupported by IE (including IE8) case 'georgian': // unsupported by IE (including IE8) case 'lower-alpha': case 'lower-greek': // unsupported by IE (including IE8) case 'lower-latin': // unsupported by IE (including IE8) case 'lower-roman': case 'upper-alpha': case 'upper-latin': // unsupported by IE (including IE8) case 'upper-roman': $type = $token->getAttribute('list'); break; } } } if (null !== $type) { return "<ol style=\"list-style-type: {$type}\">{$text}</ol>"; } else { return "<ul>{$text}</ul>"; } }
/** * Test filtering * * @return void */ public function testFiltering() { // create a token list $tokenList = new TokenList(); // first create a root token $root = new Token('', Token::TYPE_MARKUP, 'Zend_Markup_Root'); $tokenList->addChild($root); // now add the actual markups to the root token $root->addChild(new Token('baz ', Token::TYPE_NONE, '', array(), $root)); $testMarkup = new Token('foohooo', Token::TYPE_MARKUP, 'test', array(), $root); $testMarkup->setStopper('bazzaa'); $root->addChild($testMarkup); // now add the content for the test markup $testMarkup->addChild(new Token('booh', Token::TYPE_NONE, '', array(), $testMarkup)); // add a filter to the test markup $this->_renderer->getMarkup('test')->addFilter(new StringToUpperFilter(), -1); $this->assertEquals('baz fooBOOHbar', $this->_renderer->render($tokenList)); }
/** * Render some attributes * * @param \Zend\Markup\Token $token * @return string */ public function renderAttributes(Markup\Token $token) { $return = ''; $tokenAttributes = $token->getAttributes(); /* * loop through all the available attributes, and check if there is * a value defined by the token * if there is no value defined by the token, use the default value or * don't set the attribute */ foreach ($this->_attributes as $attribute => $value) { if (isset($tokenAttributes[$attribute]) && !empty($tokenAttributes[$attribute])) { $return .= ' ' . $attribute . '="' . htmlentities($tokenAttributes[$attribute], ENT_QUOTES, $this->getEncoding()) . '"'; } elseif (!empty($value)) { $return .= ' ' . $attribute . '="' . htmlentities($value, ENT_QUOTES, $this->getEncoding()) . '"'; } } return $return; }
/** * Remove from searched stoppers * * @param \Zend\Markup\Token $token * @return void */ protected function _removeFromSearchedStoppers(Markup\Token $token) { $this->_checkTagDeclaration($token->getName()); foreach ($this->_tags[$token->getName()]['stoppers'] as $stopper) { --$this->_searchedStoppers[$stopper]; } }
/** * Render some attributes * * @param \Zend\Markup\Token $token * @param array $attributes * @return string */ public static function renderAttributes(Token $token, array $attributes = array()) { $attributes = array_merge(self::$_defaultAttributes, $attributes); $return = ''; $tokenAttributes = $token->getAttributes(); // correct style attribute if (isset($tokenAttributes['style'])) { $tokenAttributes['style'] = trim($tokenAttributes['style']); if ($tokenAttributes['style'][strlen($tokenAttributes['style']) - 1] != ';') { $tokenAttributes['style'] .= ';'; } } else { $tokenAttributes['style'] = ''; } // special treathment for 'align' and 'color' attribute if (isset($tokenAttributes['align'])) { $tokenAttributes['style'] .= 'text-align: ' . $tokenAttributes['align'] . ';'; unset($tokenAttributes['align']); } if (isset($tokenAttributes['color']) && self::checkColor($tokenAttributes['color'])) { $tokenAttributes['style'] .= 'color: ' . $tokenAttributes['color'] . ';'; unset($tokenAttributes['color']); } /* * loop through all the available attributes, and check if there is * a value defined by the token * if there is no value defined by the token, use the default value or * don't set the attribute */ foreach ($attributes as $attribute => $value) { if (isset($tokenAttributes[$attribute]) && !empty($tokenAttributes[$attribute])) { $return .= ' ' . $attribute . '="' . htmlentities($tokenAttributes[$attribute], ENT_QUOTES, self::getEncoding()) . '"'; } elseif (!empty($value)) { $return .= ' ' . $attribute . '="' . htmlentities($value, ENT_QUOTES, self::getEncoding()) . '"'; } } return $return; }
/** * Execute the token * * @param \Zend\Markup\Token $token * * @return string */ protected function _execute(Token $token) { switch ($token->getType()) { case Token::TYPE_MARKUP: if (!isset($this->_markups[$token->getName()])) { // TODO: apply filters return $this->_markup->filter($token->getContent()) . $this->_render($token) . $this->_markup->filter($token->getStopper()); } $markup = $this->_markups[$token->getName()]; // change the rendering environiment $oldMarkup = $this->_markup; $this->_markup = $markup; $value = $markup($token, $this->_render($token)); // and change the rendering environiment back $this->_markup = $oldMarkup; return $value; break; case Token::TYPE_NONE: default: return $this->_markup->filter($token->getContent()); break; } }
/** * Check if a tag is a stopper * * @param array $token * @param \Zend\Markup\Token $current * * @return bool */ protected function _isStopper(array $token, Markup\Token $current) { switch ($current->getName()) { case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6': case 'list': case 'li': if ($token['type'] == Markup\Token::TYPE_TAG && ($token['name'] == 'break' || $token['name'] == 'p')) { return true; } break; case 'break': return false; break; default: if ($token['type'] == Markup\Token::TYPE_TAG && $token['name'] == $current->getName()) { return true; } break; } return false; }
/** * Get the markup name * * @param \Zend\Markup\Token * * @return string */ protected function _getMarkupName(Markup\Token $token) { $name = $token->getName(); if (empty($name)) { return false; } return $this->_resolveMarkupName($name); }