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

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

Powered by Google App Engine
This is Rietveld 408576698