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

Side by Side 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: comments addressed Created 4 years, 6 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.distiller.extractors.embeds; 5 package org.chromium.distiller.extractors.embeds;
6 6
7 import com.google.gwt.dom.client.Element; 7 import com.google.gwt.dom.client.Element;
8 import com.google.gwt.dom.client.ImageElement; 8 import com.google.gwt.dom.client.ImageElement;
9 import com.google.gwt.dom.client.NodeList;
10 import org.chromium.distiller.DomUtil;
9 import org.chromium.distiller.LogUtil; 11 import org.chromium.distiller.LogUtil;
12 import org.chromium.distiller.webdocument.WebFigure;
10 import org.chromium.distiller.webdocument.WebImage; 13 import org.chromium.distiller.webdocument.WebImage;
11 14
12 import java.util.HashSet; 15 import java.util.HashSet;
13 import java.util.Set; 16 import java.util.Set;
14 17
15 /** 18 /**
16 * This class treats images as another type of embed and provides heuristics for lead image 19 * This class treats images as another type of embed and provides heuristics for lead image
17 * candidacy. 20 * candidacy.
18 */ 21 */
19 public class ImageExtractor implements EmbedExtractor { 22 public class ImageExtractor implements EmbedExtractor {
20 private static final Set<String> relevantTags = new HashSet<>(); 23 private static final Set<String> relevantTags = new HashSet<>();
24 private String imgSrc;
25 private int width;
26 private int height;
27
21 static { 28 static {
22 // TODO(mdjones): Add "DIV" to this list for css images and possibly cap tions. 29 // TODO(mdjones): Add "DIV" to this list for css images and possibly cap tions.
23 relevantTags.add("IMG"); 30 relevantTags.add("IMG");
31 relevantTags.add("FIGURE");
24 } 32 }
33
25 private static final String[] LAZY_IMAGE_ATTRIBUTES = 34 private static final String[] LAZY_IMAGE_ATTRIBUTES =
26 {"data-src", "data-original", "datasrc", "data-url"}; 35 {"data-src", "data-original", "datasrc", "data-url"};
27 36
28 @Override 37 @Override
29 public Set<String> getRelevantTagNames() { 38 public Set<String> getRelevantTagNames() {
30 return relevantTags; 39 return relevantTags;
31 } 40 }
32 41
33 @Override 42 @Override
34 public WebImage extract(Element e) { 43 public WebImage extract(Element e) {
35 if (!relevantTags.contains(e.getTagName())) { 44 if (!relevantTags.contains(e.getTagName())) {
36 return null; 45 return null;
37 } 46 }
38 String imgSrc = ""; 47 imgSrc = "";
39 // Getting OffSetWidth/Height as default values, even they are 48
40 // affected by padding, border, etc.
41 int width = e.getOffsetWidth();
42 int height = e.getOffsetHeight();
43 if ("IMG".equals(e.getTagName())) { 49 if ("IMG".equals(e.getTagName())) {
44 // This will get the absolute URL of the image and 50 extractImageAttributes(ImageElement.as(e));
45 // the displayed image dimension. 51 return new WebImage(e, width, height, imgSrc);
46 ImageElement imageElement = ImageElement.as(e); 52 } else if ("FIGURE".equals(e.getTagName())) {
47 // Try to get lazily-loaded images before falling back to get the sr c attribute. 53 Element img = getFirstElementByTagName(e, "IMG");
48 for(String attr: LAZY_IMAGE_ATTRIBUTES) { 54 if (img != null) {
49 imgSrc = imageElement.getAttribute(attr); 55 String caption;
50 if (!imgSrc.isEmpty()) 56 extractImageAttributes(ImageElement.as(img));
51 break; 57 Element cap = getFirstElementByTagName(e, "FIGCAPTION");
52 } 58 if (cap != null) {
53 if (!imgSrc.isEmpty()) { 59 caption = getFirstElementByTagName(cap, "A") != null ?
54 // We cannot trust the dimension if the image is not loaded yet. 60 cap.getInnerHTML() : DomUtil.escapeHTML(cap.getInner Text());
55 // In some cases there are 1x1 placeholder images. 61 } else {
56 width = 0; 62 caption = DomUtil.escapeHTML(e.getInnerText());
57 height = 0; 63 }
58 } else { 64 return new WebFigure(img, width, height, imgSrc, caption);
59 imgSrc = imageElement.getSrc();
60 // As an ImageElement is manipulated here, it is possible
61 // to get the real dimensions.
62 width = imageElement.getWidth();
63 height = imageElement.getHeight();
64 } 65 }
65 } 66 }
67 return null;
68 }
66 69
70 private void extractImageAttributes(ImageElement imageElement) {
71 // This will get the absolute URL of the image and
72 // the displayed image dimension.
73 // Try to get lazily-loaded images before falling back to get the src at tribute.
74 for (String attr : LAZY_IMAGE_ATTRIBUTES) {
75 imgSrc = imageElement.getAttribute(attr);
76 if (!imgSrc.isEmpty())
77 break;
78 }
79 if (!imgSrc.isEmpty()) {
80 // We cannot trust the dimension if the image is not loaded yet.
81 // In some cases there are 1x1 placeholder images.
82 width = 0;
83 height = 0;
84 } else {
85 imgSrc = imageElement.getSrc();
86 // As an ImageElement is manipulated here, it is possible
87 // to get the real dimensions.
88 width = imageElement.getWidth();
89 height = imageElement.getHeight();
90 }
67 if (LogUtil.isLoggable(LogUtil.DEBUG_LEVEL_VISIBILITY_INFO)) { 91 if (LogUtil.isLoggable(LogUtil.DEBUG_LEVEL_VISIBILITY_INFO)) {
68 LogUtil.logToConsole("Extracted WebImage: " + imgSrc); 92 LogUtil.logToConsole("Extracted WebImage: " + imgSrc);
69 } 93 }
70 return new WebImage(e, width, height, imgSrc); 94 }
95
96 private Element getFirstElementByTagName(Element e, String tagName) {
97 NodeList<Element> elements = e.getElementsByTagName(tagName);
98 if (elements.getLength() > 0) {
99 return elements.getItem(0);
100 }
101 return null;
71 } 102 }
72 } 103 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698