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

twig svg_image() classes wrongly applied #3068

Closed
TheRealThor opened this issue Nov 17, 2020 · 0 comments
Closed

twig svg_image() classes wrongly applied #3068

TheRealThor opened this issue Nov 17, 2020 · 0 comments
Assignees

Comments

@TheRealThor
Copy link

TheRealThor commented Nov 17, 2020

Grav 1.7:
Example Twig:
{{svg_image("/user/pages/img/image.svg", "h-24 w-auto")}}

Expected behaviour: css classes are attached to the SVG tag

Actually happening:
classes being applied to all subtags (paths etc) but not the SVG tag

Problem:
In the current twig function svgImageFunction() the regex matches the whole svg file instead of the SVG tag alone for the replacement

Solution:
Replace in Line 1440 of TwigExtension.php with this code:

$svg = preg_replace_callback('/^<svg[^>]*(class=\")([^"]*)(\")[^>]*>/', function($matches) use ($classes, &$matched) {
                if (isset($matches[2])) {
                    $new_classes = $matches[2] . $classes;
                    $matched = true;
                    return str_replace($matches[1], "class=\"$new_classes\"", $matches[0]);
                }
                return $matches[0];
            }, $svg
            );

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

No branches or pull requests

2 participants