Skip to content
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

Check for widget versioning snippets #170

Merged
merged 5 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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