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

Fix striptags to clean HTML instead of parsing #733

Merged
merged 2 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

/**
* striptags(value) Strip SGML/XML tags and replace adjacent whitespace by one space.
Expand Down Expand Up @@ -34,8 +35,9 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar
}

String val = interpreter.renderFlat((String) object);
String strippedVal = Jsoup.parseBodyFragment(val).text();
String normalizedVal = WHITESPACE.matcher(strippedVal).replaceAll(" ");
String cleanedVal = Jsoup.clean(val, Whitelist.none());

String normalizedVal = WHITESPACE.matcher(cleanedVal).replaceAll(" ");

return normalizedVal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void itPassesThruNonStringVals() throws Exception {
@Test
public void itWorksWithNonHtmlStrings() throws Exception {
assertThat(filter.filter("foo", interpreter)).isEqualTo("foo");
assertThat(filter.filter("foo < bar", interpreter)).isEqualTo("foo < bar");
assertThat(filter.filter("foo < bar", interpreter)).isEqualTo("foo &lt; bar");
}

@Test
Expand All @@ -51,4 +51,16 @@ public void itStripsTagsFromHtml() throws Exception {
assertThat(filter.filter("foo <b>bar</b> other", interpreter))
.isEqualTo("foo bar other");
}

@Test
public void itStripsTagsFromNestedHtml() throws Exception {
assertThat(filter.filter("<div><strong>test</strong></div>", interpreter))
.isEqualTo("test");
}

@Test
public void itStripsTagsFromEscapedHtml() throws Exception {
assertThat(filter.filter("&lt;div&gt;test&lt;/test&gt;", interpreter))
.isEqualTo("&lt;div&gt;test&lt;/test&gt;");
}
}