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