miracle2k / webassets

Asset management for Python web development.
BSD 2-Clause "Simplified" License
921 stars 258 forks source link

cssrewrite breaks SVG Data URI #508

Open robshep opened 5 years ago

robshep commented 5 years ago

In the default,and with a replace map config, the cssrewrite filter breaks a data uri css url with SVG content.

This particular breakage affecting me is in the css of Bootstrap 4(.1.3)

https://github.com/twbs/bootstrap/blob/v4.1.3/dist/css/bootstrap.css#L4308

original:

   Original: "data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 30 30' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255, 255, 255, 0.5)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 7h22M4 15h22M4 23h22'/%3E%3C/svg%3E"
Replacement: "data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 30 30' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255, 255, 255, 0.5")' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 7h22M4 15h22M4 23h22'/%3E%3C/svg%3E"
marker.....                                                                                                                                          ^ spurious character

Note: I've tried to workaround this, but the replace lambda version only gives me the url to fiddle with, and not the original path, so I'm stuck.

barbossa commented 5 years ago

I confirm the issue. Someone has a solution?

02JanDal commented 5 years ago

Also ran into this, solved it by subclassing CSSRewrite and overriding rewrite_url to just returning the matched data if it seems to be a data-url (which doesn't make sense to rewrite anyway):

def rewrite_url(self, m):
    url = m.groups()[1]
    if url.startswith('data:') or url.startswith('"data:') or url.startswith('\'data:'):
        return m.group(0)
    else:
        return super(FixedCSSRewrite, self).rewrite_url(m)

I can put this into a PR if this is an acceptable solution.