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

Side by Side 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, 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.webdocument; 5 package org.chromium.distiller.webdocument;
6 6
7 import com.google.gwt.dom.client.Document; 7 import com.google.gwt.dom.client.Document;
8 import com.google.gwt.dom.client.Element; 8 import com.google.gwt.dom.client.Element;
9 import com.google.gwt.dom.client.ImageElement; 9 import com.google.gwt.dom.client.ImageElement;
10 10
11 import org.chromium.distiller.DomUtil; 11 import org.chromium.distiller.DomUtil;
12 12
13 import java.util.ArrayList;
14 import java.util.List;
15
13 /** 16 /**
14 * WebImage represents an image in the WebDocument potentially needing extractio n. 17 * WebImage represents an image in the WebDocument potentially needing extractio n.
15 */ 18 */
16 public class WebImage extends WebElement { 19 public class WebImage extends WebElement {
17 // The main image element. 20 // The main image element.
18 Element imgElement; 21 Element imgElement;
19 // The absolute source of the image. 22 // The absolute source of the image.
20 private String srcUrl; 23 private String srcUrl;
21 // The original width of the image in pixels. 24 // The original width of the image in pixels.
22 private int width; 25 private int width;
23 // The original height of the image in pixels. 26 // The original height of the image in pixels.
24 private int height; 27 private int height;
28 // Cloned and processed element.
29 private ImageElement clonedImg;
25 30
26 /** 31 /**
27 * Build an image element. 32 * Build an image element.
28 * @param e The element detected as an image. 33 * @param e The element detected as an image.
29 * @param w The original width of the image. 34 * @param w The original width of the image.
30 * @param h The original height of the image. 35 * @param h The original height of the image.
31 * @param src The source URL of the image being extracted. 36 * @param src The source URL of the image being extracted.
32 */ 37 */
33 public WebImage(Element e, int w, int h, String src) { 38 public WebImage(Element e, int w, int h, String src) {
34 imgElement = e; 39 imgElement = e;
35 width = w; 40 width = w;
36 height = h; 41 height = h;
37 srcUrl = src; 42 srcUrl = src;
38 if (srcUrl == null) { 43 if (srcUrl == null) {
39 srcUrl = ""; 44 srcUrl = "";
40 } 45 }
41 } 46 }
42 47
43 @Override 48 private void cloneAndProcessNode() {
44 public String generateOutput(boolean textOnly) {
45 if (textOnly) return "";
46
47 ImageElement ie = ImageElement.as(Element.as(imgElement.cloneNode(false) )); 49 ImageElement ie = ImageElement.as(Element.as(imgElement.cloneNode(false) ));
48 ie.setSrc(srcUrl); 50 ie.setSrc(srcUrl);
49 ie.setSrc(ie.getSrc()); 51 ie.setSrc(ie.getSrc());
50 // If computed width or height is zero, do not override them 52 // If computed width or height is zero, do not override them
51 // to keep them visible. 53 // to keep them visible.
52 if (width > 0 && height > 0) { 54 if (width > 0 && height > 0) {
53 ie.setWidth(width); 55 ie.setWidth(width);
54 ie.setHeight(height); 56 ie.setHeight(height);
55 } 57 }
56 DomUtil.makeSrcSetAbsolute(ie); 58 DomUtil.makeSrcSetAbsolute(ie);
57 DomUtil.stripImageElement(ie); 59 DomUtil.stripImageElement(ie);
58 60
61 clonedImg = ie;
62 }
63
64 @Override
65 public String generateOutput(boolean textOnly) {
66 if (textOnly) return "";
67 if (clonedImg == null) {
68 cloneAndProcessNode();
69 }
59 Element container = Document.get().createDivElement(); 70 Element container = Document.get().createDivElement();
60 container.appendChild(ie); 71 container.appendChild(clonedImg);
61 return container.getInnerHTML(); 72 return container.getInnerHTML();
62 } 73 }
63 74
64 /** 75 /**
65 * Get the image element of this WebImage. 76 * Get the image element of this WebImage.
66 * @return Image element or null. 77 * @return Image element or null.
67 */ 78 */
68 public Element getImageNode() { 79 public Element getImageNode() {
69 return imgElement; 80 return imgElement;
70 } 81 }
71 82
72 /** 83 /**
73 * Get the width of this image in pixels. 84 * Get the width of this image in pixels.
74 * @return The width of this image in pixels. 85 * @return The width of this image in pixels.
75 */ 86 */
76 public int getWidth() { 87 public int getWidth() {
77 return width; 88 return width;
78 } 89 }
79 90
80 /** 91 /**
81 * Get the height of this image in pixels. 92 * Get the height of this image in pixels.
82 * @return The height of this image in pixels. 93 * @return The height of this image in pixels.
83 */ 94 */
84 public int getHeight() { 95 public int getHeight() {
85 return height; 96 return height;
86 } 97 }
87 98
88 /** 99 /**
89 * Get the source URL of this image. 100 * Get the list of source URLs of this image.
90 * @return Source URL or an empty string. 101 * It's more efficient to call after generateOutput().
102 * @return Source URLs or an empty List.
91 */ 103 */
92 public String getSrc() { 104 public List<String> getUrlList() {
93 return srcUrl; 105 if (clonedImg == null) {
106 cloneAndProcessNode();
107 }
108 List<String> list = new ArrayList<>();
109 list.add(srcUrl);
110 list.addAll(DomUtil.getSrcSetUrls(clonedImg));
111 return list;
94 } 112 }
95 } 113 }
OLDNEW
« 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