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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 public static native String javascriptTextContent(Node node) /*-{ | 182 public static native String javascriptTextContent(Node node) /*-{ |
183 return node.textContent; | 183 return node.textContent; |
184 }-*/; | 184 }-*/; |
185 | 185 |
186 /** | 186 /** |
187 * Get a list of all the parents of this node starting with the node itself. | 187 * Get a list of all the parents of this node starting with the node itself. |
188 * @param n The node to get the parents of. | 188 * @param n The node to get the parents of. |
189 * @return A list of the provided node's parents. | 189 * @return A list of the provided node's parents. |
190 */ | 190 */ |
191 public static List<Node> getParentNodes(Node n) { | 191 public static List<Node> getParentNodes(Node n) { |
192 ArrayList<Node> result = new ArrayList<Node>(); | 192 ArrayList<Node> result = new ArrayList<>(); |
193 Node curr = n; | 193 Node curr = n; |
194 while (curr != null) { | 194 while (curr != null) { |
195 result.add(curr); | 195 result.add(curr); |
196 curr = curr.getParentNode(); | 196 curr = curr.getParentNode(); |
197 } | 197 } |
198 return result; | 198 return result; |
199 } | 199 } |
200 | 200 |
201 /** | 201 /** |
202 * Get the depth of the given node in the DOM tree (only counting elements). | 202 * Get the depth of the given node in the DOM tree (only counting elements). |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 makeSrcSetAbsolute(ImageElement.as(root)); | 322 makeSrcSetAbsolute(ImageElement.as(root)); |
323 } | 323 } |
324 NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG[SRCSET]"); | 324 NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG[SRCSET]"); |
325 for (int i = 0; i < imgs.getLength(); i++) { | 325 for (int i = 0; i < imgs.getLength(); i++) { |
326 makeSrcSetAbsolute(ImageElement.as(imgs.getItem(i))); | 326 makeSrcSetAbsolute(ImageElement.as(imgs.getItem(i))); |
327 } | 327 } |
328 } | 328 } |
329 | 329 |
330 public static void makeSrcSetAbsolute(ImageElement ie) { | 330 public static void makeSrcSetAbsolute(ImageElement ie) { |
331 String srcset = ie.getAttribute("srcset"); | 331 String srcset = ie.getAttribute("srcset"); |
332 if (srcset == "") { | 332 if (srcset.isEmpty()) { |
333 ie.removeAttribute("srcset"); | 333 ie.removeAttribute("srcset"); |
334 return; | 334 return; |
335 } | 335 } |
336 | 336 |
337 String oldsrc = ie.getSrc(); | 337 String oldsrc = ie.getSrc(); |
338 String[] sizes = StringUtil.jsSplit(srcset, ","); | 338 String[] sizes = StringUtil.jsSplit(srcset, ","); |
339 for(int i = 0; i < sizes.length; i++) { | 339 for(int i = 0; i < sizes.length; i++) { |
340 String size = StringUtil.jsTrim(sizes[i]); | 340 String size = StringUtil.jsTrim(sizes[i]); |
341 if (size.isEmpty()) continue; | 341 if (size.isEmpty()) continue; |
342 String[] comp = size.split(" "); | 342 String[] comp = size.split(" "); |
343 ie.setSrc(comp[0]); | 343 ie.setSrc(comp[0]); |
344 comp[0] = ie.getSrc(); | 344 comp[0] = ie.getSrc(); |
345 sizes[i] = StringUtil.join(comp, " "); | 345 sizes[i] = StringUtil.join(comp, " "); |
346 } | 346 } |
347 ie.setAttribute("srcset", StringUtil.join(sizes, ", ")); | 347 ie.setAttribute("srcset", StringUtil.join(sizes, ", ")); |
348 ie.setSrc(oldsrc); | 348 ie.setSrc(oldsrc); |
349 } | 349 } |
350 | 350 |
| 351 public static List<String> getSrcSetUrls(ImageElement ie) { |
| 352 List<String> list = new ArrayList<>(); |
| 353 String srcset = ie.getAttribute("srcset"); |
| 354 if (srcset.isEmpty()) { |
| 355 return list; |
| 356 } |
| 357 |
| 358 String[] sizes = StringUtil.jsSplit(srcset, ","); |
| 359 for(int i = 0; i < sizes.length; i++) { |
| 360 String size = StringUtil.jsTrim(sizes[i]); |
| 361 if (size.isEmpty()) continue; |
| 362 String[] comp = size.split(" "); |
| 363 list.add(comp[0]); |
| 364 } |
| 365 return list; |
| 366 } |
| 367 |
351 public static void stripImageElements(Node root) { | 368 public static void stripImageElements(Node root) { |
352 if (root.getNodeType() == Node.ELEMENT_NODE) { | 369 if (root.getNodeType() == Node.ELEMENT_NODE) { |
353 Element element = Element.as(root); | 370 Element element = Element.as(root); |
354 if (element.getTagName().equals("IMG")) { | 371 if (element.getTagName().equals("IMG")) { |
355 stripImageElement(ImageElement.as(element)); | 372 stripImageElement(ImageElement.as(element)); |
356 } | 373 } |
357 } | 374 } |
358 NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG"); | 375 NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG"); |
359 for (int i = 0; i < imgs.getLength(); i++) { | 376 for (int i = 0; i < imgs.getLength(); i++) { |
360 stripImageElement(ImageElement.as(imgs.getItem(i))); | 377 stripImageElement(ImageElement.as(imgs.getItem(i))); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 }-*/; | 541 }-*/; |
525 | 542 |
526 public static native Document createHTMLDocument(Document doc) /*-{ | 543 public static native Document createHTMLDocument(Document doc) /*-{ |
527 return doc.implementation.createHTMLDocument(); | 544 return doc.implementation.createHTMLDocument(); |
528 }-*/; | 545 }-*/; |
529 | 546 |
530 public static native Element getFirstElementChild(Document document) /*-{ | 547 public static native Element getFirstElementChild(Document document) /*-{ |
531 return document.firstElementChild; | 548 return document.firstElementChild; |
532 }-*/; | 549 }-*/; |
533 } | 550 } |
OLD | NEW |