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

Side by Side Diff: java/org/chromium/distiller/DomUtil.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
« no previous file with comments | « no previous file | java/org/chromium/distiller/extractors/embeds/ImageExtractor.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 21 matching lines...) Expand all
32 public static native JsArray<Node> getAttributes(Element elem) /*-{ 32 public static native JsArray<Node> getAttributes(Element elem) /*-{
33 return elem.attributes; 33 return elem.attributes;
34 }-*/; 34 }-*/;
35 35
36 // Returns the first element with |className| in the tree rooted at |root|, null if none is 36 // Returns the first element with |className| in the tree rooted at |root|, null if none is
37 // found. 37 // found.
38 public static native Element getFirstElementWithClassName(Element root, Stri ng className) /*-{ 38 public static native Element getFirstElementWithClassName(Element root, Stri ng className) /*-{
39 return root.querySelector("." + className); 39 return root.querySelector("." + className);
40 }-*/; 40 }-*/;
41 41
42 // Returns the first element with |tagName| in the tree rooted at |root|, in cluding root.
43 // null if none is found.
44 public static Element getFirstElementByTagNameInc(Element e, String tagName) {
45 if (e.getTagName() == tagName) {
46 return e;
47 }
48 return getFirstElementByTagName(e, tagName);
49 }
50
51 // Returns the first element with |tagName| in the tree rooted at |root|.
52 // null if none is found.
53 public static Element getFirstElementByTagName(Element e, String tagName) {
54 NodeList<Element> elements = e.getElementsByTagName(tagName);
55 if (elements.getLength() > 0) {
56 return elements.getItem(0);
57 }
58 return null;
59 }
60
42 public static native boolean hasClassName(Element elem, String className) /* -{ 61 public static native boolean hasClassName(Element elem, String className) /* -{
43 return elem.classList.contains(className); 62 return elem.classList.contains(className);
44 }-*/; 63 }-*/;
45 64
46 public static native JsArrayString getClassList(Element elem) /*-{ 65 public static native JsArrayString getClassList(Element elem) /*-{
47 return elem.classList; 66 return elem.classList;
48 }-*/; 67 }-*/;
49 68
50 /** 69 /**
51 * Check to see if a provided URL has the specified root domain (ex. http:// a.b.c/foo/bar has 70 * Check to see if a provided URL has the specified root domain (ex. http:// a.b.c/foo/bar has
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 stripTargetAttributes(clonedSubtree); 285 stripTargetAttributes(clonedSubtree);
267 stripFontColorAttributes(clonedSubtree); 286 stripFontColorAttributes(clonedSubtree);
268 stripTableBackgroundColorAttributes(clonedSubtree); 287 stripTableBackgroundColorAttributes(clonedSubtree);
269 stripStyleAttributes(clonedSubtree); 288 stripStyleAttributes(clonedSubtree);
270 stripImageElements(clonedSubtree); 289 stripImageElements(clonedSubtree);
271 290
272 return (Element) clonedSubtree; 291 return (Element) clonedSubtree;
273 } 292 }
274 293
275 /** 294 /**
276 * Makes all anchors and video posters absolute. This calls "makeAllSrcAttri butesAbsolute". 295 * Makes all anchors and video posters absolute. This calls "makeAllSrcAttri butesAbsolute"
296 * and "makeAllSrcSetAbsolute".
277 * @param rootNode The root Node to look through. 297 * @param rootNode The root Node to look through.
278 */ 298 */
279 public static void makeAllLinksAbsolute(Node rootNode) { 299 public static void makeAllLinksAbsolute(Node rootNode) {
280 Element root = Element.as(rootNode); 300 Element root = Element.as(rootNode);
281 301
282 // AnchorElement.getHref() and ImageElement.getSrc() both return the 302 // AnchorElement.getHref() and ImageElement.getSrc() both return the
283 // absolute URI, so simply set them as the respective attributes. 303 // absolute URI, so simply set them as the respective attributes.
284 304
285 if ("A".equals(root.getTagName())) { 305 if ("A".equals(root.getTagName())) {
286 AnchorElement link = AnchorElement.as(root); 306 AnchorElement link = AnchorElement.as(root);
(...skipping 16 matching lines...) Expand all
303 } 323 }
304 NodeList<Element> videoTags = root.getElementsByTagName("VIDEO"); 324 NodeList<Element> videoTags = root.getElementsByTagName("VIDEO");
305 for (int i = 0; i < videoTags.getLength(); i++) { 325 for (int i = 0; i < videoTags.getLength(); i++) {
306 VideoElement video = (VideoElement) videoTags.getItem(i); 326 VideoElement video = (VideoElement) videoTags.getItem(i);
307 if (!video.getPoster().isEmpty()) { 327 if (!video.getPoster().isEmpty()) {
308 video.setPoster(video.getPoster()); 328 video.setPoster(video.getPoster());
309 } 329 }
310 } 330 }
311 makeAllSrcAttributesAbsolute(root); 331 makeAllSrcAttributesAbsolute(root);
312 332
313 makeSrcSetAbsolute(root); 333 makeAllSrcSetAbsolute(root);
314 } 334 }
315 335
316 private static void makeSrcSetAbsolute(Element root) { 336 public static void makeAllSrcSetAbsolute(Element root) {
317 if (root.getTagName().equals("IMG")) { 337 if (root.hasAttribute("srcset")) {
318 makeSrcSetAbsolute(ImageElement.as(root)); 338 makeSrcSetAbsolute(root);
319 } 339 }
320 NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG[SRCSET]"); 340 NodeList<Element> es = DomUtil.querySelectorAll(root, "[SRCSET]");
321 for (int i = 0; i < imgs.getLength(); i++) { 341 for (int i = 0; i < es.getLength(); i++) {
322 makeSrcSetAbsolute(ImageElement.as(imgs.getItem(i))); 342 makeSrcSetAbsolute(es.getItem(i));
323 } 343 }
324 } 344 }
325 345
326 public static void makeSrcSetAbsolute(ImageElement ie) { 346 private static void makeSrcSetAbsolute(Element ie) {
327 String srcset = ie.getAttribute("srcset"); 347 String srcset = ie.getAttribute("srcset");
328 if (srcset.isEmpty()) { 348 if (srcset.isEmpty()) {
329 ie.removeAttribute("srcset"); 349 ie.removeAttribute("srcset");
330 return; 350 return;
331 } 351 }
332 352
333 String oldsrc = ie.getSrc(); 353 ImageElement holder = Document.get().createImageElement();
334 String[] sizes = StringUtil.jsSplit(srcset, ","); 354 String[] sizes = StringUtil.jsSplit(srcset, ",");
335 for(int i = 0; i < sizes.length; i++) { 355 for(int i = 0; i < sizes.length; i++) {
336 String size = StringUtil.jsTrim(sizes[i]); 356 String size = StringUtil.jsTrim(sizes[i]);
337 if (size.isEmpty()) continue; 357 if (size.isEmpty()) continue;
338 String[] comp = size.split(" "); 358 String[] comp = size.split(" ");
339 ie.setSrc(comp[0]); 359 holder.setSrc(comp[0]);
340 comp[0] = ie.getSrc(); 360 comp[0] = holder.getSrc();
341 sizes[i] = StringUtil.join(comp, " "); 361 sizes[i] = StringUtil.join(comp, " ");
342 } 362 }
343 ie.setAttribute("srcset", StringUtil.join(sizes, ", ")); 363 ie.setAttribute("srcset", StringUtil.join(sizes, ", "));
344 ie.setSrc(oldsrc);
345 } 364 }
346 365
347 public static List<String> getSrcSetUrls(ImageElement ie) { 366 public static List<String> getAllSrcSetUrls(Element root) {
348 List<String> list = new ArrayList<>(); 367 List<String> list = new ArrayList<>();
349 String srcset = ie.getAttribute("srcset"); 368 if (root.hasAttribute("srcset")) {
369 list.addAll(getSrcSetUrls(root));
370 }
371 NodeList<Element> es = DomUtil.querySelectorAll(root, "[SRCSET]");
372 for (int i = 0; i < es.getLength(); i++) {
373 list.addAll(getSrcSetUrls(es.getItem(i)));
374 }
375 return list;
376 }
377
378 public static List<String> getSrcSetUrls(Element e) {
379 List<String> list = new ArrayList<>();
380 String srcset = e.getAttribute("srcset");
350 if (srcset.isEmpty()) { 381 if (srcset.isEmpty()) {
351 return list; 382 return list;
352 } 383 }
353 384
354 String[] sizes = StringUtil.jsSplit(srcset, ","); 385 String[] sizes = StringUtil.jsSplit(srcset, ",");
355 for(int i = 0; i < sizes.length; i++) { 386 for(int i = 0; i < sizes.length; i++) {
356 String size = StringUtil.jsTrim(sizes[i]); 387 String size = StringUtil.jsTrim(sizes[i]);
357 if (size.isEmpty()) continue; 388 if (size.isEmpty()) continue;
358 String[] comp = size.split(" "); 389 String[] comp = size.split(" ");
359 list.add(comp[0]); 390 list.add(comp[0]);
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 }-*/; 594 }-*/;
564 595
565 public static native Document createHTMLDocument(Document doc) /*-{ 596 public static native Document createHTMLDocument(Document doc) /*-{
566 return doc.implementation.createHTMLDocument(); 597 return doc.implementation.createHTMLDocument();
567 }-*/; 598 }-*/;
568 599
569 public static native Element getFirstElementChild(Document document) /*-{ 600 public static native Element getFirstElementChild(Document document) /*-{
570 return document.firstElementChild; 601 return document.firstElementChild;
571 }-*/; 602 }-*/;
572 } 603 }
OLDNEW
« no previous file with comments | « no previous file | java/org/chromium/distiller/extractors/embeds/ImageExtractor.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698