修复HtmlUtil.removeHtmlAttr处理空格问题

This commit is contained in:
Looly 2024-01-25 18:01:31 +08:00
parent c5a1223803
commit 40f2bcab15
2 changed files with 13 additions and 5 deletions

View File

@ -189,14 +189,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;
}

View File

@ -15,4 +15,10 @@ public class IssueI8YV0KTest {
final String str = "<content styleCode=\"xmChange\"/>";
Assertions.assertEquals("<content/>", HtmlUtil.removeHtmlAttr(str, "styleCode"));
}
@Test
public void removeHtmlAttrTest3(){
final String str = "<content styleCode=\"dada ada\" data=\"dsad\" >";
Assertions.assertEquals("<content data=\"dsad\">", HtmlUtil.removeHtmlAttr(str, "styleCode"));
}
}