Skip to content

Commit

Permalink
Merge pull request #72 from WOVNio/class_ignore
Browse files Browse the repository at this point in the history
Implement HTML class ignore.
  • Loading branch information
hythof authored Jul 25, 2018
2 parents 1fb8340 + ccdbfec commit 3f49cee
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ useProxy | | 'false'
debugMode | | '0'
originalUrlHeader | | ''
originalQueryStringHeader | | ''
ignoreClasses | | ''

* A required parameter with a default setting does not need to be set within the web.xml. (Only the projectToken and secretKey parameters must be set in order for the library to work)

Expand Down Expand Up @@ -182,3 +183,31 @@ wovnjava will use the following settings along with the correct URL (prior to re
* The sample request header shown above was referenced from the following site.

https://coderwall.com/p/jhkw7w/passing-request-uri-into-request-header

### 2.9. ignoreClasses

This parameter is a comma-separated list of HTML classes for which you would like WOVN to skip the elements of.

For example, if you include `my-secret-class` in this parameter and you have an element as follows
```HTML
<div>
<p class="my-secret-class">Some information WOVN does not touch</p>
</div>
```
WOVN will treat it as
```HTML
<div></div>
```

Including three classes, `email-address-element`, `my-secret-class`, and `noshow`, in your ignoreClasses parameter would look as follows

```XML
<filter>
...
<init-param>
<param-name>ignoreClasses</param-name>
<param-value>email-address-element,my-secret-class,noshow</param-value>
</init-param>
...
</filter>
```
10 changes: 10 additions & 0 deletions src/main/java/com/github/wovnio/wovnjava/HtmlConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ String strip() {
removeSnippetAndScripts();
removeHrefLangIfConflicts();
removeWovnIgnore();
removeClassIgnore();
removeForm();
return doc.html();
}
Expand Down Expand Up @@ -97,6 +98,15 @@ private void removeWovnIgnore() {
}
}

private void removeClassIgnore() {
if (settings.ignoreClasses.isEmpty()) return;
String classNames = "." + String.join(", .", settings.ignoreClasses);
Elements elements = doc.select(classNames);
for (Element element : elements) {
replaceNodeToMarkerComment(element);
}
}

