From d0a2a5469b9f3cd1bc75ecbac67ab2d67a50938b Mon Sep 17 00:00:00 2001 From: orz12 Date: Sun, 29 Dec 2024 00:35:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=A0=87=E9=A2=98=E8=BD=AC=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/em.dart | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/utils/em.dart b/lib/utils/em.dart index 733f5c357..0f62c3b0f 100644 --- a/lib/utils/em.dart +++ b/lib/utils/em.dart @@ -12,6 +12,7 @@ class Em { static regTitle(String origin) { RegExp exp = RegExp('<[^>]*>([^<]*)]*>'); List res = []; + origin.splitMapJoin(exp, onMatch: (Match match) { String matchStr = match[0]!; Map map = {'type': 'em', 'text': regCate(matchStr)}; @@ -22,17 +23,34 @@ class Em { str = str .replaceAll('<', '<') .replaceAll('>', '>') - .replaceAll('"', '"') - .replaceAll(''', "'") .replaceAll('"', '"') .replaceAll(''', "'") .replaceAll(' ', " ") .replaceAll('&', "&"); + + // 处理类似 ' " 这类的 HTML 实体字符 + final entityRegex = RegExp(r'&#x([0-9A-Fa-f]+);|&#(\d+);'); + str = str.replaceAllMapped(entityRegex, (match) { + if (match[1] != null) { + final hexValue = int.tryParse(match[1]!, radix: 16); + if (hexValue != null) { + return String.fromCharCode(hexValue); + } + } else if (match[2] != null) { + final decimalValue = int.tryParse(match[2]!, radix: 10); + if (decimalValue != null) { + return String.fromCharCode(decimalValue); + } + } + return match.group(0)!; + }); + Map map = {'type': 'text', 'text': str}; res.add(map); } return str; }); + return res; } }