Chromium Code Reviews| Index: java/org/chromium/distiller/webdocument/filters/NestedElementRetainer.java |
| diff --git a/java/org/chromium/distiller/webdocument/filters/NestedElementRetainer.java b/java/org/chromium/distiller/webdocument/filters/NestedElementRetainer.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..293c766577c87df6ebcf5dc58ee710ef59193f7a |
| --- /dev/null |
| +++ b/java/org/chromium/distiller/webdocument/filters/NestedElementRetainer.java |
| @@ -0,0 +1,40 @@ |
| +package org.chromium.distiller.webdocument.filters; |
| + |
| +import org.chromium.distiller.webdocument.WebDocument; |
| +import org.chromium.distiller.webdocument.WebElement; |
| +import org.chromium.distiller.webdocument.WebTag; |
| + |
| +import java.util.Stack; |
| + |
| +public class NestedElementRetainer { |
|
mdjones
2015/08/05 15:44:35
Add some documentation about what this class is an
|
| + public static void process(WebDocument document) { |
| + boolean isContent = false; |
| + int stackMark = -1; |
| + Stack<WebTag> stack = new Stack<>(); |
| + |
| + for (WebElement e : document.getElements()) { |
| + if (!(e instanceof WebTag)) { |
| + if (!isContent) { |
| + isContent = e.getIsContent(); |
| + } |
| + } else { |
| + WebTag webTag = (WebTag) e; |
| + if (webTag.isStartTag()) { |
| + webTag.setIsContent(isContent); |
| + stack.push(webTag); |
| + isContent = false; |
| + } else { |
| + WebTag startWebTag = stack.pop(); |
| + isContent |= stackMark >= stack.size(); |
| + if (isContent) { |
| + stackMark = stack.size() - 1; |
| + } |
| + boolean wasContent = startWebTag.getIsContent(); |
| + startWebTag.setIsContent(isContent); |
| + webTag.setIsContent(isContent); |
| + isContent = wasContent; |
| + } |
| + } |
| + } |
| + } |
| +} |