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.HtmlTableRow;
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.JsxFunction;
28 import org.htmlunit.javascript.configuration.JsxGetter;
29 import org.htmlunit.javascript.configuration.JsxSetter;
30 import org.htmlunit.javascript.host.dom.DOMException;
31
32
33
34
35
36
37
38
39
40
41 @JsxClass(isJSObject = false)
42 public class RowContainer extends HTMLElement {
43
44
45
46
47
48 @JsxGetter
49 public HTMLCollection getRows() {
50 final HTMLCollection rows = new HTMLCollection(getDomNodeOrDie(), false);
51 rows.setIsMatchingPredicate(
52 (Predicate<DomNode> & Serializable)
53 node -> node instanceof HtmlTableRow && isContainedRow((HtmlTableRow) node));
54 return rows;
55 }
56
57
58
59
60
61
62 protected boolean isContainedRow(final HtmlTableRow row) {
63 return row.getParentNode() == getDomNodeOrDie();
64 }
65
66
67
68
69
70
71 @JsxFunction
72 public void deleteRow(int rowIndex) {
73 final HTMLCollection rows = getRows();
74 final int rowCount = rows.getLength();
75 if (rowIndex == -1) {
76 rowIndex = rowCount - 1;
77 }
78 final boolean rowIndexValid = rowIndex >= 0 && rowIndex < rowCount;
79 if (rowIndexValid) {
80 final HtmlUnitScriptable row = (HtmlUnitScriptable) rows.item(Integer.valueOf(rowIndex));
81 row.getDomNodeOrDie().remove();
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94 @JsxFunction
95 public HtmlUnitScriptable insertRow(final Object index) {
96 int rowIndex = -1;
97 if (!JavaScriptEngine.isUndefined(index)) {
98 rowIndex = (int) JavaScriptEngine.toNumber(index);
99 }
100 final HTMLCollection rows = getRows();
101 final int rowCount = rows.getLength();
102 final int r;
103 if (rowIndex == -1 || rowIndex == rowCount) {
104 r = Math.max(0, rowCount);
105 }
106 else {
107 r = rowIndex;
108 }
109
110 if (r < 0 || r > rowCount) {
111 throw JavaScriptEngine.asJavaScriptException(
112 getWindow(),
113 "Index or size is negative or greater than the allowed amount "
114 + "(index: " + rowIndex + ", " + rowCount + " rows)",
115 DOMException.INDEX_SIZE_ERR);
116 }
117
118 return insertRow(r);
119 }
120
121
122
123
124
125
126 public HtmlUnitScriptable insertRow(final int index) {
127 final HTMLCollection rows = getRows();
128 final int rowCount = rows.getLength();
129 final DomElement newRow = ((HtmlPage) getDomNodeOrDie().getPage()).createElement("tr");
130 if (rowCount == 0) {
131 getDomNodeOrDie().appendChild(newRow);
132 }
133 else if (index == rowCount) {
134 final HtmlUnitScriptable row = (HtmlUnitScriptable) rows.item(Integer.valueOf(index - 1));
135 row.getDomNodeOrDie().getParentNode().appendChild(newRow);
136 }
137 else {
138 final HtmlUnitScriptable row = (HtmlUnitScriptable) rows.item(Integer.valueOf(index));
139
140 if (index > rowCount - 1) {
141 row.getDomNodeOrDie().getParentNode().appendChild(newRow);
142 }
143 else {
144 row.getDomNodeOrDie().insertBefore(newRow);
145 }
146 }
147 return getScriptableFor(newRow);
148 }
149
150
151
152
153
154 @JsxGetter
155 public String getAlign() {
156 return getAlign(true);
157 }
158
159
160
161
162
163 @JsxSetter
164 public void setAlign(final String align) {
165 setAlign(align, false);
166 }
167
168 }