| OLD | NEW |
| 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 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 stripTargetAttributes(clonedSubtree); | 266 stripTargetAttributes(clonedSubtree); |
| 267 stripFontColorAttributes(clonedSubtree); | 267 stripFontColorAttributes(clonedSubtree); |
| 268 stripTableBackgroundColorAttributes(clonedSubtree); | 268 stripTableBackgroundColorAttributes(clonedSubtree); |
| 269 stripStyleAttributes(clonedSubtree); | 269 stripStyleAttributes(clonedSubtree); |
| 270 stripImageElements(clonedSubtree); | 270 stripImageElements(clonedSubtree); |
| 271 | 271 |
| 272 return (Element) clonedSubtree; | 272 return (Element) clonedSubtree; |
| 273 } | 273 } |
| 274 | 274 |
| 275 /** | 275 /** |
| 276 * Makes all anchors and video posters absolute. This calls "makeAllSrcAttri
butesAbsolute". | 276 * Makes all anchors and video posters absolute. This calls "makeAllSrcAttri
butesAbsolute" |
| 277 * and "makeAllSrcSetAbsolute". |
| 277 * @param rootNode The root Node to look through. | 278 * @param rootNode The root Node to look through. |
| 278 */ | 279 */ |
| 279 public static void makeAllLinksAbsolute(Node rootNode) { | 280 public static void makeAllLinksAbsolute(Node rootNode) { |
| 280 Element root = Element.as(rootNode); | 281 Element root = Element.as(rootNode); |
| 281 | 282 |
| 282 // AnchorElement.getHref() and ImageElement.getSrc() both return the | 283 // AnchorElement.getHref() and ImageElement.getSrc() both return the |
| 283 // absolute URI, so simply set them as the respective attributes. | 284 // absolute URI, so simply set them as the respective attributes. |
| 284 | 285 |
| 285 if ("A".equals(root.getTagName())) { | 286 if ("A".equals(root.getTagName())) { |
| 286 AnchorElement link = AnchorElement.as(root); | 287 AnchorElement link = AnchorElement.as(root); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 303 } | 304 } |
| 304 NodeList<Element> videoTags = root.getElementsByTagName("VIDEO"); | 305 NodeList<Element> videoTags = root.getElementsByTagName("VIDEO"); |
| 305 for (int i = 0; i < videoTags.getLength(); i++) { | 306 for (int i = 0; i < videoTags.getLength(); i++) { |
| 306 VideoElement video = (VideoElement) videoTags.getItem(i); | 307 VideoElement video = (VideoElement) videoTags.getItem(i); |
| 307 if (!video.getPoster().isEmpty()) { | 308 if (!video.getPoster().isEmpty()) { |
| 308 video.setPoster(video.getPoster()); | 309 video.setPoster(video.getPoster()); |
| 309 } | 310 } |
| 310 } | 311 } |
| 311 makeAllSrcAttributesAbsolute(root); | 312 makeAllSrcAttributesAbsolute(root); |
| 312 | 313 |
| 313 makeSrcSetAbsolute(root); | 314 makeAllSrcSetAbsolute(root); |
| 314 } | 315 } |
| 315 | 316 |
| 316 private static void makeSrcSetAbsolute(Element root) { | 317 public static void makeAllSrcSetAbsolute(Element root) { |
| 317 if (root.getTagName().equals("IMG")) { | 318 if (root.hasAttribute("srcset")) { |
| 318 makeSrcSetAbsolute(ImageElement.as(root)); | 319 makeSrcSetAbsolute(root); |
| 319 } | 320 } |
| 320 NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG[SRCSET]"); | 321 NodeList<Element> es = DomUtil.querySelectorAll(root, "[SRCSET]"); |
| 321 for (int i = 0; i < imgs.getLength(); i++) { | 322 for (int i = 0; i < es.getLength(); i++) { |
| 322 makeSrcSetAbsolute(ImageElement.as(imgs.getItem(i))); | 323 makeSrcSetAbsolute(es.getItem(i)); |
| 323 } | 324 } |
| 324 } | 325 } |
| 325 | 326 |
| 326 public static void makeSrcSetAbsolute(ImageElement ie) { | 327 private static void makeSrcSetAbsolute(Element ie) { |
| 327 String srcset = ie.getAttribute("srcset"); | 328 String srcset = ie.getAttribute("srcset"); |
| 328 if (srcset.isEmpty()) { | 329 if (srcset.isEmpty()) { |
| 329 ie.removeAttribute("srcset"); | 330 ie.removeAttribute("srcset"); |
| 330 return; | 331 return; |
| 331 } | 332 } |
| 332 | 333 |
| 333 String oldsrc = ie.getSrc(); | 334 ImageElement holder = Document.get().createImageElement(); |
| 334 String[] sizes = StringUtil.jsSplit(srcset, ","); | 335 String[] sizes = StringUtil.jsSplit(srcset, ","); |
| 335 for(int i = 0; i < sizes.length; i++) { | 336 for(int i = 0; i < sizes.length; i++) { |
| 336 String size = StringUtil.jsTrim(sizes[i]); | 337 String size = StringUtil.jsTrim(sizes[i]); |
| 337 if (size.isEmpty()) continue; | 338 if (size.isEmpty()) continue; |
| 338 String[] comp = size.split(" "); | 339 String[] comp = size.split(" "); |
| 339 ie.setSrc(comp[0]); | 340 holder.setSrc(comp[0]); |
| 340 comp[0] = ie.getSrc(); | 341 comp[0] = holder.getSrc(); |
| 341 sizes[i] = StringUtil.join(comp, " "); | 342 sizes[i] = StringUtil.join(comp, " "); |
| 342 } | 343 } |
| 343 ie.setAttribute("srcset", StringUtil.join(sizes, ", ")); | 344 ie.setAttribute("srcset", StringUtil.join(sizes, ", ")); |
| 344 ie.setSrc(oldsrc); | |
| 345 } | 345 } |
| 346 | 346 |
| 347 public static List<String> getSrcSetUrls(ImageElement ie) { | 347 public static List<String> getAllSrcSetUrls(Element root) { |
| 348 List<String> list = new ArrayList<>(); | 348 List<String> list = new ArrayList<>(); |
| 349 String srcset = ie.getAttribute("srcset"); | 349 if (root.hasAttribute("srcset")) { |
| 350 list.addAll(getSrcSetUrls(root)); |
| 351 } |
| 352 NodeList<Element> es = DomUtil.querySelectorAll(root, "[SRCSET]"); |
| 353 for (int i = 0; i < es.getLength(); i++) { |
| 354 list.addAll(getSrcSetUrls(es.getItem(i))); |
| 355 } |
| 356 return list; |
| 357 } |
| 358 |
| 359 public static List<String> getSrcSetUrls(Element e) { |
| 360 List<String> list = new ArrayList<>(); |
| 361 String srcset = e.getAttribute("srcset"); |
| 350 if (srcset.isEmpty()) { | 362 if (srcset.isEmpty()) { |
| 351 return list; | 363 return list; |
| 352 } | 364 } |
| 353 | 365 |
| 354 String[] sizes = StringUtil.jsSplit(srcset, ","); | 366 String[] sizes = StringUtil.jsSplit(srcset, ","); |
| 355 for(int i = 0; i < sizes.length; i++) { | 367 for(int i = 0; i < sizes.length; i++) { |
| 356 String size = StringUtil.jsTrim(sizes[i]); | 368 String size = StringUtil.jsTrim(sizes[i]); |
| 357 if (size.isEmpty()) continue; | 369 if (size.isEmpty()) continue; |
| 358 String[] comp = size.split(" "); | 370 String[] comp = size.split(" "); |
| 359 list.add(comp[0]); | 371 list.add(comp[0]); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 }-*/; | 575 }-*/; |
| 564 | 576 |
| 565 public static native Document createHTMLDocument(Document doc) /*-{ | 577 public static native Document createHTMLDocument(Document doc) /*-{ |
| 566 return doc.implementation.createHTMLDocument(); | 578 return doc.implementation.createHTMLDocument(); |
| 567 }-*/; | 579 }-*/; |
| 568 | 580 |
| 569 public static native Element getFirstElementChild(Document document) /*-{ | 581 public static native Element getFirstElementChild(Document document) /*-{ |
| 570 return document.firstElementChild; | 582 return document.firstElementChild; |
| 571 }-*/; | 583 }-*/; |
| 572 } | 584 } |
| OLD | NEW |