| 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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 stripImageElements(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 Loading... |
| 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 == "") { |
| 266 ie.removeAttribute("srcset"); |
| 267 return; |
| 268 } |
| 269 |
| 263 String oldsrc = ie.getSrc(); | 270 String oldsrc = ie.getSrc(); |
| 264 String[] sizes = StringUtil.jsSplit(ie.getAttribute("srcset"), ","); | 271 String[] sizes = StringUtil.jsSplit(srcset, ","); |
| 265 for(int i = 0; i < sizes.length; i++) { | 272 for(int i = 0; i < sizes.length; i++) { |
| 266 String size = StringUtil.jsTrim(sizes[i]); | 273 String size = StringUtil.jsTrim(sizes[i]); |
| 267 if (size.isEmpty()) continue; | 274 if (size.isEmpty()) continue; |
| 268 String[] comp = size.split(" "); | 275 String[] comp = size.split(" "); |
| 269 ie.setSrc(comp[0]); | 276 ie.setSrc(comp[0]); |
| 270 comp[0] = ie.getSrc(); | 277 comp[0] = ie.getSrc(); |
| 271 sizes[i] = StringUtil.join(comp, " "); | 278 sizes[i] = StringUtil.join(comp, " "); |
| 272 } | 279 } |
| 273 ie.setAttribute("srcset", StringUtil.join(sizes, ", ")); | 280 ie.setAttribute("srcset", StringUtil.join(sizes, ", ")); |
| 274 ie.setSrc(oldsrc); | 281 ie.setSrc(oldsrc); |
| 275 } | 282 } |
| 276 | 283 |
| 284 public static void stripImageElements(Node root) { |
| 285 NodeList<Element> imgs = DomUtil.querySelectorAll(root, "IMG"); |
| 286 for (int i = 0; i < imgs.getLength(); i++) { |
| 287 stripImageElement(ImageElement.as(imgs.getItem(i))); |
| 288 } |
| 289 } |
| 290 |
| 291 /** |
| 292 * Only keep some attributes for image elements. |
| 293 * @param ie The image element to strip in-place. |
| 294 */ |
| 295 public static void stripImageElement(ImageElement imgElement) { |
| 296 JsArray<Node> attrs = getAttributes(imgElement); |
| 297 for (int i = 0; i < attrs.length(); ) { |
| 298 String name = attrs.get(i).getNodeName(); |
| 299 if (!"src".equals(name) && |
| 300 !"alt".equals(name) && |
| 301 !"srcset".equals(name) && |
| 302 !"dir".equals(name) && |
| 303 !"title".equals(name)) { |
| 304 imgElement.removeAttribute(name); |
| 305 } else { |
| 306 i++; |
| 307 } |
| 308 } |
| 309 } |
| 310 |
| 277 /** | 311 /** |
| 278 * Makes all "img", "source", "track", and "video" tags have an absolute "sr
c" attribute. | 312 * Makes all "img", "source", "track", and "video" tags have an absolute "sr
c" attribute. |
| 279 * @param root The root element to look through. | 313 * @param root The root element to look through. |
| 280 */ | 314 */ |
| 281 public static native void makeAllSrcAttributesAbsolute(Element root) /*-{ | 315 public static native void makeAllSrcAttributesAbsolute(Element root) /*-{ |
| 282 var elementsWithSrc = root.querySelectorAll('img,source,track,video'); | 316 var elementsWithSrc = root.querySelectorAll('img,source,track,video'); |
| 283 for (var key in elementsWithSrc) { | 317 for (var key in elementsWithSrc) { |
| 284 if (elementsWithSrc[key].src) { | 318 if (elementsWithSrc[key].src) { |
| 285 elementsWithSrc[key].src = elementsWithSrc[key].src; | 319 elementsWithSrc[key].src = elementsWithSrc[key].src; |
| 286 } | 320 } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 }-*/; | 428 }-*/; |
| 395 | 429 |
| 396 public static native Document createHTMLDocument(Document doc) /*-{ | 430 public static native Document createHTMLDocument(Document doc) /*-{ |
| 397 return doc.implementation.createHTMLDocument(); | 431 return doc.implementation.createHTMLDocument(); |
| 398 }-*/; | 432 }-*/; |
| 399 | 433 |
| 400 public static native Element getFirstElementChild(Document document) /*-{ | 434 public static native Element getFirstElementChild(Document document) /*-{ |
| 401 return document.firstElementChild; | 435 return document.firstElementChild; |
| 402 }-*/; | 436 }-*/; |
| 403 } | 437 } |
| OLD | NEW |