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.DomElement;
23 import org.htmlunit.html.DomNode;
24 import org.htmlunit.html.HtmlPage;
25 import org.htmlunit.html.HtmlTable;
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 = HtmlTableRow.class)
47 public class HTMLTableRowElement extends HTMLElement {
48
49
50 private static final String VALIGN_DEFAULT_VALUE = "top";
51
52
53
54
55 @Override
56 @JsxConstructor
57 public void jsConstructor() {
58 super.jsConstructor();
59 }
60
61
62
63
64
65
66 @JsxGetter
67 public int getRowIndex() {
68 final HtmlTableRow row = (HtmlTableRow) getDomNodeOrDie();
69 final HtmlTable table = row.getEnclosingTable();
70 if (table == null) {
71 return -1;
72 }
73 return table.getRows().indexOf(row);
74 }
75
76
77
78
79
80
81
82
83 @JsxGetter
84 public int getSectionRowIndex() {
85 DomNode row = getDomNodeOrDie();
86 final HtmlTable table = ((HtmlTableRow) row).getEnclosingTable();
87 if (table == null) {
88 return -1;
89 }
90 int index = -1;
91 while (row != null) {
92 if (row instanceof HtmlTableRow) {
93 index++;
94 }
95 row = row.getPreviousSibling();
96 }
97 return index;
98 }
99
100
101
102
103
104 @JsxGetter
105 public HTMLCollection getCells() {
106 final HtmlTableRow row = (HtmlTableRow) getDomNodeOrDie();
107
108 final HTMLCollection cells = new HTMLCollection(row, false);
109 cells.setElementsSupplier((Supplier<List<DomNode>> & Serializable) () -> new ArrayList<>(row.getCells()));
110 return cells;
111 }
112
113
114
115
116
117
118 @JsxGetter
119 public String getBgColor() {
120 return getDomNodeOrDie().getAttribute("bgColor");
121 }
122
123
124
125
126
127
128 @JsxSetter
129 public void setBgColor(final String bgColor) {
130 setColorAttribute("bgColor", bgColor);
131 }
132
133
134
135
136
137
138
139
140
141
142 @JsxFunction
143 public HtmlUnitScriptable insertCell(final Object index) {
144 int position = -1;
145 if (!JavaScriptEngine.isUndefined(index)) {
146 position = (int) JavaScriptEngine.toNumber(index);
147 }
148 final HtmlTableRow htmlRow = (HtmlTableRow) getDomNodeOrDie();
149
150 final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size();
151 if (indexValid) {
152 final DomElement newCell = ((HtmlPage) htmlRow.getPage()).createElement("td");
153 if (position == -1 || position == htmlRow.getCells().size()) {
154 htmlRow.appendChild(newCell);
155 }
156 else {
157 htmlRow.getCell(position).insertBefore(newCell);
158 }
159 return getScriptableFor(newCell);
160 }
161 throw JavaScriptEngine.asJavaScriptException(
162 getWindow(),
163 "Index or size is negative or greater than the allowed amount",
164 DOMException.INDEX_SIZE_ERR);
165 }
166
167
168
169
170
171
172
173
174 @JsxFunction
175 public void deleteCell(final Object index) {
176 if (JavaScriptEngine.isUndefined(index)) {
177 throw JavaScriptEngine.typeError("No enough arguments");
178 }
179
180 int position = (int) JavaScriptEngine.toNumber(index);
181
182 final HtmlTableRow htmlRow = (HtmlTableRow) getDomNodeOrDie();
183
184 if (position == -1) {
185 position = htmlRow.getCells().size() - 1;
186 }
187 final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size();
188 if (!indexValid) {
189 throw JavaScriptEngine.asJavaScriptException(
190 getWindow(),
191 "Index or size is negative or greater than the allowed amount",
192 DOMException.INDEX_SIZE_ERR);
193 }
194
195 htmlRow.getCell(position).remove();
196 }
197
198
199
200
201
202 @Override
203 public void setOuterHTML(final Object value) {
204 throw JavaScriptEngine.reportRuntimeError("outerHTML is read-only for tag 'tr'");
205 }
206
207
208
209
210
211 @JsxGetter
212 public String getAlign() {
213 return getAlign(true);
214 }
215
216
217
218
219
220 @JsxSetter
221 public void setAlign(final String align) {
222 setAlign(align, false);
223 }
224
225
226
227
228
229 @JsxGetter
230 public String getVAlign() {
231 return getVAlign(getValidVAlignValues(), VALIGN_DEFAULT_VALUE);
232 }
233
234
235
236
237
238 @JsxSetter
239 public void setVAlign(final Object vAlign) {
240 setVAlign(vAlign, getValidVAlignValues());
241 }
242
243
244
245
246
247 private String[] getValidVAlignValues() {
248 return null;
249 }
250
251
252
253
254
255 @Override
256 @JsxGetter
257 public String getCh() {
258 return super.getCh();
259 }
260
261
262
263
264
265 @Override
266 @JsxSetter
267 public void setCh(final String ch) {
268 super.setCh(ch);
269 }
270
271
272
273
274
275 @Override
276 @JsxGetter
277 public String getChOff() {
278 return super.getChOff();
279 }
280
281
282
283
284
285 @Override
286 @JsxSetter
287 public void setChOff(final String chOff) {
288 super.setChOff(chOff);
289 }
290 }