feat(extra-features): Use word boundary to avoid matching patrial attr.

(e.g., width vs stroke-width)
This commit is contained in:
2025-12-31 14:49:46 +01:00
parent e2c4f265c2
commit 6435eb4c1a

View File

@ -216,10 +216,10 @@ class SmartyHelperFunctions
return ''; 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']); $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 (!is_file($icon_path)) {
if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_) { if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_) {
return 'Invalid SVG path: ' . htmlspecialchars($icon_path); return 'Invalid SVG path: ' . htmlspecialchars($icon_path);
@ -229,40 +229,49 @@ class SmartyHelperFunctions
$svg_content = file_get_contents($icon_path); $svg_content = file_get_contents($icon_path);
// Define SVG attributes that should be replaced/added // Define SVG properties that either should be added as a style in the style tag, or replaced in the svg code itself (attributes)
$svg_attributes = [ $svg_props = array(
'width', 'width' => 'style',
'height', 'height' => 'style',
'fill', 'opacity' => 'style',
'stroke', 'fill' => 'attribute',
'stroke-width', 'stroke' => 'attribute',
'opacity', );
'viewBox',
]; // Build style attribute from parameters
$styles = [];
foreach ($svg_attributes as $attr) { foreach ($svg_props as $prop => $type) {
if (!empty($params[$attr])) { if ($type === 'style' && !empty($params[$prop])) {
$value = $params[$attr]; $value = $params[$prop];
// Add 'px' unit for numeric values
// Check if attribute already exists in the SVG if (in_array($prop, ['width', 'height']) && is_numeric($value)) {
if (preg_match('/' . preg_quote($attr) . '="[^"]*"/', $svg_content)) { $value .= 'px';
// Replace existing attribute
$svg_content = preg_replace(
'/' . preg_quote($attr) . '="[^"]*"/',
$attr . '="' . $value . '"',
$svg_content
);
} else {
// Add new attribute to the opening <svg> tag
$svg_content = preg_replace(
'/(<svg[^>]*?)>/',
'$1 ' . $attr . '="' . $value . '">',
$svg_content,
1 // Only replace the first occurrence
);
}
} }
$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('/(<svg[^>]*?)>/', '$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; return $svg_content;
} }