kriswallsmith/assetic — Issue #187: cssrewrite "internal server error" (suggested patch)
Author: unluckypixie
Created: 2012-02-21 00:49:53+00:00
State: open

Hi,

My twig layout contains this:

{% stylesheets output='/bundles/mysite/css/mini.css' filter='?yui_css,cssrewrite'
'@MySiteBundle/Resources/public/css/reset.css'
'@MySiteBundle/Resources/public/css/jquery.fancybox-1.3.4.css'
'@MySiteBundle/Resources/public/css/nivo-slider.css'
'@MySiteBundle/Resources/public/css/style.css'
%}

{% endstylesheets %}

The generated HTML looks like this (in dev mode):

However the cssrewrite is causing issues. When I use the cssrewrite filter all the css file links that get output have this message:

[exception] 500 | Internal Server Error | ErrorException
[message] Warning: strpos(): Empty delimiter in /var/www/mysite/vendor/assetic/src/Assetic/Filter/CssRewriteFilter.php line 56
[1] ErrorException: Warning: strpos(): Empty delimiter in /var/www/mysite/vendor/assetic/src/Assetic/Filter/CssRewriteFilter.php line 56

The error is because during the while loop in the third condition below targetDir becomes empty.

// pop entries off the target until it fits in the source
if ('.' == dirname($sourcePath)) {
$path = str_repeat('../', substr_count($targetPath, '/'));
} elseif ('.' == $targetDir = dirname($targetPath)) {
$path = dirname($sourcePath).'/';
} else {
$path = '';
while (0 !== strpos($sourcePath, $targetDir)) {
if (false !== $pos = strrpos($targetDir, '/')) {
$targetDir = substr($targetDir, 0, $pos);
$path .= '../';
} else {
$targetDir = '';
$path .= '../';
break;
}
}
$path .= ltrim(substr(dirname($sourcePath).'/', strlen($targetDir)), '/');
}

To be honest I really don't understand what that these conditions and loops are trying to achieve, but what I do know is that if I comment out or remove the third condition so it looks like this:

// pop entries off the target until it fits in the source
if ('.' == dirname($sourcePath)) {
$path = str_repeat('../', substr_count($targetPath, '/'));
} else {
$path = dirname($sourcePath).'/';
}

It works perfectly. This is because the links in my css look like "../images/mypic.png" by the way, so adding dirname($sourcePath) i.e. where the css is coming from you get what you expect and I can't honestly think of under what condition you would want anything else than that, I'm not even sure why the first condition is needed. If the third condition is some special handling could it be made to fall back to the second one if this error occurs?

Please let me know if I am using cssrewrite wrong but no matter what I try I always end up back with this problem, I can't believe I'm the only one?!