From c76db7d8d1b94cd9de430f82a22cfa35c98d62ad Mon Sep 17 00:00:00 2001 From: luoqiz Date: Mon, 1 Dec 2025 09:51:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dicon=E4=B8=A2=E5=A4=B1?= =?UTF-8?q?=E6=A0=B9=E5=B1=9E=E6=80=A7=E5=AF=BC=E8=87=B4=E7=9A=84=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E9=94=99=E8=AF=AF=20(#6986)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/icons/src/svg/load.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/icons/src/svg/load.ts b/packages/icons/src/svg/load.ts index 1557467f..59098bdb 100644 --- a/packages/icons/src/svg/load.ts +++ b/packages/icons/src/svg/load.ts @@ -13,10 +13,28 @@ function parseSvg(svgData: string): IconifyIconStructure { const xmlDoc = parser.parseFromString(svgData, 'image/svg+xml'); const svgElement = xmlDoc.documentElement; + // 提取 SVG 根元素的关键样式属性 + const getAttrs = (el: Element, attrs: string[]) => + attrs + .map((attr) => + el.hasAttribute(attr) ? `${attr}="${el.getAttribute(attr)}"` : '', + ) + .filter(Boolean) + .join(' '); + + const rootAttrs = getAttrs(svgElement, [ + 'fill', + 'stroke', + 'fill-rule', + 'stroke-width', + ]); + const svgContent = [...svgElement.childNodes] .filter((node) => node.nodeType === Node.ELEMENT_NODE) .map((node) => new XMLSerializer().serializeToString(node)) .join(''); + // 若根有属性,用一个 g 标签包裹内容并继承属性 + const body = rootAttrs ? `${svgContent}` : svgContent; const viewBoxValue = svgElement.getAttribute('viewBox') || ''; const [left, top, width, height] = viewBoxValue.split(' ').map((val) => { @@ -25,7 +43,7 @@ function parseSvg(svgData: string): IconifyIconStructure { }); return { - body: svgContent, + body, height, left, top,