Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Unified Diff: java/org/chromium/distiller/DomUtil.java

Issue 2401853004: Strip unwanted classNames from all nodes (Closed)
Patch Set: address comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | java/org/chromium/distiller/webdocument/WebText.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: java/org/chromium/distiller/DomUtil.java
diff --git a/java/org/chromium/distiller/DomUtil.java b/java/org/chromium/distiller/DomUtil.java
index 98d95f5e87d7fffaf63622b46c06501c8983af6a..79ed52ff4d6ea72756610464861dd9dc9512dc6e 100644
--- a/java/org/chromium/distiller/DomUtil.java
+++ b/java/org/chromium/distiller/DomUtil.java
@@ -418,6 +418,7 @@ public class DomUtil {
/**
* Strips some attribute from certain tags in the tree rooted at |rootNode|, including root.
* @param tagNames The tag names to be processed. ["*"] means all.
+ * TODO(crbug.com/654108): We should convert to whitelisting for all the tags.
*/
@SuppressWarnings("unused")
public static void stripAttributeFromTags(Node rootNode, String attribute, String[] tagNames) {
@@ -474,6 +475,32 @@ public class DomUtil {
}
/**
+ * Strips unwanted classNames from all nodes in the tree rooted at |root|.
+ * TODO(crbug.com/654109): "caption" is essential for styling, but all classNames should
+ * be removed eventually.
+ */
+ public static void stripUnwantedClassNames(Node root) {
+ if (root.getNodeType() == Node.ELEMENT_NODE) {
+ Element element = Element.as(root);
+ if (element.hasAttribute("class")) {
+ stripUnwantedClassName(element);
+ }
+ }
+ NodeList<Element> elems = DomUtil.querySelectorAll(root, "[class]");
+ for (int i = 0; i < elems.getLength(); i++) {
+ stripUnwantedClassName(elems.getItem(i));
+ }
+ }
+
+ private static void stripUnwantedClassName(Element elem) {
+ if (elem.getClassName().contains("caption")) {
+ elem.setClassName("caption");
+ } else {
+ elem.removeAttribute("class");
+ }
+ }
+
+ /**
* Get a list of relevant nodes from a subtree.
* @param root The root of the subtree.
* @return A list of relevant nodes.
« no previous file with comments | « no previous file | java/org/chromium/distiller/webdocument/WebText.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698