-
Notifications
You must be signed in to change notification settings - Fork 170
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
Introduces a limited length render filter and HTML tag close filter #1128
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8fcd5ef
Stubs out filters
asegal-hs bfb78f5
Turned on prettier and changed filter name
asegal-hs 87e6a0e
Stubbed out if case statement
asegal-hs baae46e
Using max output param to clamp outputs
asegal-hs 0a82efb
Calls new render limit method from filter
asegal-hs 0e4692a
Basic functionality working with two tests
asegal-hs be44efa
Added two more tests
asegal-hs 630768f
Removed close html filter because limited rendering works on whole HT…
asegal-hs 16fb43f
Added back closehtml filter and test case that demonstrates it
asegal-hs daf2c84
Added close html filter implementation with Jsoup
asegal-hs b4c604f
Using document traversal to close HTML tags
asegal-hs 09f051b
Removed references to safelist for Jsoup
asegal-hs 38b50a5
Removed unnecessary method and formalized default case for non-number…
asegal-hs 4e2e4a1
Clamp output size to config max if not 0
asegal-hs 254d5fc
Added safer limit clamping function
asegal-hs 10b4ff6
Extracted clamp to helper class with tests and fixed negative config bug
asegal-hs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/hubspot/jinjava/lib/filter/CloseHtmlFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.hubspot.jinjava.lib.filter; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaParam; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import java.util.Objects; | ||
import org.jsoup.Jsoup; | ||
|
||
@JinjavaDoc( | ||
value = "Closes open HTML tags in a string", | ||
input = @JinjavaParam(value = "s", desc = "String to close", required = true), | ||
snippets = { @JinjavaSnippet(code = "{{ \"<p> Hello, world\"|closehtml }}") } | ||
) | ||
public class CloseHtmlFilter implements Filter { | ||
|
||
@Override | ||
public String getName() { | ||
return "closehtml"; | ||
} | ||
|
||
@Override | ||
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { | ||
return Jsoup.parseBodyFragment(Objects.toString(var)).body().html(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/java/com/hubspot/jinjava/lib/filter/CloseHtmlFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.hubspot.jinjava.lib.filter; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.hubspot.jinjava.BaseInterpretingTest; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class CloseHtmlFilterTest extends BaseInterpretingTest { | ||
CloseHtmlFilter f; | ||
|
||
@Before | ||
public void setup() { | ||
f = new CloseHtmlFilter(); | ||
} | ||
|
||
@Test | ||
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>"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, can we just get some tests for this logic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks a lot Jack. To avoid repeating tests or changing signatures I moved this into a small static utils class which I tested on it's own. Let me know if you'd rather I just make it public in the interpreter or add more tests to the render method as a whole and take it back to being private.
I did like this a bit for simplicity though.