-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1128 from HubSpot/capped-rendering
Introduces a limited length render filter and HTML tag close filter
- Loading branch information
Showing
7 changed files
with
207 additions
and
7 deletions.
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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/hubspot/jinjava/util/RenderLimitUtils.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,23 @@ | ||
package com.hubspot.jinjava.util; | ||
|
||
import com.hubspot.jinjava.JinjavaConfig; | ||
|
||
public class RenderLimitUtils { | ||
|
||
public static long clampProvidedRenderLimitToConfig( | ||
long providedLimit, | ||
JinjavaConfig jinjavaConfig | ||
) { | ||
long configMaxOutput = jinjavaConfig.getMaxOutputSize(); | ||
|
||
if (configMaxOutput <= 0) { | ||
return providedLimit; | ||
} | ||
|
||
if (providedLimit <= 0) { | ||
return configMaxOutput; | ||
} | ||
|
||
return Math.min(providedLimit, configMaxOutput); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
src/test/java/com/hubspot/jinjava/util/RenderLimitUtilsTest.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,46 @@ | ||
package com.hubspot.jinjava.util; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.hubspot.jinjava.JinjavaConfig; | ||
import org.junit.Test; | ||
|
||
public class RenderLimitUtilsTest { | ||
|
||
@Test | ||
public void itPicksLowerLimitWhenConfigIsSet() { | ||
assertThat( | ||
RenderLimitUtils.clampProvidedRenderLimitToConfig(100, configWithOutputSize(10)) | ||
) | ||
.isEqualTo(10); | ||
} | ||
|
||
@Test | ||
public void itKeepsConfigLimitWhenConfigSetAndUnlimitedProvided() { | ||
assertThat( | ||
RenderLimitUtils.clampProvidedRenderLimitToConfig(0, configWithOutputSize(10)) | ||
) | ||
.isEqualTo(10); | ||
assertThat( | ||
RenderLimitUtils.clampProvidedRenderLimitToConfig(-10, configWithOutputSize(10)) | ||
) | ||
.isEqualTo(10); | ||
} | ||
|
||
@Test | ||
public void itUsesProvidedLimitWhenConfigIsUnlimited() { | ||
assertThat( | ||
RenderLimitUtils.clampProvidedRenderLimitToConfig(10, configWithOutputSize(0)) | ||
) | ||
.isEqualTo(10); | ||
|
||
assertThat( | ||
RenderLimitUtils.clampProvidedRenderLimitToConfig(10, configWithOutputSize(-10)) | ||
) | ||
.isEqualTo(10); | ||
} | ||
|
||
private JinjavaConfig configWithOutputSize(long size) { | ||
return JinjavaConfig.newBuilder().withMaxOutputSize(size).build(); | ||
} | ||
} |