diff --git a/core/SSViewer.php b/core/SSViewer.php index 11211424b..d7bc8f0ca 100644 --- a/core/SSViewer.php +++ b/core/SSViewer.php @@ -366,13 +366,14 @@ class SSViewer extends Object { // If we have our crazy base tag, then fix # links referencing the current page. 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); - } - + } + return $output; } diff --git a/tests/SSViewerTest.php b/tests/SSViewerTest.php index 905897f61..b5d12f115 100644 --- a/tests/SSViewerTest.php +++ b/tests/SSViewerTest.php @@ -43,4 +43,73 @@ SS $this->assertEquals("This is my templateThis is some contentThis is the final content", preg_replace("/\n?\n?/U",'',$output)); } + + 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( + '