Chromium Code Reviews| Index: src/com/dom_distiller/client/FilteringDomVisitor.java |
| diff --git a/src/com/dom_distiller/client/FilteringDomVisitor.java b/src/com/dom_distiller/client/FilteringDomVisitor.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1c7752c03081ccbf984be53c2ab2c43eb202f409 |
| --- /dev/null |
| +++ b/src/com/dom_distiller/client/FilteringDomVisitor.java |
| @@ -0,0 +1,61 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package com.dom_distiller.client; |
| + |
| +import com.google.gwt.dom.client.Element; |
| +import com.google.gwt.dom.client.Node; |
| +import com.google.gwt.dom.client.Style; |
| + |
| +import java.util.logging.Logger; |
| + |
| +/** |
| + * Filters out elements that are to be ignored from the DOM tree, and passes other nodes and |
| + * elements on to the actual DOM visitor for processing. |
| + */ |
| +public class FilteringDomVisitor implements DomWalker.Visitor { |
| + // To log debug information about the filtering of processed elements, set this flag to true. |
| + private static final boolean DEBUG = false; |
| + |
| + private static Logger logger = Logger.getLogger("FilteringDomParser"); |
| + private final DomWalker.Visitor domVisitor; |
| + |
| + FilteringDomVisitor(DomWalker.Visitor v) { |
| + domVisitor = v; |
| + } |
| + |
| + @Override |
| + public boolean visit(Node n) { |
| + if (n.getNodeType() == Node.ELEMENT_NODE) { |
| + // Skip invisible elements. |
| + Element e = Element.as(n); |
| + boolean visible = true; |
| + Style style = null; |
| + if (e.hasAttribute("hidden")) { |
| + visible = false; |
| + } else { |
| + style = DomUtil.getComputedStyle(e); |
| + if (style.getDisplay().equals("none") || style.getVisibility().equals("hidden")) { |
|
cjhopman
2014/05/13 16:20:53
how about:
visible = !(style.getDisplay().equals("
kuan
2014/05/13 16:55:43
Done.
|
| + visible = false; |
| + } |
| + } |
| + logDbgInfo(e, visible, style); |
| + if (!visible) return false; |
| + } |
| + return domVisitor.visit(n); |
| + } |
| + |
| + @Override |
| + public void exit(Node n) { |
| + domVisitor.exit(n); |
| + } |
| + |
| + private void logDbgInfo(Element e, boolean visible, Style style) { |
| + if (!DEBUG) return; |
| + LogUtil.logToConsole((visible ? "KEEP " : "SKIP ") + e.getTagName() + ": id=" + e.getId() + |
| + ", hiddenAttr=" + (e.hasAttribute("hidden") ? "yes" : "no") + |
| + (style == null ? "" : |
| + (", dsp=" + style.getDisplay() + ", vis=" + style.getVisibility()))); |
| + } |
| +} |