Chromium Code Reviews| OLD | NEW |
|---|---|
| 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; | 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 | 30 |
| 31 /** | 31 /** |
| 32 * Build an image element. | 32 * Build an image element. |
| 33 * @param e The element detected as an image. | 33 * @param e The element detected as an image. |
| 34 * @param w The original width of the image. | 34 * @param w The original width of the image. |
| 35 * @param h The original height of the image. | 35 * @param h The original height of the image. |
| 36 * @param src The source URL of the image being extracted. | 36 * @param src The source URL of the image being extracted. |
| 37 */ | 37 */ |
| 38 public WebImage(Element e, int w, int h, String src) { | 38 public WebImage(Element e, int w, int h, String src) { |
| 39 imgElement = e; | 39 imgElement = e; |
| 40 width = w; | 40 width = w; |
| 41 height = h; | 41 height = h; |
| 42 srcUrl = src; | 42 srcUrl = src; |
| 43 if (srcUrl == null) { | 43 if (srcUrl == null) { |
| 44 srcUrl = ""; | 44 srcUrl = ""; |
| 45 } | 45 } |
| 46 } | 46 } |
| 47 | 47 |
| 48 private void cloneAndProcessNode() { | 48 private void cloneAndProcessNode() { |
| 49 ImageElement ie = ImageElement.as(Element.as(imgElement.cloneNode(false) )); | 49 Element cloned = Element.as(imgElement.cloneNode(true)); |
| 50 ie.setSrc(srcUrl); | 50 if (imgElement.getTagName() == "IMG") { |
| 51 ie.setSrc(ie.getSrc()); | 51 ImageElement ie = ImageElement.as(cloned); |
| 52 // If computed width or height is zero, do not override them | 52 ie.setSrc(srcUrl); |
| 53 // to keep them visible. | 53 // If computed width or height is zero, do not override them |
| 54 if (width > 0 && height > 0) { | 54 // to keep them visible. |
| 55 ie.setWidth(width); | 55 if (width > 0 && height > 0) { |
| 56 ie.setHeight(height); | 56 ie.setWidth(width); |
| 57 ie.setHeight(height); | |
| 58 } | |
| 59 DomUtil.stripImageElement(ie); | |
| 57 } | 60 } |
| 58 DomUtil.makeSrcSetAbsolute(ie); | 61 DomUtil.makeAllSrcAttributesAbsolute(cloned); |
| 59 DomUtil.stripImageElement(ie); | 62 DomUtil.makeAllSrcSetAbsolute(cloned); |
| 60 | 63 |
| 61 clonedImg = ie; | 64 clonedImg = cloned; |
| 62 } | 65 } |
| 63 | 66 |
| 64 @Override | 67 @Override |
| 65 public String generateOutput(boolean textOnly) { | 68 public String generateOutput(boolean textOnly) { |
| 66 if (textOnly) return ""; | 69 if (textOnly) return ""; |
| 67 if (clonedImg == null) { | 70 if (clonedImg == null) { |
| 68 cloneAndProcessNode(); | 71 cloneAndProcessNode(); |
| 69 } | 72 } |
| 70 return clonedImg.getString(); | 73 return clonedImg.getString(); |
| 71 } | 74 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 97 /** | 100 /** |
| 98 * Get the list of source URLs of this image. | 101 * Get the list of source URLs of this image. |
| 99 * It's more efficient to call after generateOutput(). | 102 * It's more efficient to call after generateOutput(). |
| 100 * @return Source URLs or an empty List. | 103 * @return Source URLs or an empty List. |
| 101 */ | 104 */ |
| 102 public List<String> getUrlList() { | 105 public List<String> getUrlList() { |
| 103 if (clonedImg == null) { | 106 if (clonedImg == null) { |
| 104 cloneAndProcessNode(); | 107 cloneAndProcessNode(); |
| 105 } | 108 } |
| 106 List<String> list = new ArrayList<>(); | 109 List<String> list = new ArrayList<>(); |
| 107 list.add(srcUrl); | 110 if (!srcUrl.isEmpty()) { |
| 108 list.addAll(DomUtil.getSrcSetUrls(clonedImg)); | 111 list.add(srcUrl); |
|
mdjones
2017/01/17 17:41:47
nit: this can be in-lined, but it doesn't look lik
wychen
2017/01/18 17:29:54
Acknowledged.
| |
| 112 } | |
| 113 list.addAll(DomUtil.getAllSrcSetUrls(clonedImg)); | |
| 109 return list; | 114 return list; |
| 110 } | 115 } |
| 111 | 116 |
| 112 protected ImageElement getProcessedNode() { | 117 protected Element getProcessedNode() { |
| 113 if (clonedImg == null) { | 118 if (clonedImg == null) { |
| 114 cloneAndProcessNode(); | 119 cloneAndProcessNode(); |
| 115 } | 120 } |
| 116 return clonedImg; | 121 return clonedImg; |
| 117 } | 122 } |
| 118 } | 123 } |
| OLD | NEW |