diff --git a/cake/libs/view/helpers/html.php b/cake/libs/view/helpers/html.php index 2b77edb99..4c13bd301 100644 --- a/cake/libs/view/helpers/html.php +++ b/cake/libs/view/helpers/html.php @@ -45,7 +45,8 @@ class HtmlHelper extends AppHelper { * @var array */ var $tags = array( - 'metalink' => '', + 'meta' => '', + 'metalink' => '', 'link' => '%s', 'mailto' => '%s', 'form' => '
', @@ -184,43 +185,61 @@ class HtmlHelper extends AppHelper { } } /** - * Creates a link to an external resource + * Creates a link to an external resource and handles basic meta tags * * @param string $title The title of the external resource - * @param mixed $url The address of the external resource + * @param mixed $url The address of the external resource or string for content attribute * @param array $attributes Other attributes for the generated tag. If the type attribute is html, rss, atom, or icon, the mime-type is returned. * @param boolean $inline If set to false, the generated tag appears in the head tag of the layout. * @return string */ - function meta($title = null, $url = null, $attributes = array(), $inline = true) { - $types = array( - 'html' => 'text/html', - 'rss' => 'application/rss+xml', - 'atom' => 'application/atom+xml', - 'icon' => 'image/x-icon' - ); + function meta($type = null, $url = null, $attributes = array(), $inline = true) { + if (!is_array($type)) { + $types = array( + 'rss' => array('type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $url), + 'atom' => array('type' => 'application/atom+xml', 'title' => $type, 'link' => $url), + 'icon' => array('type' => 'image/x-icon', 'rel' => 'icon', 'link' => $url), + 'keywords' => array('name' => 'keywords', 'content' => $url), + 'description' => array('name' => 'description', 'content' => $url), + ); - if (!isset($attributes['type']) && is_array($url) && isset($url['ext'])) { - if (in_array($url['ext'], array_keys($types))) { - $attributes['type'] = $url['ext']; - } else { - $attributes['type'] = 'rss'; + if ($type === 'icon' && $url === null) { + $types['icon']['link'] = $this->webroot('favicon.ico'); + } + + if (isset($types[$type])) { + $type = $types[$type]; + } elseif (!isset($types['type']) && !isset($attributes['type']) && $url !== null) { + if (is_array($url) && isset($url['ext'])) { + $type = $types[$url['ext']]; + } else { + $type = $types['rss']; + } + } elseif (isset($attributes['type']) && isset($types[$attributes['type']])) { + $type = $types[$attributes['type']]; + } + } else { + if ($url !== null) { + $inline = $url; } - } elseif (!isset($attributes['type'])) { - $attributes['type'] = 'rss'; } - if (isset($attributes['type']) && in_array($attributes['type'], array_keys($types))) { - $attributes['type'] = $types[$attributes['type']]; - } + $attributes = array_merge($type, $attributes); - if (!isset($attributes['rel'])) { - $attributes['rel'] = 'alternate'; + if (isset($attributes['link'])) { + if (isset($attributes['rel']) && $attributes['rel'] === 'icon') { + $attributes['rel'] = 'shortcut icon'; + $out .= sprintf($this->tags['metalink'], $attributes['link'], $this->_parseAttributes($attributes, array('link'))); + } else { + $attributes['link'] = $this->url($attributes['link'], true); + } + $out = sprintf($this->tags['metalink'], $attributes['link'], $this->_parseAttributes($attributes, array('link'))); + } else { + $out = sprintf($this->tags['meta'], $this->_parseAttributes($attributes, array('type'))); } - $out = $this->output(sprintf($this->tags['metalink'], $this->url($url, true), $title, $this->_parseAttributes($attributes))); if ($inline) { - return $out; + return $this->output($out); } else { $view =& ClassRegistry::getObject('view'); $view->addScript($out); @@ -528,7 +547,7 @@ class HtmlHelper extends AppHelper { * @access public */ function nestedList($list, $attributes = array(), $itemAttributes = array(), $tag = 'ul') { - if(is_string($attributes)) { + if (is_string($attributes)) { $tag = $attributes; $attributes = array(); } diff --git a/cake/tests/cases/libs/view/helpers/html.test.php b/cake/tests/cases/libs/view/helpers/html.test.php index 3788a2718..77c92f2e6 100644 --- a/cake/tests/cases/libs/view/helpers/html.test.php +++ b/cake/tests/cases/libs/view/helpers/html.test.php @@ -212,6 +212,40 @@ class HtmlHelperTest extends UnitTestCase { $this->assertPattern('/]*class="item"[^<>]*>Item 5\s*]*class="list"[^<>]*>\s*]*class="item"[^<>]*>Item 5.1<\/li>\s*]*class="item"[^<>]*>Item 5.2<\/li>\s*<\/ul>\s*<\/li>/', $result); } + function testMeta() { + + $result = $this->Html->meta('this is an rss feed', array('controller'=> 'posts', 'ext' => 'rss')); + $this->assertPattern('/^]+href=".*\/posts\.rss"[^<>]+\/>$/', $result); + $this->assertPattern('/^]+rel="alternate"[^<>]+\/>$/', $result); + $this->assertPattern('/^]+type="application\/rss\+xml"[^<>]+\/>$/', $result); + $this->assertPattern('/^]+title="this is an rss feed"\/>$/', $result); + + $result = $this->Html->meta('rss', array('controller'=> 'posts', 'ext' => 'rss'), array('title' => 'this is an rss feed')); + $this->assertPattern('/^]+href=".*\/posts\.rss"[^<>]+\/>$/', $result); + $this->assertPattern('/^]+rel="alternate"[^<>]+\/>$/', $result); + $this->assertPattern('/^]+type="application\/rss\+xml"[^<>]+\/>$/', $result); + $this->assertPattern('/^]+title="this is an rss feed"\/>$/', $result); + + $result = $this->Html->meta('icon', 'favicon.ico'); + $this->assertPattern('/^]+href=".*favicon\.ico"[^<>]+\/>$/', $result); + $this->assertPattern('/^]+type="image\/x-icon"[^<>]+/', $result); + $this->assertPattern('/^]+rel="icon"\/>[^<>]*/', $result); + $this->assertPattern('/]+rel="shortcut icon"\/>[^<>]*/', $result); + + $result = $this->Html->meta('keywords', 'these, are, some, meta, keywords'); + $this->assertPattern('/^]+name="keywords"[^<>]+\/>$/', $result); + $this->assertPattern('/^]+content="these, are, some, meta, keywords"\/>$/', $result); + + $result = $this->Html->meta('description', 'this is the meta description'); + $this->assertPattern('/^]+name="description"[^<>]+\/>$/', $result); + $this->assertPattern('/^]+content="this is the meta description"\/>$/', $result); + + + $result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL')); + $this->assertPattern('/^]+name="ROBOTS"[^<>]+\/>$/', $result); + $this->assertPattern('/^]+content="ALL"\/>$/', $result); + } + function tearDown() { unset($this->Html); }