diff --git a/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php b/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php index 39220c2..8662b60 100644 --- a/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php +++ b/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php @@ -216,10 +216,10 @@ class SmartyHelperFunctions return ''; } - // The path to the icons directory + // The path to the icons directory. By default under falcon/_dev/img $icon_path = _PS_THEME_DIR_ . '/_dev/img/' . basename($params['file']); - // If there aint an image, display a basic error message on dev mode + // If the image does not exist, display a basic error message on dev mode if (!is_file($icon_path)) { if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_) { return 'Invalid SVG path: ' . htmlspecialchars($icon_path); @@ -229,40 +229,49 @@ class SmartyHelperFunctions $svg_content = file_get_contents($icon_path); - // Define SVG attributes that should be replaced/added - $svg_attributes = [ - 'width', - 'height', - 'fill', - 'stroke', - 'stroke-width', - 'opacity', - 'viewBox', - ]; - - foreach ($svg_attributes as $attr) { - if (!empty($params[$attr])) { - $value = $params[$attr]; - - // Check if attribute already exists in the SVG - if (preg_match('/' . preg_quote($attr) . '="[^"]*"/', $svg_content)) { - // Replace existing attribute - $svg_content = preg_replace( - '/' . preg_quote($attr) . '="[^"]*"/', - $attr . '="' . $value . '"', - $svg_content - ); - } else { - // Add new attribute to the opening tag - $svg_content = preg_replace( - '/(]*?)>/', - '$1 ' . $attr . '="' . $value . '">', - $svg_content, - 1 // Only replace the first occurrence - ); - } + // Define SVG properties that either should be added as a style in the style tag, or replaced in the svg code itself (attributes) + $svg_props = array( + 'width' => 'style', + 'height' => 'style', + 'opacity' => 'style', + 'fill' => 'attribute', + 'stroke' => 'attribute', + ); + + // Build style attribute from parameters + $styles = []; + foreach ($svg_props as $prop => $type) { + if ($type === 'style' && !empty($params[$prop])) { + $value = $params[$prop]; + // Add 'px' unit for numeric values + if (in_array($prop, ['width', 'height']) && is_numeric($value)) { + $value .= 'px'; } + $styles[] = $prop . ': ' . $value; } + } + + // If we have styles to apply, add them to the SVG tag + if (!empty($styles)) { + $style_attr = 'style="' . implode('; ', $styles) . '"'; + $svg_content = preg_replace('/(]*?)>/', '$1 ' . $style_attr . '>', $svg_content, 1); + } + + // Replace attribute values throughout the entire SVG (all occurrences) + foreach ($svg_props as $prop => $type) { + if ($type === 'attribute' && !empty($params[$prop])) { + $value = $params[$prop]; + + // Use word boundaries to avoid matching partial names like fill-opacity or stroke-width + // Replace ALL occurrences of the attribute + $svg_content = preg_replace( + '/\b' . preg_quote($prop) . '\b\s*=\s*"[^"]*"/', + $prop . '="' . $value . '"', + $svg_content + ); + } + } + return $svg_content; }