feat(extra-features): Use word boundary to avoid matching patrial attr.
(e.g., width vs stroke-width)
This commit is contained in:
@ -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',
|
||||
];
|
||||
// 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',
|
||||
);
|
||||
|
||||
foreach ($svg_attributes as $attr) {
|
||||
if (!empty($params[$attr])) {
|
||||
$value = $params[$attr];
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if attribute already exists in the SVG
|
||||
if (preg_match('/' . preg_quote($attr) . '="[^"]*"/', $svg_content)) {
|
||||
// Replace existing attribute
|
||||
// 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(
|
||||
'/' . preg_quote($attr) . '="[^"]*"/',
|
||||
$attr . '="' . $value . '"',
|
||||
'/\b' . preg_quote($prop) . '\b\s*=\s*"[^"]*"/',
|
||||
$prop . '="' . $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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $svg_content;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user