Index: java/org/chromium/distiller/DomUtil.java |
diff --git a/java/org/chromium/distiller/DomUtil.java b/java/org/chromium/distiller/DomUtil.java |
index 45d78a2ee01484f646eb79e2255f1f6bc52c2838..859ae623c109e5ba44c57d85a4ce4e11d9509fa0 100644 |
--- a/java/org/chromium/distiller/DomUtil.java |
+++ b/java/org/chromium/distiller/DomUtil.java |
@@ -15,6 +15,8 @@ import com.google.gwt.dom.client.NodeList; |
import com.google.gwt.dom.client.Style; |
import com.google.gwt.dom.client.VideoElement; |
import com.google.gwt.http.client.URL; |
+import com.google.gwt.regexp.shared.MatchResult; |
+import com.google.gwt.regexp.shared.RegExp; |
import java.util.ArrayList; |
import java.util.HashMap; |
@@ -495,6 +497,59 @@ public class DomUtil { |
return nodes; |
} |
+ public static String formatDuration(String duration) { |
+ RegExp pattern = RegExp.compile("^P(?:([0-9]+)Y)?" + |
+ "(?:([0-9]+)M)?(?:([0-9]+)W)?(?:([0-9]+)D)?" + |
+ "(T(?:([0-9]+)H)?(?:([0-9]+)M)?(?:([0-9]+)S)?)?$", "i"); |
+ MatchResult matchResult = pattern.exec(duration); |
+ List<String> result = new ArrayList<>(); |
+ if (matchResult != null) { |
+ if (matchResult.getGroup(1) != null) { |
+ result.add(matchResult.getGroup(1) + " year(s)"); |
wychen
2016/07/24 23:06:34
Can we handle plural forms?
|
+ } |
+ if (matchResult.getGroup(2) != null) { |
+ result.add(matchResult.getGroup(2) + " month(s)"); |
+ } |
+ if (matchResult.getGroup(3) != null) { |
+ result.add(matchResult.getGroup(3) + " week(s)"); |
+ } |
+ if (matchResult.getGroup(4) != null) { |
+ result.add(matchResult.getGroup(4) + " day(s)"); |
+ } |
+ if (matchResult.getGroup(6) != null) { |
+ result.add(matchResult.getGroup(6) + " hour(s)"); |
+ } |
+ if (matchResult.getGroup(7) != null) { |
+ result.add(matchResult.getGroup(7) + " minute(s)"); |
+ } |
+ if (matchResult.getGroup(8) != null) { |
+ result.add(matchResult.getGroup(8) + " second(s)"); |
+ } |
+ } |
+ return join(result.toArray(), " "); |
+ } |
+ |
+ /** |
+ * Tries to get the language of the web page. It looks for |
+ * the 'lang' attribute in the HTML tag, if it doesn't find it looks |
+ * the meta tags for Content-Language or Language properties. |
+ * |
+ * @param root The root element. |
+ * @return A string containing the language(s) or empty. |
+ */ |
+ public static String getLanguage(Element root) { |
+ String language = root.getLang(); |
+ if (language.isEmpty()) { |
+ String query = "META[HTTP-EQUIV=\"content-language\" i][CONTENT]," + |
+ "META[NAME=\"language\" i][CONTENT]"; |
+ NodeList<Element> languages = DomUtil.querySelectorAll(root, query); |
wychen
2016/07/24 23:06:34
Would it be faster if we only handle <head> instea
|
+ if (languages.getLength() > 0) { |
+ language = languages.getItem(0).getAttribute("CONTENT"); |
+ } |
+ } |
+ return language; |
+ } |
+ |
public static int getArea(Element e) { |
if (e != null) { |
return e.getOffsetHeight() * e.getOffsetWidth(); |
@@ -530,4 +585,8 @@ public class DomUtil { |
public static native Element getFirstElementChild(Document document) /*-{ |
return document.firstElementChild; |
}-*/; |
+ |
+ public static native String join(Object[] list, String conjunction) /*-{ |
+ return list.join(conjunction); |
+ }-*/; |
} |