Skip to content

Commit

Permalink
Fix issues with '#first' as a url.
Browse files Browse the repository at this point in the history
Reduce variables used as well.
  • Loading branch information
markstory committed Jul 4, 2012
1 parent 5aaa5c5 commit d984ceb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
20 changes: 9 additions & 11 deletions lib/Cake/Routing/Router.php
Expand Up @@ -660,11 +660,14 @@ public static function url($url = null, $options = array()) {
$options = array();
}
$urlType = gettype($url);
$hasColonSlash = $hasLeadingSlash = $isJavascript = $isMailto = false;
$hasColonSlash = $hasLeadingSlash = $plainString = false;

if ($urlType === 'string') {
$isJavascript = strpos($url, 'javascript:') === 0;
$isMailto = strpos($url, 'mailto:') === 0;
$plainString = (
strpos($url, 'javascript:') === 0 ||
strpos($url, 'mailto:') === 0 ||
strpos($url, '#') === 0
);
$hasColonSlash = strpos($url, '://') !== false;
$hasLeadingSlash = isset($url[0]) ? $url[0] === '/' : false;
}
Expand All @@ -673,8 +676,7 @@ public static function url($url = null, $options = array()) {
$urlType === 'string' &&
strpos($url, ':') !== false &&
strpos($url, '/') === false &&
!$isMailto &&
!$isJavascript
!$plainString
) {
$url = self::_splitName($url, $options);
$urlType = 'array';
Expand Down Expand Up @@ -758,8 +760,7 @@ public static function url($url = null, $options = array()) {
$urlType === 'string' &&
!$hasLeadingSlash &&
!$hasColonSlash &&
!$isMailto &&
!$isJavascript
!$plainString
) {
// named route.
$route = self::$_routes->get($url);
Expand All @@ -777,10 +778,7 @@ public static function url($url = null, $options = array()) {
$output = self::$_routes->match($url);
} else {
// String urls.
if (
($hasColonSlash || $isJavascript || $isMailto) ||
(!strncmp($url, '#', 1))
) {
if ($hasColonSlash || $plainString) {
return $url;
}
if ($hasLeadingSlash) {
Expand Down
8 changes: 6 additions & 2 deletions lib/Cake/Test/TestCase/Routing/RouterTest.php
Expand Up @@ -860,18 +860,22 @@ public function testUrlGenerationWithUrlFilter() {
}

/**
* Test that strings starting with mailto: etc are handled correctly.
* Test that plain strings urls work
*
* @return void
*/
public function testUrlGenerationMailtoAndJavascript() {
public function testUrlGenerationPlainString() {
$mailto = 'mailto:mark@example.com';
$result = Router::url($mailto);
$this->assertEquals($mailto, $result);

$js = 'javascript:alert("hi")';
$result = Router::url($js);
$this->assertEquals($js, $result);

$hash = '#first';
$result = Router::url($hash);
$this->assertEquals($hash, $result);
}

/**
Expand Down

0 comments on commit d984ceb

Please sign in to comment.