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
Posted by Thomas Weidner (thomas) on 2008-03-16T04:52:13.000+0000
Fixed with SVN-8852
Posted by Wil Sinclair (wil) on 2008-03-21T17:11:57.000+0000
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.
Posted by David Toniolo (david toniolo) on 2008-03-27T16:44:31.000+0000
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; }
Posted by Thomas Weidner (thomas) on 2008-03-27T17:04:15.000+0000
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.
Posted by David Toniolo (david toniolo) on 2008-03-27T19:05:56.000+0000
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.
Posted by David Toniolo (david toniolo) on 2008-03-27T19:24:45.000+0000
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.
Posted by Thomas Weidner (thomas) on 2008-03-28T02:24:27.000+0000
Sorry, but I still do not see the problem...
See the code from the core:
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.
Posted by David Toniolo (david toniolo) on 2008-03-28T08:21:17.000+0000
If $conditionalStylesheet is a boolean it won't be converted to strings "false" or "true" but to strings "" or "1". That's the bug.
Posted by David Toniolo (david toniolo) on 2008-03-28T08:24:46.000+0000
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.