修复XmlUtil转义调用方法错误问题,修复XmlEscape未转义单引号问题(pr#3837@Github)

This commit is contained in:
Looly 2025-01-09 19:31:23 +08:00
parent 50df86d846
commit 5befdeea3f
3 changed files with 22 additions and 5 deletions

View File

@ -2,12 +2,13 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.36(2025-01-02)
# 5.8.36(2025-01-09)
### 🐣新特性
* 【crypto 】 增加BCUtil.decodeECPrivateKey方法issue#3829@Github
### 🐞Bug修复
* 【aop 】 修复ProxyUtil可能的空指针问题issue#IBF20Z@Gitee
* 【core 】 修复XmlUtil转义调用方法错误问题修复XmlEscape未转义单引号问题pr#3837@Github
-------------------------------------------------------------------------------------------------------------
# 5.8.35(2024-12-25)

View File

@ -1,6 +1,7 @@
package cn.hutool.core.text.escape;
import cn.hutool.core.text.replacer.LookupReplacer;
import cn.hutool.core.text.replacer.ReplacerChain;
/**
* HTML4的ESCAPE
@ -9,9 +10,21 @@ import cn.hutool.core.text.replacer.LookupReplacer;
* @author looly
*
*/
public class Html4Escape extends XmlEscape {
public class Html4Escape extends ReplacerChain {
private static final long serialVersionUID = 1L;
/**
* HTML转义字符<br>
* HTML转义相比XML并不转义单引号<br>
* https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents
*/
protected static final String[][] BASIC_ESCAPE = { //
{"\"", "&quot;"}, // " - double-quote
{"&", "&amp;"}, // & - ampersand
{"<", "&lt;"}, // < - less-than
{">", "&gt;"}, // > - greater-than
};
protected static final String[][] ISO8859_1_ESCAPE = { //
{ "\u00A0", "&nbsp;" }, // non-breaking space
{ "\u00A1", "&iexcl;" }, // inverted exclamation mark
@ -310,6 +323,7 @@ public class Html4Escape extends XmlEscape {
public Html4Escape() {
super();
addChain(new LookupReplacer(BASIC_ESCAPE));
addChain(new LookupReplacer(ISO8859_1_ESCAPE));
addChain(new LookupReplacer(HTML40_EXTENDED_ESCAPE));
}

View File

@ -7,7 +7,7 @@ import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import lombok.Data;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
@ -22,6 +22,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
/**
* {@link XmlUtil} 工具类
*
@ -319,8 +321,8 @@ public class XmlUtilTest {
public void escapeTest(){
final String a = "<>";
final String escape = XmlUtil.escape(a);
Console.log(escape);
Console.log(XmlUtil.escape("中文“双引号”"));
Assertions.assertEquals("&lt;&gt;", escape);
Assertions.assertEquals("中文“双引号”", XmlUtil.escape("中文“双引号”"));
}
@Test