Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addCssClass/mergeCssClasses mixed result for "similar" value #17952

Open
bobonov opened this issue Mar 31, 2020 · 1 comment
Open

addCssClass/mergeCssClasses mixed result for "similar" value #17952

bobonov opened this issue Mar 31, 2020 · 1 comment

Comments

@bobonov
Copy link

bobonov commented Mar 31, 2020

Using "similar" (null, true, false...) value give different result.
In particular:

  • if null is passed in array or as value give different results.

  • int 0 give empty result, int >0 give the number

The problem is trivial since in my case affect only TestClass for widget since when applying a class with null value result in an additional space and the resulting string do not match

What steps will reproduce the problem?

The below code compare the results

$array=['null'=>null, 'false'=>false, 'true'=>true, "empty ''"=>'', 'space'=>' ',
    'str 0'=>'0', 'int 0'=>0, 'int 1'=>1];

foreach($array as $item=>$value){
    $options=['class'=>'base_class'];
    Html::addCssClass($options, [$value]);
    echo Html::tag('div', "array[$item]", $options)."\n";
    Html::addCssClass($options, $value);
    echo Html::tag('div', "$item value", $options)."\n\n";
}

The above code give as result:

<div class="base_class ">array[null]</div>
<div class="base_class">null value</div>

<div class="base_class ">array[false]</div>
<div class="base_class ">false value</div>

<div class="base_class">array[true]</div>
<div class="base_class">true value</div>

<div class="base_class ">array[empty '']</div>
<div class="base_class ">empty '' value</div>

<div class="base_class  ">array[space]</div>
<div class="base_class  ">space value</div>

<div class="base_class 0">array[str 0]</div>
<div class="base_class 0">str 0 value</div>

<div class="base_class">array[int 0]</div>
<div class="base_class">int 0 value</div>

<div class="base_class 1">array[int 1]</div>
<div class="base_class 1">int 1 value</div>

Especially for null it gives a different result but in general the result is quite mixed for values that should give same result when casted to string.
(int) 0 should be same as (str) 0
true/false should produce in both cases ''
null passed as value or value in array should give same result

###Expected result
The expected result should be:
<div class="base_class">
except for (int) 0 which should be
<div class="base_class 0">
In the above case, starting a css class name with 0 or any number is invalid, but I do not know if there is any css framewrok doing so

Additional info

I think the best place to fix this behavior is in BaseHtml::mergeCssClasses
Here the code should drop empty values and eventually non valid css class name.
Personally I would avoid the second part (csss name validation) since this would make big problem with css framework like Tailwind.
Probably the best solution is to change the code of BaseHtml::mergeCssClasses to filter out empty and "only space" string value.
Applying the following array_filter before return fix it.

$existingClasses=array_filter($existingClasses, function($value) { return !is_null($value) && trim($value) !== ''; });
Q A
Yii version 2.0.32
PHP version 7.2.18
Operating system Ubuntu Server 18.04
@samdark samdark added the type:bug Bug label Mar 31, 2020
@samdark
Copy link
Member

samdark commented Mar 31, 2020

Interesting find. Thanks for reporting.

Can't we solve it at HTML class rendering method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants