1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.html;
16
17 import java.io.Serializable;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.function.Supplier;
21
22 import org.htmlunit.html.DomNode;
23 import org.htmlunit.html.HtmlElement;
24 import org.htmlunit.html.HtmlTable;
25 import org.htmlunit.html.HtmlTableBody;
26 import org.htmlunit.html.HtmlTableFooter;
27 import org.htmlunit.html.HtmlTableHeader;
28 import org.htmlunit.html.HtmlTableRow;
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.configuration.JsxSetter;
36 import org.htmlunit.javascript.host.dom.Node;
37
38
39
40
41
42
43
44
45
46
47
48
49
50 @JsxClass(domClass = HtmlTable.class)
51 public class HTMLTableElement extends RowContainer {
52
53
54
55
56 @Override
57 @JsxConstructor
58 public void jsConstructor() {
59 super.jsConstructor();
60 }
61
62
63
64
65
66
67 @JsxGetter
68 public HtmlUnitScriptable getCaption() {
69 final List<HtmlElement> captions = getDomNodeOrDie().getStaticElementsByTagName("caption");
70 if (captions.isEmpty()) {
71 return null;
72 }
73 return getScriptableFor(captions.get(0));
74 }
75
76
77
78
79
80 @JsxSetter
81 public void setCaption(final Object o) {
82 if (!(o instanceof HTMLTableCaptionElement)) {
83 throw JavaScriptEngine.typeError("Not a caption");
84 }
85
86
87 deleteCaption();
88
89 final HTMLTableCaptionElement caption = (HTMLTableCaptionElement) o;
90 getDomNodeOrDie().appendChild(caption.getDomNodeOrDie());
91 }
92
93
94
95
96
97
98 @JsxGetter
99 public HtmlUnitScriptable getTFoot() {
100 final List<HtmlElement> tfoots = getDomNodeOrDie().getStaticElementsByTagName("tfoot");
101 if (tfoots.isEmpty()) {
102 return null;
103 }
104 return getScriptableFor(tfoots.get(0));
105 }
106
107
108
109
110
111 @JsxSetter
112 public void setTFoot(final Object o) {
113 if (!(o instanceof HTMLTableSectionElement
114 && "TFOOT".equals(((HTMLTableSectionElement) o).getTagName()))) {
115 throw JavaScriptEngine.typeError("Not a tFoot");
116 }
117
118
119 deleteTFoot();
120
121 final HTMLTableSectionElement tfoot = (HTMLTableSectionElement) o;
122 getDomNodeOrDie().appendChild(tfoot.getDomNodeOrDie());
123 }
124
125
126
127
128
129
130 @JsxGetter
131 public HtmlUnitScriptable getTHead() {
132 final List<HtmlElement> theads = getDomNodeOrDie().getStaticElementsByTagName("thead");
133 if (theads.isEmpty()) {
134 return null;
135 }
136 return getScriptableFor(theads.get(0));
137 }
138
139
140
141
142
143 @JsxSetter
144 public void setTHead(final Object o) {
145 if (!(o instanceof HTMLTableSectionElement
146 && "THEAD".equals(((HTMLTableSectionElement) o).getTagName()))) {
147 throw JavaScriptEngine.typeError("Not a tHead");
148 }
149
150
151 deleteTHead();
152
153 final HTMLTableSectionElement thead = (HTMLTableSectionElement) o;
154 getDomNodeOrDie().appendChild(thead.getDomNodeOrDie());
155 }
156
157
158
159
160
161 @JsxGetter
162 public HtmlUnitScriptable getTBodies() {
163 final HtmlTable table = (HtmlTable) getDomNodeOrDie();
164 final HTMLCollection bodies = new HTMLCollection(table, false);
165 bodies.setElementsSupplier((Supplier<List<DomNode>> & Serializable) () -> new ArrayList<>(table.getBodies()));
166 return bodies;
167 }
168
169
170
171
172
173
174
175
176 @JsxFunction
177 public HtmlUnitScriptable createCaption() {
178 return getScriptableFor(getDomNodeOrDie().appendChildIfNoneExists("caption"));
179 }
180
181
182
183
184
185
186
187
188 @JsxFunction
189 public HtmlUnitScriptable createTFoot() {
190 return getScriptableFor(getDomNodeOrDie().appendChildIfNoneExists("tfoot"));
191 }
192
193
194
195
196
197
198
199
200 @JsxFunction
201 public HtmlUnitScriptable createTBody() {
202 return getScriptableFor(getDomNodeOrDie().appendChildIfNoneExists("tbody"));
203 }
204
205
206
207
208
209
210
211
212 @JsxFunction
213 public HtmlUnitScriptable createTHead() {
214 return getScriptableFor(getDomNodeOrDie().appendChildIfNoneExists("thead"));
215 }
216
217
218
219
220
221
222
223 @JsxFunction
224 public void deleteCaption() {
225 getDomNodeOrDie().removeChild("caption", 0);
226 }
227
228
229
230
231
232
233
234 @JsxFunction
235 public void deleteTFoot() {
236 getDomNodeOrDie().removeChild("tfoot", 0);
237 }
238
239
240
241
242
243
244
245 @JsxFunction
246 public void deleteTHead() {
247 getDomNodeOrDie().removeChild("thead", 0);
248 }
249
250
251
252
253
254
255 @Override
256 protected boolean isContainedRow(final HtmlTableRow row) {
257 final DomNode parent = row.getParentNode();
258 return parent != null
259 && parent.getParentNode() == getDomNodeOrDie();
260 }
261
262
263
264
265
266 @Override
267 public HtmlUnitScriptable insertRow(final int index) {
268
269 if (index != 0) {
270 for (final HtmlElement htmlElement : getDomNodeOrDie().getHtmlElementDescendants()) {
271 if (htmlElement instanceof HtmlTableBody
272 || htmlElement instanceof HtmlTableHeader
273 || htmlElement instanceof HtmlTableFooter) {
274 return super.insertRow(index);
275 }
276 }
277 }
278
279 final HtmlElement tBody = getDomNodeOrDie().appendChildIfNoneExists("tbody");
280 return ((RowContainer) getScriptableFor(tBody)).insertRow(0);
281 }
282
283
284
285
286
287 @JsxGetter(propertyName = "width")
288 public String getWidth_js() {
289 return getDomNodeOrDie().getAttributeDirect("width");
290 }
291
292
293
294
295
296 @JsxSetter(propertyName = "width")
297 public void setWidth_js(final String width) {
298 getDomNodeOrDie().setAttribute("width", width);
299 }
300
301
302
303
304
305 @JsxGetter
306 public String getCellSpacing() {
307 return getDomNodeOrDie().getAttributeDirect("cellspacing");
308 }
309
310
311
312
313
314 @JsxSetter
315 public void setCellSpacing(final String cellSpacing) {
316 getDomNodeOrDie().setAttribute("cellspacing", cellSpacing);
317 }
318
319
320
321
322
323 @JsxGetter
324 public String getCellPadding() {
325 return getDomNodeOrDie().getAttributeDirect("cellpadding");
326 }
327
328
329
330
331
332 @JsxSetter
333 public void setCellPadding(final String cellPadding) {
334 getDomNodeOrDie().setAttribute("cellpadding", cellPadding);
335 }
336
337
338
339
340
341 @JsxGetter
342 public String getBorder() {
343 return getDomNodeOrDie().getAttributeDirect("border");
344 }
345
346
347
348
349
350 @JsxSetter
351 public void setBorder(final String border) {
352 getDomNodeOrDie().setAttribute("border", border);
353 }
354
355
356
357
358
359
360 @JsxGetter
361 public String getBgColor() {
362 return getDomNodeOrDie().getAttribute("bgColor");
363 }
364
365
366
367
368
369
370 @JsxSetter
371 public void setBgColor(final String bgColor) {
372 setColorAttribute("bgColor", bgColor);
373 }
374
375
376
377
378 @Override
379 public Node appendChild(final Object childObject) {
380 final Node appendedChild = super.appendChild(childObject);
381 getDomNodeOrDie().getPage().clearComputedStyles(getDomNodeOrDie());
382 return appendedChild;
383 }
384
385
386
387
388 @Override
389 public Node removeChild(final Object childObject) {
390 final Node removedChild = super.removeChild(childObject);
391 getDomNodeOrDie().getPage().clearComputedStyles(getDomNodeOrDie());
392 return removedChild;
393 }
394
395
396
397
398
399 @JsxGetter
400 public String getSummary() {
401 return getDomNodeOrDie().getAttributeDirect("summary");
402 }
403
404
405
406
407
408 @JsxSetter
409 public void setSummary(final String summary) {
410 setAttribute("summary", summary);
411 }
412
413
414
415
416
417 @JsxGetter
418 public String getRules() {
419 return getDomNodeOrDie().getAttributeDirect("rules");
420 }
421
422
423
424
425
426 @JsxSetter
427 public void setRules(final String rules) {
428 setAttribute("rules", rules);
429 }
430
431 }