rewriting hash links forgets to change & into & #2877

Open
sunnysideup opened this Issue Feb 19, 2014 · 0 comments

Projects

None yet

2 participants

@sunnysideup
Contributor

I have noticed when when I write

<a href="#foo"> 

in a template file the template engine turns it into

<a href="CurrentURL#foo">

The problem arises when the URL is, for example, http://www.mysite.co.nz/?sort=5&filter=bar. According to our friends at W3, the ampersand should be escaped in the link (i.e. link should be written as http://www.mysite.co.nz/?sort=5&amp;filter=bar

I had a look at the code and it seems the whole code may need a bit of a cleanup there...

Here is the relevant code, see SSTemplateParser.php line 4654


    /**
     * We convert text 
     */
    function Text__finalise(&$res) {
        $text = $res['text'];

        // Unescape any escaped characters in the text, then put back escapes for any single quotes and backslashes
        $text = stripslashes($text);
        $text = addcslashes($text, '\'\\');

        // TODO: This is pretty ugly & gets applied on all files not just html. I wonder if we can make this
        // non-dynamically calculated
        $text = preg_replace(
            '/href\s*\=\s*\"\#/', 
            'href="\' . (Config::inst()->get(\'SSViewer\', \'rewrite_hash_links\') ?' .
            ' strip_tags( $_SERVER[\'REQUEST_URI\'] ) : "") . 
                \'#',
            $text
        );

        $res['php'] .= '$val .= \'' . $text . '\';' . PHP_EOL;
    }

AND see SSViewer.php line 1054


        $rewrite = Config::inst()->get('SSViewer', 'rewrite_hash_links');
        if($this->rewriteHashlinks && $rewrite) {
            if(strpos($output, '<base') !== false) {
                if($rewrite === 'php') { 
                    $thisURLRelativeToBase = "<?php echo strip_tags(\$_SERVER['REQUEST_URI']); ?>"; 
                } else { 
                    $thisURLRelativeToBase = strip_tags($_SERVER['REQUEST_URI']); 
                }

                $output = preg_replace('/(<a[^>]+href *= *)"#/i', '\\1"' . $thisURLRelativeToBase . '#', $output);
            }
        }
@simonwelsh simonwelsh added the 3.1 label Mar 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment