Chromium Code Reviews| 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 src, alt, and srcset 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(); i++) { | |
| 298 String name = attrs.get(i).getNodeName(); | |
| 299 if (name != "src" && name != "alt" && name != "srcset") { | |
|
mdjones
2015/12/10 17:05:22
Even though this gets converted to JavaScript, I'd
| |
| 300 imgElement.removeAttribute(name); | |
| 301 } | |
| 302 } | |
| 303 // Somehow getAttributes() doesn't include "style". | |
| 304 imgElement.removeAttribute("style"); | |
|
wychen
2015/12/10 05:39:16
Mysterious! Without this line, the tests would fai
mdjones
2015/12/10 17:05:22
I think "style" is a special property for elements
| |
| 305 } | |
| 306 | |
| 277 /** | 307 /** |
| 278 * Makes all "img", "source", "track", and "video" tags have an absolute "sr c" attribute. | 308 * Makes all "img", "source", "track", and "video" tags have an absolute "sr c" attribute. |
| 279 * @param root The root element to look through. | 309 * @param root The root element to look through. |
| 280 */ | 310 */ |
| 281 public static native void makeAllSrcAttributesAbsolute(Element root) /*-{ | 311 public static native void makeAllSrcAttributesAbsolute(Element root) /*-{ |
| 282 var elementsWithSrc = root.querySelectorAll('img,source,track,video'); | 312 var elementsWithSrc = root.querySelectorAll('img,source,track,video'); |
| 283 for (var key in elementsWithSrc) { | 313 for (var key in elementsWithSrc) { |
| 284 if (elementsWithSrc[key].src) { | 314 if (elementsWithSrc[key].src) { |
| 285 elementsWithSrc[key].src = elementsWithSrc[key].src; | 315 elementsWithSrc[key].src = elementsWithSrc[key].src; |
| 286 } | 316 } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 }-*/; | 424 }-*/; |
| 395 | 425 |
| 396 public static native Document createHTMLDocument(Document doc) /*-{ | 426 public static native Document createHTMLDocument(Document doc) /*-{ |
| 397 return doc.implementation.createHTMLDocument(); | 427 return doc.implementation.createHTMLDocument(); |
| 398 }-*/; | 428 }-*/; |
| 399 | 429 |
| 400 public static native Element getFirstElementChild(Document document) /*-{ | 430 public static native Element getFirstElementChild(Document document) /*-{ |
| 401 return document.firstElementChild; | 431 return document.firstElementChild; |
| 402 }-*/; | 432 }-*/; |
| 403 } | 433 } |
| OLD | NEW |