Welcome Guest, Not a member yet? Register   Sign In
Double space in source link_tag function
#1

[eluser]InterCoder[/eluser]
Hi everybody,

I'm using the link_tag function, but I discovered a ??typo?? in the function itself. I'm using 1.6.3. of CodeIgniter and the function outputs the following HTML:
Code:
<link  href="/path/to/style.css" rel="stylesheet" type="text/css" />

As you can see a double space after <link. Currently I've made a MY_html_helper file to undo that, but I think it should be fixed.

The code says:
Code:
function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
{
    $CI =& get_instance();

    $link = '<link '; // space no. #1

    if (is_array($href))

    /* Some code */

        if ($index_page === TRUE)
        {
            // space no. #2
            $link .= ' href="'.$CI->config->site_url($v).'" ';
        }
        else
        {
            // or space no. #2
            $link .= ' href="'.$CI->config->slash_item('base_url').$v.'" ';
        }

Simple fix:
Replace
Code:
$link = '<link ';
with
Code:
$link = '<link';

Kind regards,
InterCoder
#2

[eluser]xwero[/eluser]
It doesn't affect the browsers ability to read the tag so it's not a problem in any way.

The reason why the extra space is added to the initial link string is to make the function foolproof. Further in the function you see
Code:
$link .= "$k=\"$v\" ";
so without initial whitespace
Code:
$link = array(
          'rel' => 'stylesheet',          
          'href' => 'css/printer.css',
          'type' => 'text/css',
          'media' => 'print'
);

echo link_tag($link);
The output would be
Code:
<linkrel="stylesheet" href="http://site.com/css/printer.css" type="text/css" media="print" />
This could be resolved by using an array to catch all the attribute strings and implode it when the link needs to be returned.
Code:
function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
    {
        $CI =& get_instance();

                $attributes = array();

        if (is_array($href))
        {
            foreach ($href as $k=>$v)
            {
                if ($k == 'href' AND strpos($v, '://') === FALSE)
                {
                    if ($index_page === TRUE)
                    {
                        $attributes[] = 'href="'.$CI->config->site_url($v).'"';
                    }
                    else
                    {
                        $attributes[] = 'href="'.$CI->config->slash_item('base_url').$v.'"';
                    }
                }
                else
                {
                    $attributes[] = "$k=\"$v\"";
                }
            }
        }
        else
        {
            if ( strpos($href, '://') !== FALSE)
            {
                $attributes[] = 'href="'.$href.'"';
            }
            elseif ($index_page === TRUE)
            {
                $attributes[] = 'href="'.$CI->config->site_url($href).'"';
            }
            else
            {
                $attributes[] = 'href="'.$CI->config->slash_item('base_url').$href.'"';
            }

            $attributes[] = 'rel="'.$rel.'"';
                        $attributes[] = 'type="'.$type.'"';

            if ($media    != '')
            {
                $attributes[] = 'media="'.$media.'" ';
            }

            if ($title    != '')
            {
                $attributes[] = 'title="'.$title.'"';
            }
        }


        return '<link '.implode(' ',$attributes).'/>'."\n";
    }
No more need to mess around with whitespaces

edit : the easy solution would be
Code:
$link .= " $k=\"$v\"";
But that was too far fetched Smile




Theme © iAndrew 2016 - Forum software by © MyBB