You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SVG在Chrome正常显示但Firefox报未闭合CDATA段解析错误求助

Fixing "unclosed CDATA section" Error in Firefox for SVG Data URI

I’ve run into this exact Firefox quirk with SVG CDATA parsing before—let’s break down the solutions that worked for me:

1. Ditch the CDATA Section Entirely

Your CSS doesn’t contain any characters that require CDATA escaping (like <, >, or &), so you can safely remove the <![CDATA[ wrapper and ]]> closing tag from the <style> block. Firefox sometimes gets finicky with CDATA inside data URIs even when it’s properly formatted, and this bypasses the issue entirely.

Here’s your modified SVG data URI with CDATA removed:

data:image/svg+xml;utf8,&lt;svg clip-rule="evenodd" height="48px" width="48px" fill-rule="evenodd" image-rendering="optimizeQuality" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" viewBox="0 0 5000 5000" xmlns="http://www.w3.org/2000/svg"&gt;&lt;defs&gt;&lt;style type="text/css"&gt;.str0 {stroke:#434242;stroke-width:100} .fil2 {fill:#434242} .fil0 {fill:url(#id0)} .fil1 {fill:url(#id1)}&lt;/style&gt;&lt;linearGradient gradientUnits="userSpaceOnUse" id="id0" x1="2500" x2="2500" y1="900.02" y2="1950.02"&gt;&lt;stop offset="0" stop-color="#FCFCFD"/&gt;&lt;stop offset="1" stop-color="#fff"/&gt;&lt;/linearGradient&gt;&lt;linearGradient gradientUnits="userSpaceOnUse" id="id1" x1="2499.86" x2="2499.86" y1="3835.02" y2="1715"&gt;&lt;stop offset="0" stop-color="#84ff45"/&gt;&lt;stop offset="1" stop-color="#84e45c"/&gt;&lt;/linearGradient&gt;&lt;/defs&gt;&lt;g id="Layer_x0020_1"&gt;&lt;path class="fil0 str0" d="M500 1950v-950c0-55 45-100 100-100h1400l400 300h2000c55 0 100 45 100 100v650h-4000z"/&gt;&lt;path class="fil1 str0" d="M600 4100h3800c55 0 98-45 100-100l100-2450c2-55-45-100-100-100h-1500l-400 300h-2100c-55 0-102 45-100 100l100 2150c3 55 45 100 100 100z"/&gt;&lt;path class="fil2" d="M800 1100h1100c55 0 100 45 100 100s-45 100-100 100h-1100c-55 0-100-45-100-100s45-100 100-100z"/&gt;&lt;/g&gt;&lt;/svg&gt;

2. Test with Base64 Encoding (If You Need CDATA)

If you ever need CDATA for more complex CSS, try encoding your SVG as base64 instead of using UTF-8 escaped characters. This avoids any XML parsing conflicts entirely because Firefox treats base64 data as a raw blob. You can convert your SVG code to base64 using any local tool or script, then format the data URI like:
data:image/svg+xml;base64,[your-base64-string]

3. Verify Escaping Consistency

Double-check that all < and > characters in your data URI are properly escaped to &lt; and &gt;. While your code looks correct, sometimes a missing escape (especially around CDATA) can trip up Firefox’s stricter XML parser.


内容的提问来源于stack exchange,提问作者onok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:01:17