mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-12-03 16:59:42 +00:00
* fix(shared): escape attribute values in buildLucideSvg Attribute values were interpolated into the SVG string as-is, so a value containing a double quote, ampersand or angle bracket (for example in a custom attribute or class name) produced malformed markup. * Use replace(/x/g) instead of replaceAll for broader TS lib compat lucide-react-native's tsconfig doesn't include the ES2021 lib that replaceAll requires, failing typecheck. A global regex replace is functionally identical here and works under any target. * Escape the full set of relevant HTML entities in attribute values Also escape > and ' per review feedback, not just & " <. * Apply suggestion from @karsa-mistmere --------- Co-authored-by: Karsa <contact@karsa.org>
32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
import buildLucideIconNode from './buildLucideIconNode';
|
|
import type { LucideBuildParams, LucideIconData, LucideIconNode } from './types';
|
|
|
|
const entities = {
|
|
'&': '&',
|
|
'<': '<',
|
|
'>': '>',
|
|
'"': '"',
|
|
"'": ''',
|
|
} as const;
|
|
|
|
const escapeAttribute = (value: string) =>
|
|
value.replace(/[&<>"']/g, (char) => entities[char as keyof typeof entities]);
|
|
|
|
const buildDomNode = ([tagName, attributes, children = []]: LucideIconNode): string =>
|
|
`<${tagName} ${Object.entries(attributes)
|
|
.filter(([, value]) => value !== undefined && value !== null)
|
|
.map(([attrName, value]) => `${attrName}="${escapeAttribute(String(value))}"`)
|
|
.join(' ')}>${children?.map((child) => buildDomNode(child)).join('')}</${tagName}>`;
|
|
|
|
/**
|
|
* Creates an SVG string from a Lucide icon object.
|
|
*
|
|
* @param icon The icon to build.
|
|
* @param params Additional build parameters.
|
|
*/
|
|
function buildLucideSvg(icon: LucideIconData, params: LucideBuildParams = {}) {
|
|
return buildDomNode(buildLucideIconNode(icon, params));
|
|
}
|
|
|
|
export default buildLucideSvg;
|