ZF-2889: View_Helper_HeadLink does not check for "false"

Description

Expect this code:


foreach ($this->layout()->metaHeader['css'] as $key => $array) {
    echo $this->headLink()->appendStylesheet(
                            $array['href'],
                            $array['media'],
                            $array['conditionalStylesheet'])->toString(); 
}  

or this


<?php
if (is_array($this->layout()->metaHeader['css'])) {
    foreach ($this->layout()->metaHeader['css'] as $key => $array) {
        echo $this->headLink()->appendStylesheet($array['href'], $array['media'], false); 
    }
}
?> 

with this config:


$array['href'] = 'landing-page.css'
$array['media'] = 'screen'
$array['conditionalStylesheet'] = false; // Docu says boolean !!

does not work right...

The failure is within HeadLink.php...


if (0 < count($args)) { 
            $conditionalStylesheet = array_shift($args); 
            $conditionalStylesheet = (string) $conditionalStylesheet; 
        }  

it has to check for "false" before converting to string otherwise it will contain the string false instead of 0.

The problem can be fixed by adding this code:


 if (false !== $conditionalStylesheet) {
                $conditionalStylesheet = (string) $conditionalStylesheet;
            } 

Comments

Fixed with SVN-8852

I'm assuming this fix is merged to the 1.5 release branch for release with 1.5.1. Please update JIRA if this is not the case.

Hi,

i think this fix does not confirm to the data type of parameter $conditionalStylesheet, because it should be a string and not a boolean.

So, if the stylesheets to include come from an array like this: <?php if (is_array($this->layout()->metaHeader['css'])) { foreach ($this->layout()->metaHeader['css'] as $key => $array) { echo $this->headLink()->appendStylesheet($array['href'], $array['media'], $array['conditionalStylesheet']); } } ?>

then usually the value of $array['conditionalStylesheet'] is a string or an empty string, but not a boolean. And the code wouldn't work like before the fix.

So instead of

if (false !== $conditionalStylesheet) { $conditionalStylesheet = (string) $conditionalStylesheet; }

would be correct

if (!empty($conditionalStylesheet)) { $conditionalStylesheet = (string) $conditionalStylesheet; }

I don't know from where you have you information but conditionalStylesheet should be a boolean.

The problem is only if you process the data and your boolean is converted to an string. A boolean false is converted to an string "false" and this was not recognised. Therefore this patch has been added.

A boolean value true doesn't make sense, because the output would be like this:

Only a string does make sense here, like this

$array['conditionalStylesheet'] = ' lte IE 7';

Then the output would be like this

A boolean false or true generates invalid output.

please read the "Syntax of Conditional Comments":

http://msdn2.microsoft.com/en-us/library/…

The last two items in the example are true and false. But true and false are only two options of a few more possible values like IE, !IE, IE 5, lt IE 5.5, ...

And true|false must be given as strings to the Zend method appendStylesheet(), otherwise the output is irregular and senseless like a posted above.

Sorry, but I still do not see the problem...

See the code from the core:


        if (0 < count($args)) {
            $conditionalStylesheet = array_shift($args);
            if (false !== $conditionalStylesheet) {
                $conditionalStylesheet = (string) $conditionalStylesheet;
            }
        }

As you can see the stylesheet is used as is. ONLY if the stylesheet is given as boolean, it will be converted to a string as defined also by msdn where you refered to. An empty string would be ignored by compact.

And as you can see under the msdn link a or is also handled as expected.

If $conditionalStylesheet is a boolean it won't be converted to strings "false" or "true" but to strings "" or "1". That's the bug.

Ans also the doci is incorrect, because $conditionalStylesheet must be a string not a boolean. But a string "true" or "false" make sense, that's thinking error in my opinion.