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

Unified Diff: java/org/chromium/distiller/DomUtil.java

Issue 1507373003: Clean up attributes of image elements (Closed) Base URL: git@github.com:chromium/dom-distiller.git@master
Patch Set: add todos Created 5 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | java/org/chromium/distiller/webdocument/WebImage.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: java/org/chromium/distiller/DomUtil.java
diff --git a/java/org/chromium/distiller/DomUtil.java b/java/org/chromium/distiller/DomUtil.java
index d0bcb1bfd7330c1cc8c56be59fdbc6320918284f..b6c706d7de3fd7ac9988766d7a8b75c804391e39 100644
--- a/java/org/chromium/distiller/DomUtil.java
+++ b/java/org/chromium/distiller/DomUtil.java
@@ -216,6 +216,7 @@ public class DomUtil {
stripFontColorAttributes(clonedSubtree);
stripTableBackgroundColorAttributes(clonedSubtree);
stripStyleAttributes(clonedSubtree);
+ stripImageElements(clonedSubtree);
if (textOnly) {
return DomUtil.getTextFromTree(clonedSubtree);
@@ -260,8 +261,14 @@ public class DomUtil {
}
public static void makeSrcSetAbsolute(ImageElement ie) {
+ String srcset = ie.getAttribute("srcset");
+ if (srcset == "") {
+ ie.removeAttribute("srcset");
+ return;
+ }
+
String oldsrc = ie.getSrc();
- String[] sizes = StringUtil.jsSplit(ie.getAttribute("srcset"), ",");
+ String[] sizes = StringUtil.jsSplit(srcset, ",");
for(int i = 0; i < sizes.length; i++) {
String size = StringUtil.jsTrim(sizes[i]);
if (size.isEmpty()) continue;
@@ -274,6 +281,33 @@ public class DomUtil {
ie.setSrc(oldsrc);
}
+ public static void stripImageElements(Node root) {
+ NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG");
+ for (int i = 0; i < imgs.getLength(); i++) {
+ stripImageElement(ImageElement.as(imgs.getItem(i)));
+ }
+ }
+
+ /**
+ * Only keep some attributes for image elements.
+ * @param ie The image element to strip in-place.
+ */
+ public static void stripImageElement(ImageElement imgElement) {
+ JsArray<Node> attrs = getAttributes(imgElement);
+ for (int i = 0; i < attrs.length(); ) {
+ String name = attrs.get(i).getNodeName();
+ if (!"src".equals(name) &&
+ !"alt".equals(name) &&
+ !"srcset".equals(name) &&
+ !"dir".equals(name) &&
+ !"title".equals(name)) {
+ imgElement.removeAttribute(name);
+ } else {
+ i++;
+ }
+ }
+ }
+
/**
* Makes all "img", "source", "track", and "video" tags have an absolute "src" attribute.
* @param root The root element to look through.
« no previous file with comments | « no previous file | java/org/chromium/distiller/webdocument/WebImage.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698