1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.dom;
16
17 import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
18 import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
19 import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
20
21 import org.htmlunit.corejs.javascript.Context;
22 import org.htmlunit.corejs.javascript.Function;
23 import org.htmlunit.corejs.javascript.Scriptable;
24 import org.htmlunit.corejs.javascript.VarScope;
25 import org.htmlunit.cssparser.parser.CSSException;
26 import org.htmlunit.html.DomDocumentFragment;
27 import org.htmlunit.html.DomNode;
28 import org.htmlunit.html.HtmlPage;
29 import org.htmlunit.javascript.HtmlUnitScriptable;
30 import org.htmlunit.javascript.JavaScriptEngine;
31 import org.htmlunit.javascript.configuration.JsxClass;
32 import org.htmlunit.javascript.configuration.JsxConstructor;
33 import org.htmlunit.javascript.configuration.JsxFunction;
34 import org.htmlunit.javascript.configuration.JsxGetter;
35 import org.htmlunit.javascript.host.Element;
36 import org.htmlunit.javascript.host.html.HTMLCollection;
37
38
39
40
41
42
43
44
45
46
47
48 @JsxClass(domClass = DomDocumentFragment.class)
49 public class DocumentFragment extends Node {
50
51
52
53
54 @Override
55 @JsxConstructor
56 public void jsConstructor() {
57 super.jsConstructor();
58
59 final HtmlPage page = (HtmlPage) getWindow().getWebWindow().getEnclosedPage();
60 final DomDocumentFragment fragment = new DomDocumentFragment(page);
61 setDomNode(fragment);
62 }
63
64
65
66
67
68
69
70
71
72
73 @JsxFunction
74 public static void append(final Context context, final VarScope scope,
75 final Scriptable thisObj, final Object[] args, final Function function) {
76 Node.append(context, thisObj, args, function);
77 }
78
79
80
81
82
83
84
85
86
87
88 @JsxFunction
89 public static void prepend(final Context context, final VarScope scope,
90 final Scriptable thisObj, final Object[] args, final Function function) {
91 Node.prepend(context, thisObj, args, function);
92 }
93
94
95
96
97
98
99
100
101
102
103 @JsxFunction
104 public static void replaceChildren(final Context context, final VarScope scope,
105 final Scriptable thisObj, final Object[] args, final Function function) {
106 Node.replaceChildren(context, thisObj, args, function);
107 }
108
109
110
111
112
113
114
115
116
117
118 @JsxFunction({CHROME, EDGE, FF})
119 public static void moveBefore(final Context context, final VarScope scope,
120 final Scriptable thisObj, final Object[] args, final Function function) {
121 Node.moveBefore(context, scope, thisObj, args, function);
122 }
123
124
125
126
127
128
129
130
131 @JsxFunction
132 public NodeList querySelectorAll(final String selectors) {
133 try {
134 return NodeList.staticNodeList(getParentScope(), getDomNodeOrDie().querySelectorAll(selectors));
135 }
136 catch (final CSSException e) {
137 throw JavaScriptEngine.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
138 + selectors + "' error: " + e.getMessage() + ").");
139 }
140 }
141
142
143
144
145
146
147 @JsxFunction
148 public Node querySelector(final String selectors) {
149 try {
150 final DomNode node = getDomNodeOrDie().querySelector(selectors);
151 if (node != null) {
152 return node.getScriptableObject();
153 }
154 return null;
155 }
156 catch (final CSSException e) {
157 throw JavaScriptEngine.reportRuntimeError("An invalid or illegal selector was specified (selector: '"
158 + selectors + "' error: " + e.getMessage() + ").");
159 }
160 }
161
162
163
164
165 @Override
166 public Object getDefaultValue(final Class<?> hint) {
167 if (String.class.equals(hint) || hint == null) {
168 return "[object " + getClassName() + "]";
169 }
170 return super.getDefaultValue(hint);
171 }
172
173
174
175
176 @Override
177 @JsxGetter
178 public int getChildElementCount() {
179 return super.getChildElementCount();
180 }
181
182
183
184
185 @Override
186 @JsxGetter
187 public Element getFirstElementChild() {
188 return super.getFirstElementChild();
189 }
190
191
192
193
194 @Override
195 @JsxGetter
196 public Element getLastElementChild() {
197 return super.getLastElementChild();
198 }
199
200
201
202
203 @Override
204 @JsxGetter
205 public HTMLCollection getChildren() {
206 return super.getChildren();
207 }
208
209
210
211
212
213
214 @JsxFunction
215 public HtmlUnitScriptable getElementById(final Object id) {
216 if (id == null || JavaScriptEngine.isUndefined(id)) {
217 return null;
218 }
219 final String idString = JavaScriptEngine.toString(id);
220 if (idString == null || idString.length() == 0) {
221 return null;
222 }
223 for (final DomNode child : getDomNodeOrDie().getChildren()) {
224 final Element elem = child.getScriptableObject();
225 if (idString.equals(elem.getId())) {
226 return elem;
227 }
228 }
229 return null;
230 }
231
232
233
234
235 @Override
236 public Node getRootNode() {
237 return this;
238 }
239 }