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.function.Predicate;
19
20 import org.htmlunit.html.DomElement;
21 import org.htmlunit.html.DomNode;
22 import org.htmlunit.html.HtmlPage;
23 import org.htmlunit.html.HtmlTableBody;
24 import org.htmlunit.html.HtmlTableFooter;
25 import org.htmlunit.html.HtmlTableHeader;
26 import org.htmlunit.html.HtmlTableRow;
27 import org.htmlunit.javascript.HtmlUnitScriptable;
28 import org.htmlunit.javascript.JavaScriptEngine;
29 import org.htmlunit.javascript.configuration.JsxClass;
30 import org.htmlunit.javascript.configuration.JsxConstructor;
31 import org.htmlunit.javascript.configuration.JsxFunction;
32 import org.htmlunit.javascript.configuration.JsxGetter;
33 import org.htmlunit.javascript.configuration.JsxSetter;
34 import org.htmlunit.javascript.host.dom.DOMException;
35
36
37
38
39
40
41
42
43
44
45
46 @JsxClass(domClass = HtmlTableBody.class)
47 @JsxClass(domClass = HtmlTableHeader.class)
48 @JsxClass(domClass = HtmlTableFooter.class)
49 public class HTMLTableSectionElement extends HTMLElement {
50
51
52 private static final String VALIGN_DEFAULT_VALUE = "top";
53
54
55
56
57 @Override
58 @JsxConstructor
59 public void jsConstructor() {
60 super.jsConstructor();
61 }
62
63
64
65
66
67 @JsxGetter
68 public String getVAlign() {
69 return getVAlign(getValidVAlignValues(), VALIGN_DEFAULT_VALUE);
70 }
71
72
73
74
75
76 @JsxSetter
77 public void setVAlign(final Object vAlign) {
78 setVAlign(vAlign, getValidVAlignValues());
79 }
80
81
82
83
84
85 private String[] getValidVAlignValues() {
86 return null;
87 }
88
89
90
91
92
93 @Override
94 @JsxGetter
95 public String getCh() {
96 return super.getCh();
97 }
98
99
100
101
102
103 @Override
104 @JsxSetter
105 public void setCh(final String ch) {
106 super.setCh(ch);
107 }
108
109
110
111
112
113 @Override
114 @JsxGetter
115 public String getChOff() {
116 return super.getChOff();
117 }
118
119
120
121
122
123 @Override
124 @JsxSetter
125 public void setChOff(final String chOff) {
126 super.setChOff(chOff);
127 }
128
129
130
131
132
133 @JsxGetter
134 public HTMLCollection getRows() {
135 final HTMLCollection rows = new HTMLCollection(getDomNodeOrDie(), false);
136 rows.setIsMatchingPredicate(
137 (Predicate<DomNode> & Serializable)
138 node -> node instanceof HtmlTableRow htr && isContainedRow(htr));
139 return rows;
140 }
141
142
143
144
145
146
147 protected boolean isContainedRow(final HtmlTableRow row) {
148 return row.getParentNode() == getDomNodeOrDie();
149 }
150
151
152
153
154
155
156 @JsxFunction
157 public void deleteRow(final int rowIndex) {
158 final HTMLCollection rows = getRows();
159 final int rowCount = rows.getLength();
160 final int index = rowIndex == -1 ? rowCount - 1 : rowIndex;
161 final boolean rowIndexValid = index >= 0 && index < rowCount;
162 if (rowIndexValid) {
163 final HtmlUnitScriptable row = (HtmlUnitScriptable) rows.item(Integer.valueOf(index));
164 row.getDomNodeOrDie().remove();
165 }
166 }
167
168
169
170
171
172
173
174
175
176
177 @JsxFunction
178 public HtmlUnitScriptable insertRow(final Object index) {
179 int rowIndex = -1;
180 if (!JavaScriptEngine.isUndefined(index)) {
181 rowIndex = (int) JavaScriptEngine.toNumber(index);
182 }
183 final HTMLCollection rows = getRows();
184 final int rowCount = rows.getLength();
185 final int r;
186 if (rowIndex == -1 || rowIndex == rowCount) {
187 r = Math.max(0, rowCount);
188 }
189 else {
190 r = rowIndex;
191 }
192
193 if (r < 0 || r > rowCount) {
194 throw JavaScriptEngine.asJavaScriptException(
195 getWindow(),
196 "Index or size is negative or greater than the allowed amount "
197 + "(index: " + rowIndex + ", " + rowCount + " rows)",
198 DOMException.INDEX_SIZE_ERR);
199 }
200
201 return insertRow(r);
202 }
203
204
205
206
207
208
209 public HtmlUnitScriptable insertRow(final int index) {
210 final HTMLCollection rows = getRows();
211 final int rowCount = rows.getLength();
212 final DomElement newRow = ((HtmlPage) getDomNodeOrDie().getPage()).createElement("tr");
213 if (rowCount == 0) {
214 getDomNodeOrDie().appendChild(newRow);
215 }
216 else if (index == rowCount) {
217 final HtmlUnitScriptable row = (HtmlUnitScriptable) rows.item(Integer.valueOf(index - 1));
218 row.getDomNodeOrDie().getParentNode().appendChild(newRow);
219 }
220 else {
221 final HtmlUnitScriptable row = (HtmlUnitScriptable) rows.item(Integer.valueOf(index));
222
223 if (index > rowCount - 1) {
224 row.getDomNodeOrDie().getParentNode().appendChild(newRow);
225 }
226 else {
227 row.getDomNodeOrDie().insertBefore(newRow);
228 }
229 }
230 return getScriptableFor(newRow);
231 }
232
233
234
235
236
237 @JsxGetter
238 public String getAlign() {
239 return getAlign(true);
240 }
241
242
243
244
245
246 @JsxSetter
247 public void setAlign(final String align) {
248 setAlign(align, false);
249 }
250 }