From e4dffeb4c85a96e92fddd401808fb5fbb687d077 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 25 Jan 2024 18:01:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHtmlUtil.removeHtmlAttr?= =?UTF-8?q?=E5=A4=84=E7=90=86=E7=A9=BA=E6=A0=BC=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- .../src/main/java/cn/hutool/http/HtmlUtil.java | 14 ++++++++------ .../test/java/cn/hutool/http/IssueI8YV0KTest.java | 6 ++++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec52f5568..5f4381165 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.26(2024-01-24) +# 5.8.26(2024-01-25) ### 🐣新特性 * 【db 】 RedisDS增加user支持(issue#I8XEQ4@Gitee) @@ -12,6 +12,7 @@ * 【http 】 修复UserAgentUtil对QQ浏览器识别问题(issue#I8X5XQ@Gitee) * 【core 】 修复BeanToMapCopier获取类型数组越界问题(issue#3468@Github) * 【extra 】 修复SshjSftpSession关闭导致的问题(issue#3472@Github) +* 【http 】 修复HtmlUtil.removeHtmlAttr处理空格问题(issue#I8YV0K@Gitee) ------------------------------------------------------------------------------------------------------------- # 5.8.25(2024-01-11) diff --git a/hutool-http/src/main/java/cn/hutool/http/HtmlUtil.java b/hutool-http/src/main/java/cn/hutool/http/HtmlUtil.java index b6050d035..94d429a18 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HtmlUtil.java +++ b/hutool-http/src/main/java/cn/hutool/http/HtmlUtil.java @@ -149,7 +149,7 @@ public class HtmlUtil { */ public static String removeHtmlAttr(String content, String... attrs) { String regex; - for (String attr : attrs) { + for (final String attr : attrs) { // (?i) 表示忽略大小写 // \s* 属性名前后的空白符去除 // [^>]+? 属性值,至少有一个非>的字符,>表示标签结束 @@ -158,14 +158,16 @@ public class HtmlUtil { regex = StrUtil.format("(?i)(\\s*{}\\s*=\\s*)" + "(" + // name="xxxx" - "([\"][^\"]+?[\"]\\s*)|" + - // name=xxx > - "([^>]+?\\s+(?=>))|" + - // name=xxx> 或者 name=xxx name2=xxx - "([^>]+?(?=\\s|>))" + + "([\"][^\"]+?[\"])|" + + // name=xxx > 或者 name=xxx> 或者 name=xxx name2=xxx + "([^>]+?\\s*(?=\\s|>))" + ")", attr); content = content.replaceAll(regex, StrUtil.EMPTY); } + + // issue#I8YV0K 去除尾部空格 + content = ReUtil.replaceAll(content, "\\s+(>|/>)", "$1"); + return content; } diff --git a/hutool-http/src/test/java/cn/hutool/http/IssueI8YV0KTest.java b/hutool-http/src/test/java/cn/hutool/http/IssueI8YV0KTest.java index f6dbf1173..8a1a62900 100644 --- a/hutool-http/src/test/java/cn/hutool/http/IssueI8YV0KTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/IssueI8YV0KTest.java @@ -16,4 +16,10 @@ public class IssueI8YV0KTest { final String str = ""; Assert.assertEquals("", HtmlUtil.removeHtmlAttr(str, "styleCode")); } + + @Test + public void removeHtmlAttrTest3(){ + String str = ""; + Assert.assertEquals("", HtmlUtil.removeHtmlAttr(str, "styleCode")); + } }