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