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

Unified Diff: java/org/chromium/distiller/extractors/embeds/ImageExtractor.java

Issue 2020403002: Add support for figure element (Closed) Base URL: https://github.com/chromium/dom-distiller.git@master
Patch Set: Created 4 years, 7 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/extractors/embeds/ImageExtractor.java
diff --git a/java/org/chromium/distiller/extractors/embeds/ImageExtractor.java b/java/org/chromium/distiller/extractors/embeds/ImageExtractor.java
index c9b527a117c8c5d99d483b94de5ff2bd6d486f9c..107262f91069a19755b1277785dcd0f4984770d4 100644
--- a/java/org/chromium/distiller/extractors/embeds/ImageExtractor.java
+++ b/java/org/chromium/distiller/extractors/embeds/ImageExtractor.java
@@ -6,6 +6,8 @@ package org.chromium.distiller.extractors.embeds;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.ImageElement;
+import com.google.gwt.dom.client.NodeList;
+import org.chromium.distiller.webdocument.WebFigure;
wychen 2016/05/31 21:28:04 Forget to upload this one?
import org.chromium.distiller.webdocument.WebImage;
import java.util.HashSet;
@@ -17,9 +19,15 @@ import java.util.Set;
*/
public class ImageExtractor implements EmbedExtractor {
private static final Set<String> relevantTags = new HashSet<>();
+ private String src;
+ private String caption;
+ private int width;
+ private int height;
+
static {
// TODO(mdjones): Add "DIV" to this list for css images and possibly captions.
relevantTags.add("IMG");
+ relevantTags.add("FIGURE");
}
@Override
@@ -32,22 +40,40 @@ public class ImageExtractor implements EmbedExtractor {
if (!relevantTags.contains(e.getTagName())) {
return null;
}
- String imgSrc = "";
// Getting OffSetWidth/Height as default values, even they are
// affected by padding, border, etc.
- int width = e.getOffsetWidth();
- int height = e.getOffsetHeight();
+ width = e.getOffsetWidth();
+ height = e.getOffsetHeight();
+ src = "";
+ caption = "";
+
if ("IMG".equals(e.getTagName())) {
- // This will get the absolute URL of the image and
- // the displayed image dimension.
- ImageElement imageElement = ImageElement.as(e);
- imgSrc = imageElement.getSrc();
- // As an ImageElement is manipulated here, it is possible
- // to get the real dimensions.
- width = imageElement.getWidth();
- height = imageElement.getHeight();
+ extractImageAttributes(ImageElement.as(e));
+ } else if ("FIGURE".equals(e.getTagName())) {
+ Element img = getElementByTagName(e, "IMG");
+ if (img != null) {
wychen 2016/05/31 21:28:04 If a malformed <figure> contains no <img>s, an emp
marcelorcorrea 2016/05/31 21:46:44 Done.
+ extractImageAttributes(ImageElement.as(img));
+ Element cap = getElementByTagName(e, "FIGCAPTION");
+ if (cap != null) {
+ caption = cap.getInnerText();
+ }
+ return new WebFigure(img, width, height, src, caption);
+ }
}
+ return new WebImage(e, width, height, src);
+ }
- return new WebImage(e, width, height, imgSrc);
+ private void extractImageAttributes(ImageElement img) {
+ src = img.getSrc();
+ width = img.getWidth();
+ height = img.getHeight();
+ }
+
+ private Element getElementByTagName(Element e, String tagName) {
wychen 2016/05/31 21:28:04 getFirstElementByTagName?
marcelorcorrea 2016/05/31 21:46:44 Done.
+ NodeList<Element> elements = e.getElementsByTagName(tagName);
+ if (elements.getLength() > 0) {
+ return elements.getItem(0);
+ }
+ return null;
}
}

Powered by Google App Engine
This is Rietveld 408576698