diff --git a/core/SSViewer.php b/core/SSViewer.php index 13d2ef1ce..80cf1e430 100755 --- a/core/SSViewer.php +++ b/core/SSViewer.php @@ -439,9 +439,10 @@ class SSViewer { if($this->rewriteHashlinks && self::$options['rewriteHashlinks']) { if(strpos($output, ''), array('&','"',''','<','>'), \$_SERVER['REQUEST_URI']); ?>"; } else { - $thisURLRelativeToBase = Director::makeRelative(Director::absoluteURL($_SERVER['REQUEST_URI'])); + $thisURLRelativeToBase = Convert::raw2att($_SERVER['REQUEST_URI']); } $output = preg_replace('/(]+href *= *)"#/i', '\\1"' . $thisURLRelativeToBase . '#', $output); } diff --git a/tests/SSViewerTest.php b/tests/SSViewerTest.php index 50593076b..ab3120363 100644 --- a/tests/SSViewerTest.php +++ b/tests/SSViewerTest.php @@ -136,6 +136,76 @@ SS $negotiator->xhtml($response); $this->assertRegExp('/<\/head>/', $response->getBody()); } + + function testRewriteHashlinks() { + $oldRewriteHashLinks = SSViewer::getOption('rewriteHashlinks'); + SSViewer::setOption('rewriteHashlinks', true); + + // Emulate SSViewer::process() + $base = Convert::raw2att($_SERVER['REQUEST_URI']); + + $tmplFile = TEMP_FOLDER . '/SSViewerTest_testRewriteHashlinks_' . sha1(rand()) . '.ss'; + + // Note: SSViewer_FromString doesn't rewrite hash links. + file_put_contents($tmplFile, ' + + <% base_tag %> + + InlineLink + $InsertedLink + + '); + $tmpl = new SSViewer($tmplFile); + $obj = new ViewableData(); + $obj->InsertedLink = 'InsertedLink'; + $result = $tmpl->process($obj); + $this->assertContains( + 'InsertedLink', + $result + ); + $this->assertContains( + 'InlineLink', + $result + ); + + unlink($tmplFile); + + SSViewer::setOption('rewriteHashlinks', $oldRewriteHashLinks); + } + + function testRewriteHashlinksInPhpMode() { + $oldRewriteHashLinks = SSViewer::getOption('rewriteHashlinks'); + SSViewer::setOption('rewriteHashlinks', 'php'); + + $tmplFile = TEMP_FOLDER . '/SSViewerTest_testRewriteHashlinksInPhpMode_' . sha1(rand()) . '.ss'; + + // Note: SSViewer_FromString doesn't rewrite hash links. + file_put_contents($tmplFile, ' + + <% base_tag %> + + InlineLink + $InsertedLink + + '); + $tmpl = new SSViewer($tmplFile); + $obj = new ViewableData(); + $obj->InsertedLink = 'InsertedLink'; + $result = $tmpl->process($obj); + $this->assertContains( + '