HTMLFilter保留p标签

This commit is contained in:
Looly 2023-12-20 20:53:38 +08:00
parent 004d4f407d
commit 1460902eab
2 changed files with 34 additions and 1 deletions

View File

@ -149,6 +149,8 @@ public final class HTMLFilter {
vAllowed.put("strong", no_atts);
vAllowed.put("i", no_atts);
vAllowed.put("em", no_atts);
// issue#3433
vAllowed.put("p", no_atts);
vSelfClosingTags = new String[]{"img"};
vNeedClosingTags = new String[]{"a", "b", "strong", "i", "em"};
@ -429,7 +431,7 @@ public final class HTMLFilter {
ending = "";
}
if (ending == null || ending.length() < 1) {
if (ending == null || ending.isEmpty()) {
if (vTagCounts.containsKey(name)) {
vTagCounts.put(name, vTagCounts.get(name) + 1);
} else {

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2023. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.http.html;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class HTMLFilterTest {
@Test
void issue3433Test() {
final String p1 = "<p>a</p>";
final String p2 = "<p onclick=\"bbbb\">a</p>";
final HTMLFilter htmlFilter = new HTMLFilter();
String filter = htmlFilter.filter(p1);
Assertions.assertEquals("<p>a</p>", filter);
filter = htmlFilter.filter(p2);
Assertions.assertEquals("<p>a</p>", filter);
}
}