| 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 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 for (var key in elementsWithSrc) { | 411 for (var key in elementsWithSrc) { |
| 412 if (elementsWithSrc[key].src) { | 412 if (elementsWithSrc[key].src) { |
| 413 elementsWithSrc[key].src = elementsWithSrc[key].src; | 413 elementsWithSrc[key].src = elementsWithSrc[key].src; |
| 414 } | 414 } |
| 415 } | 415 } |
| 416 }-*/; | 416 }-*/; |
| 417 | 417 |
| 418 /** | 418 /** |
| 419 * Strips some attribute from certain tags in the tree rooted at |rootNode|,
including root. | 419 * Strips some attribute from certain tags in the tree rooted at |rootNode|,
including root. |
| 420 * @param tagNames The tag names to be processed. ["*"] means all. | 420 * @param tagNames The tag names to be processed. ["*"] means all. |
| 421 * TODO(crbug.com/654108): We should convert to whitelisting for all the tag
s. |
| 421 */ | 422 */ |
| 422 @SuppressWarnings("unused") | 423 @SuppressWarnings("unused") |
| 423 public static void stripAttributeFromTags(Node rootNode, String attribute, S
tring[] tagNames) { | 424 public static void stripAttributeFromTags(Node rootNode, String attribute, S
tring[] tagNames) { |
| 424 Element root = Element.as(rootNode); | 425 Element root = Element.as(rootNode); |
| 425 for (String tag: tagNames) { | 426 for (String tag: tagNames) { |
| 426 if (root.getTagName().equals(tag) || tag.equals("*")) { | 427 if (root.getTagName().equals(tag) || tag.equals("*")) { |
| 427 root.removeAttribute(attribute); | 428 root.removeAttribute(attribute); |
| 428 } | 429 } |
| 429 } | 430 } |
| 430 | 431 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 } | 468 } |
| 468 | 469 |
| 469 /** | 470 /** |
| 470 * Strips all "target" attributes from anchor nodes in the tree rooted at |r
ootNode| | 471 * Strips all "target" attributes from anchor nodes in the tree rooted at |r
ootNode| |
| 471 */ | 472 */ |
| 472 public static void stripTargetAttributes(Node rootNode) { | 473 public static void stripTargetAttributes(Node rootNode) { |
| 473 stripAttributeFromTags(rootNode, "TARGET", new String[]{"A"}); | 474 stripAttributeFromTags(rootNode, "TARGET", new String[]{"A"}); |
| 474 } | 475 } |
| 475 | 476 |
| 476 /** | 477 /** |
| 478 * Strips unwanted classNames from all nodes in the tree rooted at |root|. |
| 479 * TODO(crbug.com/654109): "caption" is essential for styling, but all class
Names should |
| 480 * be removed eventually. |
| 481 */ |
| 482 public static void stripUnwantedClassNames(Node root) { |
| 483 if (root.getNodeType() == Node.ELEMENT_NODE) { |
| 484 Element element = Element.as(root); |
| 485 if (element.hasAttribute("class")) { |
| 486 stripUnwantedClassName(element); |
| 487 } |
| 488 } |
| 489 NodeList<Element> elems = DomUtil.querySelectorAll(root, "[class]"); |
| 490 for (int i = 0; i < elems.getLength(); i++) { |
| 491 stripUnwantedClassName(elems.getItem(i)); |
| 492 } |
| 493 } |
| 494 |
| 495 private static void stripUnwantedClassName(Element elem) { |
| 496 if (elem.getClassName().contains("caption")) { |
| 497 elem.setClassName("caption"); |
| 498 } else { |
| 499 elem.removeAttribute("class"); |
| 500 } |
| 501 } |
| 502 |
| 503 /** |
| 477 * Get a list of relevant nodes from a subtree. | 504 * Get a list of relevant nodes from a subtree. |
| 478 * @param root The root of the subtree. | 505 * @param root The root of the subtree. |
| 479 * @return A list of relevant nodes. | 506 * @return A list of relevant nodes. |
| 480 */ | 507 */ |
| 481 public static List<Node> getOutputNodes(Node root) { | 508 public static List<Node> getOutputNodes(Node root) { |
| 482 final List<Node> nodes = new ArrayList<>(); | 509 final List<Node> nodes = new ArrayList<>(); |
| 483 new DomWalker(new DomWalker.Visitor() { | 510 new DomWalker(new DomWalker.Visitor() { |
| 484 @Override | 511 @Override |
| 485 public boolean visit(Node n) { | 512 public boolean visit(Node n) { |
| 486 switch (n.getNodeType()) { | 513 switch (n.getNodeType()) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 }-*/; | 563 }-*/; |
| 537 | 564 |
| 538 public static native Document createHTMLDocument(Document doc) /*-{ | 565 public static native Document createHTMLDocument(Document doc) /*-{ |
| 539 return doc.implementation.createHTMLDocument(); | 566 return doc.implementation.createHTMLDocument(); |
| 540 }-*/; | 567 }-*/; |
| 541 | 568 |
| 542 public static native Element getFirstElementChild(Document document) /*-{ | 569 public static native Element getFirstElementChild(Document document) /*-{ |
| 543 return document.firstElementChild; | 570 return document.firstElementChild; |
| 544 }-*/; | 571 }-*/; |
| 545 } | 572 } |
| OLD | NEW |