Skip to content

Commit

Permalink
Using document traversal to close HTML tags
Browse files Browse the repository at this point in the history
  • Loading branch information
asegal-hs committed Nov 15, 2023
1 parent daf2c84 commit b4c604f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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.Objects;
import org.jsoup.Jsoup;
import org.jsoup.safety.Safelist;

@JinjavaDoc(
value = "Closes open HTML tags in a string",
Expand All @@ -21,6 +22,6 @@ public String getName() {

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
return Jsoup.parse(Objects.toString(var)).toString();
return Jsoup.parseBodyFragment(Objects.toString(var)).body().html();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,17 @@ public void itClosesTags() {
String openTags = "<p>Hello, world!";
assertThat(f.filter(openTags, interpreter)).isEqualTo("<p>Hello, world!</p>");
}

@Test
public void itIgnoresClosedTags() {
String openTags = "<p>Hello, world!</p>";
assertThat(f.filter(openTags, interpreter)).isEqualTo("<p>Hello, world!</p>");
}

@Test
public void itClosesMultipleTags() {
String openTags = "<h1><p>Hello, world!";
assertThat(f.filter(openTags, interpreter))
.isEqualTo("<h1><p>Hello, world!</p></h1>");
}
}

0 comments on commit b4c604f

Please sign in to comment.