private void removeForm() {
Elements elements = doc.getElementsByTag("input");
for (Element element : elements) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/github/wovnio/wovnjava/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Settings {
String apiUrl = "https://wovn.global.ssl.fastly.net/v0/";
String defaultLang = "en";
ArrayList<String> supportedLangs;
ArrayList<String> ignoreClasses;
boolean useProxy = false;
String originalUrlHeader = "";
String originalQueryStringHeader = "";
Expand All @@ -41,6 +42,7 @@ class Settings {
this.query = new ArrayList<String>();
this.supportedLangs = new ArrayList<String>();
this.supportedLangs.add("en");
this.ignoreClasses = new ArrayList<String>();

String p;

Expand Down Expand Up @@ -101,6 +103,11 @@ class Settings {
this.supportedLangs = getArrayParameter(p);
}

p = config.getInitParameter("ignoreClasses");
if (p != null && p.length() > 0) {
this.ignoreClasses = getArrayParameter(p);
}

p = config.getInitParameter("useProxy");
if (p != null && p.length() > 0) {
this.useProxy = getBoolParameter(p);
Expand Down
35 changes: 33 additions & 2 deletions src/test/java/com/github/wovnio/wovnjava/HtmlConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ public void testRemoveWovnIgnore() {
assertEquals(original, stripExtraSpaces(converter.restore(html)));
}

public void testRemoveClassIgnore() {
String original = "<html><head></head><body>" +
"<p class=\"no-ignore\">The pizza needs <b class=\"ingredient\">pineapple</b>, <span class=\"name\">Chad</span>!</p>" +
"<p class=\"ignore-me\">It's a fruit, <span class=\"name\">Louie</span>!</p>" +
"</body></html>";
String removedHtml = "<html><head></head><body>" +
"<p class=\"no-ignore\">The pizza needs <!--wovn-marker-0-->, <!--wovn-marker-1-->!</p>" +
"<!--wovn-marker-2-->" +
"</body></html>";
Settings settings = TestUtil.makeSettings(new HashMap<String, String>() {{
put("supportedLangs", "en,fr,ja");
put("ignoreClasses", "ignore-me,name,ingredient");
}});
HtmlConverter converter = new HtmlConverter(settings, original);
String html = converter.strip();
assertEquals("ignore-me & name & ingredient", String.join(" & ", settings.ignoreClasses));
assertEquals(removedHtml, stripExtraSpaces(html));
assertEquals(original, stripExtraSpaces(converter.restore(html)));
}

public void testRemoveForm() {
String original = "<html><head></head><body><form><input type=\"hidden\" name=\"csrf\" value=\"random\"><INPUT TYPE=\"HIDDEN\" NAME=\"CSRF_TOKEN\" VALUE=\"RANDOM\"></form></body></html>";
String removedHtml = "<html><head></head><body><form><!--wovn-marker-0--><!--wovn-marker-1--></form></body></html>";
Expand Down Expand Up @@ -94,6 +114,10 @@ public void testMixAllCase() {
"<script>6</script>" +
"<script>7</script>" +
"<script>8</script>" +
"<div class=\"class-ignore-test\">" +
"<p class=\"no-ignore\">The pizza needs <b class=\"ingredient\">pineapple</b>, <span class=\"name\" wovn-ignore>Chad</span>!</p>" +
"<p class=\"ignore-me\">It's a fruit, <span class=\"name\" wovn-ignore>Louie</span>!</p>" +
"</div>" +
"<script>9</script>" +
"<script>10</script>" +
"</body></html>";
Expand All @@ -103,16 +127,23 @@ public void testMixAllCase() {
"</head><body>" +
"a <!--wovn-marker-1-->b" +
"<div>Hello <!--wovn-marker-9-->.</div>" +
"<form><!--wovn-marker-10--></form>" +
"<form><!--wovn-marker-14--></form>" +
"<!--wovn-marker-2-->" +
"<!--wovn-marker-3-->" +
"<!--wovn-marker-4-->" +
"<!--wovn-marker-5-->" +
"<!--wovn-marker-6-->" +
"<div class=\"class-ignore-test\">" +
"<p class=\"no-ignore\">The pizza needs <!--wovn-marker-12-->, <!--wovn-marker-10-->!</p>" +
"<!--wovn-marker-13-->" +
"</div>" +
"<!--wovn-marker-7-->" +
"<!--wovn-marker-8-->" +
"</body></html>";
Settings settings = TestUtil.makeSettings(new HashMap<String, String>() {{ put("supportedLangs", "en,fr,ja"); }});
Settings settings = TestUtil.makeSettings(new HashMap<String, String>() {{
put("supportedLangs", "en,fr,ja");
put("ignoreClasses", "ignore-me,name,ingredient");
}});
HtmlConverter converter = new HtmlConverter(settings, original);
String html = converter.strip();
assertEquals(removedHtml, stripExtraSpaces(html));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/github/wovnio/wovnjava/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static FilterConfig makeConfig() {

public static FilterConfig makeConfig(HashMap<String, String> option) {
FilterConfig mock = EasyMock.createMock(FilterConfig.class);
String[] keys = {"userToken", "projectToken", "sitePrefixPath", "secretKey", "urlPattern", "urlPatternReg", "query", "apiUrl", "defaultLang", "supportedLangs", "testMode", "testUrl", "useProxy", "debugMode", "originalUrlHeader", "originalQueryStringHeader", "strictHtmlCheck", "deleteInvalidClosingTag", "deleteInvalidUTF8", "connectTimeout", "readTimeout"};
String[] keys = {"userToken", "projectToken", "sitePrefixPath", "secretKey", "urlPattern", "urlPatternReg", "query", "apiUrl", "defaultLang", "supportedLangs", "testMode", "testUrl", "useProxy", "debugMode", "originalUrlHeader", "originalQueryStringHeader", "strictHtmlCheck", "deleteInvalidClosingTag", "deleteInvalidUTF8", "connectTimeout", "readTimeout", "ignoreClasses"};
for (int i=0; i<keys.length; ++i) {
String key = keys[i];
String val = option.get(key);
Expand Down

0 comments on commit 3f49cee

Please sign in to comment.