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

Side by Side Diff: java/org/chromium/distiller/webdocument/WebImage.java

Issue 2638823002: Support <picture> in image extraction (Closed)
Patch Set: support lazy loading in <picture> Created 3 years, 11 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.webdocument; 5 package org.chromium.distiller.webdocument;
6 6
7 import com.google.gwt.dom.client.Document;
8 import com.google.gwt.dom.client.Element; 7 import com.google.gwt.dom.client.Element;
9 import com.google.gwt.dom.client.ImageElement; 8 import com.google.gwt.dom.client.ImageElement;
9 import com.google.gwt.dom.client.NodeList;
10 10
11 import org.chromium.distiller.DomUtil; 11 import org.chromium.distiller.DomUtil;
12 12
13 import java.util.ArrayList; 13 import java.util.ArrayList;
14 import java.util.List; 14 import java.util.List;
15 15
16 /** 16 /**
17 * WebImage represents an image in the WebDocument potentially needing extractio n. 17 * WebImage represents an image in the WebDocument potentially needing extractio n.
18 */ 18 */
19 public class WebImage extends WebElement { 19 public class WebImage extends WebElement {
20 // The main image element. 20 // The main image element. Could be <img>, or <picture> containing <img>.
21 Element imgElement; 21 Element imgElement;
22 // The absolute source of the image. 22 // The absolute source of the image.
23 private String srcUrl; 23 private String srcUrl;
24 // The original width of the image in pixels. 24 // The original width of the image in pixels.
25 private int width; 25 private int width;
26 // The original height of the image in pixels. 26 // The original height of the image in pixels.
27 private int height; 27 private int height;
28 // Cloned and processed element. 28 // Cloned and processed element.
29 private ImageElement clonedImg; 29 private Element clonedImg;
30
31 private static final String[] LAZY_SRCSET_ATTRIBUTES =
32 {"data-srcset"};
30 33
31 /** 34 /**
32 * Build an image element. 35 * Build an image element.
33 * @param e The element detected as an image. 36 * @param e The element detected as an image.
34 * @param w The original width of the image. 37 * @param w The original width of the image.
35 * @param h The original height of the image. 38 * @param h The original height of the image.
36 * @param src The source URL of the image being extracted. 39 * @param src The source URL of the image being extracted.
37 */ 40 */
38 public WebImage(Element e, int w, int h, String src) { 41 public WebImage(Element e, int w, int h, String src) {
39 imgElement = e; 42 imgElement = e;
40 width = w; 43 width = w;
41 height = h; 44 height = h;
42 srcUrl = src; 45 srcUrl = src;
43 if (srcUrl == null) { 46 if (srcUrl == null) {
44 srcUrl = ""; 47 srcUrl = "";
45 } 48 }
46 } 49 }
47 50
48 private void cloneAndProcessNode() { 51 private void cloneAndProcessNode() {
49 ImageElement ie = ImageElement.as(Element.as(imgElement.cloneNode(false) )); 52 Element cloned = Element.as(imgElement.cloneNode(true));
50 ie.setSrc(srcUrl); 53 ImageElement ie = ImageElement.as(
51 ie.setSrc(ie.getSrc()); 54 DomUtil.getFirstElementByTagNameInc(cloned, "IMG"));
55 if (!srcUrl.isEmpty()) {
56 ie.setSrc(srcUrl);
57 }
52 // If computed width or height is zero, do not override them 58 // If computed width or height is zero, do not override them
53 // to keep them visible. 59 // to keep them visible.
54 if (width > 0 && height > 0) { 60 if (width > 0 && height > 0) {
55 ie.setWidth(width); 61 ie.setWidth(width);
56 ie.setHeight(height); 62 ie.setHeight(height);
57 } 63 }
58 DomUtil.makeSrcSetAbsolute(ie);
59 DomUtil.stripImageElement(ie); 64 DomUtil.stripImageElement(ie);
60 65
61 clonedImg = ie; 66 NodeList<Element> srcs = cloned.getElementsByTagName("SOURCE");
67 for (int i = 0; i < srcs.getLength(); i++) {
68 Element src = srcs.getItem(i);
69 for (String attr : LAZY_SRCSET_ATTRIBUTES) {
70 String srcset = src.getAttribute(attr);
71 if (!srcset.isEmpty()) {
72 src.setAttribute("srcset", srcset);
73 break;
74 }
75 }
76 }
77
78 DomUtil.makeAllSrcAttributesAbsolute(cloned);
79 DomUtil.makeAllSrcSetAbsolute(cloned);
80
81 clonedImg = cloned;
62 } 82 }
63 83
64 @Override 84 @Override
65 public String generateOutput(boolean textOnly) { 85 public String generateOutput(boolean textOnly) {
66 if (textOnly) return ""; 86 if (textOnly) return "";
67 if (clonedImg == null) { 87 if (clonedImg == null) {
68 cloneAndProcessNode(); 88 cloneAndProcessNode();
69 } 89 }
70 return clonedImg.getString(); 90 return clonedImg.getString();
71 } 91 }
(...skipping 25 matching lines...) Expand all
97 /** 117 /**
98 * Get the list of source URLs of this image. 118 * Get the list of source URLs of this image.
99 * It's more efficient to call after generateOutput(). 119 * It's more efficient to call after generateOutput().
100 * @return Source URLs or an empty List. 120 * @return Source URLs or an empty List.
101 */ 121 */
102 public List<String> getUrlList() { 122 public List<String> getUrlList() {
103 if (clonedImg == null) { 123 if (clonedImg == null) {
104 cloneAndProcessNode(); 124 cloneAndProcessNode();
105 } 125 }
106 List<String> list = new ArrayList<>(); 126 List<String> list = new ArrayList<>();
107 list.add(srcUrl); 127 if (!srcUrl.isEmpty()) {
108 list.addAll(DomUtil.getSrcSetUrls(clonedImg)); 128 list.add(srcUrl);
129 }
130 list.addAll(DomUtil.getAllSrcSetUrls(clonedImg));
109 return list; 131 return list;
110 } 132 }
111 133
112 protected ImageElement getProcessedNode() { 134 protected Element getProcessedNode() {
113 if (clonedImg == null) { 135 if (clonedImg == null) {
114 cloneAndProcessNode(); 136 cloneAndProcessNode();
115 } 137 }
116 return clonedImg; 138 return clonedImg;
117 } 139 }
118 } 140 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698