Zend Framework

View_Helper_HeadLink does not check for "false"

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.5.0
  • Fix Version/s: 1.5.1
  • Component/s: None
  • Labels:
    None

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;
            }

Activity

Hide
Thomas Weidner added a comment -

Fixed with SVN-8852

Show
Thomas Weidner added a comment - Fixed with SVN-8852
Hide
Wil Sinclair added a comment -

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.

Show
Wil Sinclair added a comment - 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.
Hide
David Toniolo added a comment -

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;
}

Show
David Toniolo added a comment - 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; }
Hide
Thomas Weidner added a comment -

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.

Show
Thomas Weidner added a comment - 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.
Hide
David Toniolo added a comment -

A boolean value true doesn't make sense, because the output would be like this:
<!--[if 1]> <link ... /><![endif]-->

Only a string does make sense here, like this

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

Then the output would be like this

<!--[if lte IE 7]> <link ... /><![endif]-->

A boolean false or true generates invalid output.

Show
David Toniolo added a comment - A boolean value true doesn't make sense, because the output would be like this: <!--[if 1]> <link ... /><![endif]--> Only a string does make sense here, like this $array['conditionalStylesheet'] = ' lte IE 7'; Then the output would be like this <!--[if lte IE 7]> <link ... /><![endif]--> A boolean false or true generates invalid output.
Hide
David Toniolo added a comment -

please read the "Syntax of Conditional Comments":

http://msdn2.microsoft.com/en-us/library/ms537512.aspx

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.

Show
David Toniolo added a comment - please read the "Syntax of Conditional Comments": http://msdn2.microsoft.com/en-us/library/ms537512.aspx 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.
Hide
Thomas Weidner added a comment -

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 <IF false> or <IF true> is also handled as expected.

Show
Thomas Weidner added a comment - 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 <IF false> or <IF true> is also handled as expected.
Hide
David Toniolo added a comment -

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

Show
David Toniolo added a comment - If $conditionalStylesheet is a boolean it won't be converted to strings "false" or "true" but to strings "" or "1". That's the bug.
Hide
David Toniolo added a comment -

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.

Show
David Toniolo added a comment - 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.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: