Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(785)

Unified Diff: java/org/chromium/distiller/webdocument/WebImage.java

Issue 2203563002: Extract image URLs in srcset as well (Closed) Base URL: git@github.com:chromium/dom-distiller.git@master
Patch Set: format Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: java/org/chromium/distiller/webdocument/WebImage.java
diff --git a/java/org/chromium/distiller/webdocument/WebImage.java b/java/org/chromium/distiller/webdocument/WebImage.java
index ceb3a405279c3dbf95c723c6eb9ced12f7a095b6..d58f4e43bf0159c9aa8eea5fe07ba2bfc348bf26 100644
--- a/java/org/chromium/distiller/webdocument/WebImage.java
+++ b/java/org/chromium/distiller/webdocument/WebImage.java
@@ -10,6 +10,9 @@ import com.google.gwt.dom.client.ImageElement;
import org.chromium.distiller.DomUtil;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* WebImage represents an image in the WebDocument potentially needing extraction.
*/
@@ -22,6 +25,8 @@ public class WebImage extends WebElement {
private int width;
// The original height of the image in pixels.
private int height;
+ // Cloned and processed element.
+ private ImageElement clonedImg;
/**
* Build an image element.
@@ -40,10 +45,7 @@ public class WebImage extends WebElement {
}
}
- @Override
- public String generateOutput(boolean textOnly) {
- if (textOnly) return "";
-
+ private void cloneAndProcessNode() {
ImageElement ie = ImageElement.as(Element.as(imgElement.cloneNode(false)));
ie.setSrc(srcUrl);
ie.setSrc(ie.getSrc());
@@ -56,8 +58,17 @@ public class WebImage extends WebElement {
DomUtil.makeSrcSetAbsolute(ie);
DomUtil.stripImageElement(ie);
+ clonedImg = ie;
+ }
+
+ @Override
+ public String generateOutput(boolean textOnly) {
+ if (textOnly) return "";
+ if (clonedImg == null) {
+ cloneAndProcessNode();
+ }
Element container = Document.get().createDivElement();
- container.appendChild(ie);
+ container.appendChild(clonedImg);
return container.getInnerHTML();
}
@@ -86,10 +97,17 @@ public class WebImage extends WebElement {
}
/**
- * Get the source URL of this image.
- * @return Source URL or an empty string.
+ * Get the list of source URLs of this image.
+ * It's more efficient to call after generateOutput().
+ * @return Source URLs or an empty List.
*/
- public String getSrc() {
- return srcUrl;
+ public List<String> getUrlList() {
+ if (clonedImg == null) {
+ cloneAndProcessNode();
+ }
+ List<String> list = new ArrayList<>();
+ list.add(srcUrl);
+ list.addAll(DomUtil.getSrcSetUrls(clonedImg));
+ return list;
}
}
« no previous file with comments | « java/org/chromium/distiller/webdocument/WebDocument.java ('k') | javatests/org/chromium/distiller/DomUtilTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698