1 /*
2 * Copyright (c) 2002-2025 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.javascript.host.dom;
16
17 import org.htmlunit.html.DomNode;
18 import org.htmlunit.html.DomNodeIterator;
19 import org.htmlunit.javascript.HtmlUnitScriptable;
20 import org.htmlunit.javascript.configuration.JsxClass;
21 import org.htmlunit.javascript.configuration.JsxConstructor;
22 import org.htmlunit.javascript.configuration.JsxFunction;
23 import org.htmlunit.javascript.configuration.JsxGetter;
24
25 /**
26 * A JavaScript object for {@code NodeIterator}.
27 *
28 * @author Ahmed Ashour
29 * @author Ronald Brill
30 */
31 @JsxClass
32 public class NodeIterator extends HtmlUnitScriptable {
33
34 private DomNodeIterator iterator_;
35
36 /**
37 * Creates an instance.
38 */
39 public NodeIterator() {
40 super();
41 }
42
43 /**
44 * JavaScript constructor.
45 */
46 @JsxConstructor
47 public void jsConstructor() {
48 // nothing to do
49 }
50
51 /**
52 * Creates a new instance.
53 *
54 * @param root The root node at which to begin the {@link NodeIterator}'s traversal
55 * @param whatToShow an optional long representing a bitmask created by combining
56 * the constant properties of {@link NodeFilter}
57 * @param filter an object implementing the {@link NodeFilter} interface
58 */
59 public NodeIterator(final Node root, final int whatToShow,
60 final org.w3c.dom.traversal.NodeFilter filter) {
61 super();
62 iterator_ = new DomNodeIterator(root.getDomNodeOrDie(), whatToShow, filter, true);
63 }
64
65 /**
66 * Returns the root node.
67 * @return the root node
68 */
69 @JsxGetter
70 public Node getRoot() {
71 return getNodeOrNull(iterator_.getRoot());
72 }
73
74 private static Node getNodeOrNull(final DomNode domNode) {
75 if (domNode == null) {
76 return null;
77 }
78 return domNode.getScriptableObject();
79 }
80
81 /**
82 * Returns the types of nodes being presented.
83 * @return combined bitmask of {@link NodeFilter}
84 */
85 public long getWhatToShow() {
86 if (iterator_.getWhatToShow() == NodeFilter.SHOW_ALL) {
87 return 0xFFFFFFFFL;
88 }
89 return iterator_.getWhatToShow();
90 }
91
92 /**
93 * Returns the filter.
94 * @return the filter
95 */
96 @JsxGetter
97 public Object getFilter() {
98 //TODO: we should return the original filter
99 return iterator_.getFilter();
100 }
101
102 /**
103 * This operation is a no-op.
104 */
105 @JsxFunction
106 public void detach() {
107 iterator_.detach();
108 }
109
110 /**
111 * Returns the next Node in the document, or null if there are none.
112 * @return the next node
113 */
114 @JsxFunction
115 public Node nextNode() {
116 return getNodeOrNull(iterator_.nextNode());
117 }
118
119 /**
120 * Returns the previous Node in the document, or null if there are none.
121 * @return the previous node
122 */
123 @JsxFunction
124 public Node previousNode() {
125 return getNodeOrNull(iterator_.previousNode());
126 }
127 }