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

Side by Side 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: 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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; 5 package org.chromium.distiller;
6 6
7 import com.google.gwt.core.client.JsArray; 7 import com.google.gwt.core.client.JsArray;
8 import com.google.gwt.core.client.JsArrayString; 8 import com.google.gwt.core.client.JsArrayString;
9 import com.google.gwt.dom.client.AnchorElement; 9 import com.google.gwt.dom.client.AnchorElement;
10 import com.google.gwt.dom.client.Document; 10 import com.google.gwt.dom.client.Document;
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 NodeTree expanded = NodeListExpander.expand(outputNodes); 209 NodeTree expanded = NodeListExpander.expand(outputNodes);
210 Node clonedSubtree = expanded.cloneSubtreeRetainDirection(); 210 Node clonedSubtree = expanded.cloneSubtreeRetainDirection();
211 211
212 if (clonedSubtree.getNodeType() != Node.ELEMENT_NODE) return ""; 212 if (clonedSubtree.getNodeType() != Node.ELEMENT_NODE) return "";
213 213
214 stripIds(clonedSubtree); 214 stripIds(clonedSubtree);
215 makeAllLinksAbsolute(clonedSubtree); 215 makeAllLinksAbsolute(clonedSubtree);
216 stripFontColorAttributes(clonedSubtree); 216 stripFontColorAttributes(clonedSubtree);
217 stripTableBackgroundColorAttributes(clonedSubtree); 217 stripTableBackgroundColorAttributes(clonedSubtree);
218 stripStyleAttributes(clonedSubtree); 218 stripStyleAttributes(clonedSubtree);
219 stripAlignAttributes(clonedSubtree);
219 220
220 if (textOnly) { 221 if (textOnly) {
221 return DomUtil.getTextFromTree(clonedSubtree); 222 return DomUtil.getTextFromTree(clonedSubtree);
222 } 223 }
223 return Element.as(clonedSubtree).getString(); 224 return Element.as(clonedSubtree).getString();
224 } 225 }
225 226
226 /** 227 /**
227 * Makes all anchors and video posters absolute. This calls "makeAllSrcAttri butesAbsolute". 228 * Makes all anchors and video posters absolute. This calls "makeAllSrcAttri butesAbsolute".
228 * @param rootNode The root Node to look through. 229 * @param rootNode The root Node to look through.
(...skipping 24 matching lines...) Expand all
253 } 254 }
254 255
255 private static void makeSrcSetAbsolute(Element root) { 256 private static void makeSrcSetAbsolute(Element root) {
256 NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG[SRCSET]"); 257 NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG[SRCSET]");
257 for (int i = 0; i < imgs.getLength(); i++) { 258 for (int i = 0; i < imgs.getLength(); i++) {
258 makeSrcSetAbsolute(ImageElement.as(imgs.getItem(i))); 259 makeSrcSetAbsolute(ImageElement.as(imgs.getItem(i)));
259 } 260 }
260 } 261 }
261 262
262 public static void makeSrcSetAbsolute(ImageElement ie) { 263 public static void makeSrcSetAbsolute(ImageElement ie) {
264 String srcset = ie.getAttribute("srcset");
265 if (srcset == "") return;
266
263 String oldsrc = ie.getSrc(); 267 String oldsrc = ie.getSrc();
264 String[] sizes = StringUtil.jsSplit(ie.getAttribute("srcset"), ","); 268 String[] sizes = StringUtil.jsSplit(srcset, ",");
265 for(int i = 0; i < sizes.length; i++) { 269 for(int i = 0; i < sizes.length; i++) {
266 String size = StringUtil.jsTrim(sizes[i]); 270 String size = StringUtil.jsTrim(sizes[i]);
267 if (size.isEmpty()) continue; 271 if (size.isEmpty()) continue;
268 String[] comp = size.split(" "); 272 String[] comp = size.split(" ");
269 ie.setSrc(comp[0]); 273 ie.setSrc(comp[0]);
270 comp[0] = ie.getSrc(); 274 comp[0] = ie.getSrc();
271 sizes[i] = StringUtil.join(comp, " "); 275 sizes[i] = StringUtil.join(comp, " ");
272 } 276 }
273 ie.setAttribute("srcset", StringUtil.join(sizes, ", ")); 277 ie.setAttribute("srcset", StringUtil.join(sizes, ", "));
274 ie.setSrc(oldsrc); 278 ie.setSrc(oldsrc);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 } 335 }
332 336
333 /** 337 /**
334 * Strips all "style" attributes from all nodes in the tree rooted at |rootN ode| 338 * Strips all "style" attributes from all nodes in the tree rooted at |rootN ode|
335 */ 339 */
336 public static void stripStyleAttributes(Node rootNode) { 340 public static void stripStyleAttributes(Node rootNode) {
337 stripAttributeFromTags(rootNode, "STYLE", new String[]{"*"}); 341 stripAttributeFromTags(rootNode, "STYLE", new String[]{"*"});
338 } 342 }
339 343
340 /** 344 /**
345 * Strips all "align" attributes from image elements in the tree rooted at | rootNode|
346 */
347 public static void stripAlignAttributes(Node rootNode) {
348 stripAttributeFromTags(rootNode, "ALIGN", new String[]{"IMG"});
349 }
350
351 /**
341 * Get a list of relevant nodes from a subtree. 352 * Get a list of relevant nodes from a subtree.
342 * @param root The root of the subtree. 353 * @param root The root of the subtree.
343 * @return A list of relevant nodes. 354 * @return A list of relevant nodes.
344 */ 355 */
345 public static List<Node> getOutputNodes(Node root) { 356 public static List<Node> getOutputNodes(Node root) {
346 final List<Node> nodes = new ArrayList<>(); 357 final List<Node> nodes = new ArrayList<>();
347 new DomWalker(new DomWalker.Visitor() { 358 new DomWalker(new DomWalker.Visitor() {
348 @Override 359 @Override
349 public boolean visit(Node n) { 360 public boolean visit(Node n) {
350 switch (n.getNodeType()) { 361 switch (n.getNodeType()) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 }-*/; 405 }-*/;
395 406
396 public static native Document createHTMLDocument(Document doc) /*-{ 407 public static native Document createHTMLDocument(Document doc) /*-{
397 return doc.implementation.createHTMLDocument(); 408 return doc.implementation.createHTMLDocument();
398 }-*/; 409 }-*/;
399 410
400 public static native Element getFirstElementChild(Document document) /*-{ 411 public static native Element getFirstElementChild(Document document) /*-{
401 return document.firstElementChild; 412 return document.firstElementChild;
402 }-*/; 413 }-*/;
403 } 414 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698