Skip to content

Commit

Permalink
Check for widget versioning snippets (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
bstewart00 authored Apr 13, 2021
1 parent cbc7b68 commit 7be64ab
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
sudo apt-get update
sudo apt install -y maven
cd ~/
curl -O https://bootstrap.pypa.io/get-pip.py
curl -O https://bootstrap.pypa.io/pip/2.7/get-pip.py
python get-pip.py --user
export PATH=~/.local/bin/:$PATH
pip install awscli --upgrade --user
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
.project
.settings/
.vscode/
bin/

docker/java7/hello/target
docker/java8/hello/target
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.github.wovnio</groupId>
<artifactId>wovnjava</artifactId>
<name>wovnjava</name>
<version>1.6.0</version>
<version>1.6.1</version>
<url>https://github.com/WOVNio/wovnjava</url>

<licenses>
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/com/github/wovnio/wovnjava/HtmlConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class HtmlConverter {
private final HashMap<String, String> hreflangMap;
private final HtmlReplaceMarker htmlReplaceMarker;

private static final String[] WOVN_WIDGET_URLS = new String[] {
"j.wovn.io",
"j.dev-wovn.io:3000",
Settings.DefaultVersionedWidgetUrlProduction
};

HtmlConverter(Settings settings, Headers headers, String original) {
this.settings = settings;
this.htmlReplaceMarker = new HtmlReplaceMarker();
Expand Down Expand Up @@ -60,15 +66,25 @@ private void removeHrefLangIfConflicts() {
}
}

private boolean isSnippet(String src) {
return src != null && (src.startsWith("//j.wovn.io/") || src.startsWith("//j.dev-wovn.io:3000/"));
private boolean isSnippet(Element element) {
String src = element.attr("src");
String dataAttr = element.attr("data-wovnio");

if (src != null) {
for (String wovnUrl : WOVN_WIDGET_URLS) {
if (src.contains(wovnUrl)) {
return true;
}
}
}

return dataAttr != null && dataAttr.length() > 0;
}

private void removeSnippet() {
Elements elements = doc.getElementsByTag("script");
for (Element element : elements) {
String src = element.attr("src");
if (isSnippet(src)) {
if (isSnippet(element)) {
element.remove();
}
}
Expand All @@ -77,8 +93,7 @@ private void removeSnippet() {
private void removeSnippetAndScripts() {
Elements elements = doc.getElementsByTag("script");
for (Element element : elements) {
String src = element.attr("src");
if (isSnippet(src)) {
if (isSnippet(element)) {
element.remove();
} else {
replaceNodeToMarkerComment(element);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/github/wovnio/wovnjava/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class Settings {

// Default configuration values
public static final int DefaultTimeout = 1000;
public static final String DefaultApiUrlProduction = "https://wovn.global.ssl.fastly.net/v0/";
public static final String DefaultApiUrlBase = "https://wovn.global.ssl.fastly.net";
public static final String DefaultApiUrlProduction = DefaultApiUrlBase + "/v0/";
public static final String DefaultVersionedWidgetUrlProduction = DefaultApiUrlBase + "/widget";
public static final String DefaultApiUrlDevelopment = "http://localhost:3001/v0/";
public static final String DefaultSnippetUrlProduction = "//j.wovn.io/1";
public static final String DefaultSnippetUrlDevelopment = "//j.dev-wovn.io:3000/1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void testDisablePrettyPrint() throws ConfigurationError {
}

public void testRemoveWovnSnippet() throws ConfigurationError {
String original = "<html><head><script src=\"//j.wovn.io/1\" data-wovnio=\"key=NCmbvk&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=path&amp;version=0.0.0\" data-wovnio-type=\"backend_without_api\" async></script></head><body></body></html>";
String original = "<html><head><script src=\"https://wovn.global.ssl.fastly.net/widget/abcdef\"></script><script src=\"https://j.dev-wovn.io:3000\"></script><script src=\"//j.wovn.io/1\" data-wovnio=\"key=NCmbvk&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=path&amp;version=0.0.0\" data-wovnio-type=\"backend_without_api\" async></script></head><body></body></html>";
String removedHtml = "<html lang=\"en\"><head></head><body></body></html>";
Settings settings = TestUtil.makeSettings(new HashMap<String, String>() {{ put("supportedLangs", "en,fr,ja"); }});
HtmlConverter converter = this.createHtmlConverter(settings, location, original);
Expand Down

0 comments on commit 7be64ab

Please sign in to comment.