1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.NoSuchElementException;
23
24 import org.htmlunit.SgmlPage;
25
26
27
28
29
30
31
32
33
34
35
36 public class HtmlTableRow extends HtmlElement {
37
38
39 public static final String TAG_NAME = "tr";
40
41
42
43
44
45
46
47
48 HtmlTableRow(final String qualifiedName, final SgmlPage page,
49 final Map<String, DomAttr> attributes) {
50 super(qualifiedName, page, attributes);
51 }
52
53
54
55
56 public CellIterator getCellIterator() {
57 return new CellIterator();
58 }
59
60
61
62
63
64 public List<HtmlTableCell> getCells() {
65 final List<HtmlTableCell> result = new ArrayList<>();
66 for (final HtmlTableCell cell : getCellIterator()) {
67 result.add(cell);
68 }
69 return Collections.unmodifiableList(result);
70 }
71
72
73
74
75
76
77 public HtmlTableCell getCell(final int index) throws IndexOutOfBoundsException {
78 int count = 0;
79 for (final HtmlTableCell cell : getCellIterator()) {
80 if (count == index) {
81 return cell;
82 }
83 count++;
84 }
85 throw new IndexOutOfBoundsException("No cell found for index " + index + ".");
86 }
87
88
89
90
91
92
93
94
95
96 public final String getAlignAttribute() {
97 return getAttributeDirect("align");
98 }
99
100
101
102
103
104
105
106
107
108 public final String getCharAttribute() {
109 return getAttributeDirect("char");
110 }
111
112
113
114
115
116
117
118
119
120 public final String getCharoffAttribute() {
121 return getAttributeDirect("charoff");
122 }
123
124
125
126
127
128
129
130
131
132 public final String getValignAttribute() {
133 return getAttributeDirect("valign");
134 }
135
136
137
138
139
140 public HtmlTable getEnclosingTable() {
141 return (HtmlTable) getEnclosingElement("table");
142 }
143
144
145
146
147
148
149
150
151
152 public final String getBgcolorAttribute() {
153 return getAttributeDirect("bgcolor");
154 }
155
156
157
158
159
160 public class CellIterator implements Iterator<HtmlTableCell>, Iterable<HtmlTableCell> {
161 private HtmlTableCell nextCell_;
162 private HtmlForm currentForm_;
163
164
165 public CellIterator() {
166 setNextCell(getFirstChild());
167 }
168
169
170
171
172 @Override
173 public boolean hasNext() {
174 return nextCell_ != null;
175 }
176
177
178
179
180
181 @Override
182 public HtmlTableCell next() throws NoSuchElementException {
183 return nextCell();
184 }
185
186
187
188
189 @Override
190 public void remove() {
191 if (nextCell_ == null) {
192 throw new IllegalStateException();
193 }
194 final DomNode sibling = nextCell_.getPreviousSibling();
195 if (sibling != null) {
196 sibling.remove();
197 }
198 }
199
200
201
202
203
204 public HtmlTableCell nextCell() throws NoSuchElementException {
205 if (nextCell_ != null) {
206 final HtmlTableCell result = nextCell_;
207 setNextCell(nextCell_.getNextSibling());
208 return result;
209 }
210 throw new NoSuchElementException();
211 }
212
213
214
215
216
217
218 private void setNextCell(final DomNode node) {
219 nextCell_ = null;
220 for (DomNode next = node; next != null; next = next.getNextSibling()) {
221 if (next instanceof HtmlTableCell) {
222 nextCell_ = (HtmlTableCell) next;
223 return;
224 }
225 else if (currentForm_ == null && next instanceof HtmlForm) {
226
227 currentForm_ = (HtmlForm) next;
228 setNextCell(next.getFirstChild());
229 return;
230 }
231 }
232 if (currentForm_ != null) {
233 final DomNode form = currentForm_;
234 currentForm_ = null;
235 setNextCell(form.getNextSibling());
236 }
237 }
238
239
240
241
242
243
244 @Override
245 public Iterator<HtmlTableCell> iterator() {
246 return this;
247 }
248 }
249
250
251
252
253 @Override
254 public DisplayStyle getDefaultStyleDisplay() {
255 return DisplayStyle.TABLE_ROW;
256 }
257 }