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 @JsxClass(domClass = HtmlTableRow.class)
46 public class HTMLTableRowElement extends HTMLTableComponent {
47
48
49
50
51 @Override
52 @JsxConstructor
53 public void jsConstructor() {
54 super.jsConstructor();
55 }
56
57
58
59
60
61
62 @JsxGetter
63 public int getRowIndex() {
64 final HtmlTableRow row = (HtmlTableRow) getDomNodeOrDie();
65 final HtmlTable table = row.getEnclosingTable();
66 if (table == null) {
67 return -1;
68 }
69 return table.getRows().indexOf(row);
70 }
71
72
73
74
75
76
77
78
79 @JsxGetter
80 public int getSectionRowIndex() {
81 DomNode row = getDomNodeOrDie();
82 final HtmlTable table = ((HtmlTableRow) row).getEnclosingTable();
83 if (table == null) {
84 return -1;
85 }
86 int index = -1;
87 while (row != null) {
88 if (row instanceof HtmlTableRow) {
89 index++;
90 }
91 row = row.getPreviousSibling();
92 }
93 return index;
94 }
95
96
97
98
99
100 @JsxGetter
101 public HTMLCollection getCells() {
102 final HtmlTableRow row = (HtmlTableRow) getDomNodeOrDie();
103
104 final HTMLCollection cells = new HTMLCollection(row, false);
105 cells.setElementsSupplier((Supplier<List<DomNode>> & Serializable) () -> new ArrayList<>(row.getCells()));
106 return cells;
107 }
108
109
110
111
112
113
114 @JsxGetter
115 public String getBgColor() {
116 return getDomNodeOrDie().getAttribute("bgColor");
117 }
118
119
120
121
122
123
124 @JsxSetter
125 public void setBgColor(final String bgColor) {
126 setColorAttribute("bgColor", bgColor);
127 }
128
129
130
131
132
133
134
135
136
137
138 @JsxFunction
139 public HtmlUnitScriptable insertCell(final Object index) {
140 int position = -1;
141 if (!JavaScriptEngine.isUndefined(index)) {
142 position = (int) JavaScriptEngine.toNumber(index);
143 }
144 final HtmlTableRow htmlRow = (HtmlTableRow) getDomNodeOrDie();
145
146 final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size();
147 if (indexValid) {
148 final DomElement newCell = ((HtmlPage) htmlRow.getPage()).createElement("td");
149 if (position == -1 || position == htmlRow.getCells().size()) {
150 htmlRow.appendChild(newCell);
151 }
152 else {
153 htmlRow.getCell(position).insertBefore(newCell);
154 }
155 return getScriptableFor(newCell);
156 }
157 throw JavaScriptEngine.asJavaScriptException(
158 getWindow(),
159 "Index or size is negative or greater than the allowed amount",
160 DOMException.INDEX_SIZE_ERR);
161 }
162
163
164
165
166
167
168
169
170 @JsxFunction
171 public void deleteCell(final Object index) {
172 if (JavaScriptEngine.isUndefined(index)) {
173 throw JavaScriptEngine.typeError("No enough arguments");
174 }
175
176 int position = (int) JavaScriptEngine.toNumber(index);
177
178 final HtmlTableRow htmlRow = (HtmlTableRow) getDomNodeOrDie();
179
180 if (position == -1) {
181 position = htmlRow.getCells().size() - 1;
182 }
183 final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size();
184 if (!indexValid) {
185 throw JavaScriptEngine.asJavaScriptException(
186 getWindow(),
187 "Index or size is negative or greater than the allowed amount",
188 DOMException.INDEX_SIZE_ERR);
189 }
190
191 htmlRow.getCell(position).remove();
192 }
193
194
195
196
197
198 @Override
199 public void setOuterHTML(final Object value) {
200 throw JavaScriptEngine.reportRuntimeError("outerHTML is read-only for tag 'tr'");
201 }
202 }