Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Html5 safelist #1784

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/org/jsoup/safety/Safelist.java
Expand Up @@ -29,6 +29,7 @@ Thank you to Ryan Grove (wonko.com) for the Ruby HTML cleaner http://github.com/
<li>{@link #basic}
<li>{@link #basicWithImages}
<li>{@link #relaxed}
<li>{@link #relaxedHTML5()}
</ul>
<p>
If you need to allow more through (please be careful!), tweak a base safelist with:
Expand Down Expand Up @@ -180,13 +181,36 @@ public static Safelist relaxed() {
;
}

/**
This safelist allows a full range of text and structural body HTML with HTML5 standard, which is based on relaxed list:
<code>a, abbr, area, article, aside, b, blockquote, br, caption, cite, code, col, colgroup, dd, del, details,
div, dl, dt, em, footer, h1, h2, h3, h4, h5, h6, i, img, label, li, ol, p, pre, q, section, small, span, strike,
strong, sub, summary, sup, table, tbody, td, textarea, tfoot, th, thead, tr, u, ul</code>
<p>
Links do not have an enforced <code>rel=nofollow</code> attribute, but you can add that if desired.
</p>

@return safelist
*/
public static Safelist relaxedHTML5() {
return relaxed().addTags("abbr", "area", "article", "aside", "blockquote", "del", "details", "footer",
"label", "section", "summary", "textarea")
.addAttributes("abbr", "title")
.addAttributes("area", "alt")
.addAttributes("details", "open")
.addAttributes("label", "for", "form")
.addAttributes("section", "aria-labelledby", "aria-live", "aria-relevant")
.addAttributes("textarea", "id", "name", "rows", "cols");
}

/**
Create a new, empty safelist. Generally it will be better to start with a default prepared safelist instead.

@see #basic()
@see #basicWithImages()
@see #simpleText()
@see #relaxed()
@see #relaxedHTML5()
*/
public Safelist() {
tagNames = new HashSet<>();
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/jsoup/safety/CleanerTest.java
Expand Up @@ -50,6 +50,13 @@ public class CleanerTest {
assertEquals("<h1>Head</h1><table><tbody><tr><td>One</td><td>Two</td></tr></tbody></table>", TextUtil.stripNewlines(cleanHtml));
}

@Test public void testRelaxedHTML5() {
String h = "<details open><summary>Requirements</summary><p>Text</p><table><tr><td>One<td>Two</td></tr></table></details>";
String cleanHtml = Jsoup.clean(h, Safelist.relaxedHTML5());
assertEquals("<details open><summary>Requirements</summary><p>Text</p><table><tbody><tr><td>One</td><td>Two</td></tr></tbody></table></details>",
TextUtil.stripNewlines(cleanHtml));
}

@Test public void testRemoveTags() {
String h = "<div><p><A HREF='HTTP://nice.com'>Nice</a></p><blockquote>Hello</blockquote>";
String cleanHtml = Jsoup.clean(h, Safelist.basic().removeTags("a"));
Expand Down