View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.html;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link HTMLTableColElement}.
23   *
24   * @author Daniel Gredler
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   * @author Frank Danek
28   */
29  public class HTMLTableColElementTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if an error occurs
33       */
34      @Test
35      @Alerts({"left", "right", "justify", "char", "center", "wrong", ""})
36      public void getAlign() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html><body>\n"
39              + "  <table>\n"
40              + "    <col id='c1' align='left' ></col>\n"
41              + "    <col id='c2' align='right' ></col>\n"
42              + "    <col id='c3' align='justify' ></col>\n"
43              + "    <col id='c4' align='char' ></col>\n"
44              + "    <col id='c5' align='center' ></col>\n"
45              + "    <col id='c6' align='wrong' ></col>\n"
46              + "    <col id='c7' ></col>\n"
47              + "  </table>\n"
48  
49              + "<script>\n"
50              + LOG_TITLE_FUNCTION
51              + "  for (var i = 1; i <= 7; i++) {\n"
52              + "    log(document.getElementById('c' + i).align);\n"
53              + "  }\n"
54              + "</script>\n"
55              + "</body></html>";
56  
57          loadPageVerifyTitle2(html);
58      }
59  
60      /**
61       * @throws Exception if an error occurs
62       */
63      @Test
64      @Alerts({"CenTer", "8", "foo", "left", "right", "justify", "char", "center"})
65      public void setAlign() throws Exception {
66          final String html = DOCTYPE_HTML
67              + "<html><body>\n"
68              + "  <table>\n"
69              + "    <col id='c1' align='left' ></col>\n"
70              + "  </table>\n"
71  
72              + "<script>\n"
73              + LOG_TITLE_FUNCTION
74              + "  function setAlign(elem, value) {\n"
75              + "    try {\n"
76              + "      elem.align = value;\n"
77              + "    } catch(e) { logEx(e); }\n"
78              + "    log(elem.align);\n"
79              + "  }\n"
80  
81              + "  var elem = document.getElementById('c1');\n"
82              + "  setAlign(elem, 'CenTer');\n"
83  
84              + "  setAlign(elem, '8');\n"
85              + "  setAlign(elem, 'foo');\n"
86  
87              + "  setAlign(elem, 'left');\n"
88              + "  setAlign(elem, 'right');\n"
89              + "  setAlign(elem, 'justify');\n"
90              + "  setAlign(elem, 'char');\n"
91              + "  setAlign(elem, 'center');\n"
92              + "</script>\n"
93              + "</body></html>";
94  
95          loadPageVerifyTitle2(html);
96      }
97  
98      /**
99       * @throws Exception if an error occurs
100      */
101     @Test
102     @Alerts({"p", "po", "", "u", "8", "U8"})
103     public void ch() throws Exception {
104         final String html = DOCTYPE_HTML
105             + "<html><body><table>\n"
106             + "  <col id='c1' char='p'></col>\n"
107             + "  <col id='c2' char='po'></col>\n"
108             + "  <col id='c3'></col>\n"
109             + "  <tr>\n"
110             + "    <td>a</td>\n"
111             + "    <td>b</td>\n"
112             + "    <td>c</td>\n"
113             + "  </tr>\n"
114             + "</table>\n"
115             + "<script>\n"
116             + LOG_TITLE_FUNCTION
117             + "  var c1 = document.getElementById('c1');\n"
118             + "  var c2 = document.getElementById('c2');\n"
119             + "  var c3 = document.getElementById('c3');\n"
120             + "  log(c1.ch);\n"
121             + "  log(c2.ch);\n"
122             + "  log(c3.ch);\n"
123             + "  c1.ch = 'u';\n"
124             + "  c2.ch = '8';\n"
125             + "  c3.ch = 'U8';\n"
126             + "  log(c1.ch);\n"
127             + "  log(c2.ch);\n"
128             + "  log(c3.ch);\n"
129             + "</script>\n"
130             + "</body></html>";
131 
132         loadPageVerifyTitle2(html);
133     }
134 
135     /**
136      * @throws Exception if an error occurs
137      */
138     @Test
139     @Alerts({"0", "4", "", "5.2", "-3", "abc"})
140     public void chOff() throws Exception {
141         final String html = DOCTYPE_HTML
142             + "<html><body><table>\n"
143             + "  <col id='c1' charoff='0'></col>\n"
144             + "  <col id='c2' charoff='4'></col>\n"
145             + "  <col id='c3'></col>\n"
146             + "  <tr>\n"
147             + "    <td>a</td>\n"
148             + "    <td>b</td>\n"
149             + "    <td>c</td>\n"
150             + "  </tr>\n"
151             + "</table>\n"
152             + "<script>\n"
153             + LOG_TITLE_FUNCTION
154             + "  var c1 = document.getElementById('c1');\n"
155             + "  var c2 = document.getElementById('c2');\n"
156             + "  var c3 = document.getElementById('c3');\n"
157             + "  log(c1.chOff);\n"
158             + "  log(c2.chOff);\n"
159             + "  log(c3.chOff);\n"
160             + "  c1.chOff = '5.2';\n"
161             + "  c2.chOff = '-3';\n"
162             + "  c3.chOff = 'abc';\n"
163             + "  log(c1.chOff);\n"
164             + "  log(c2.chOff);\n"
165             + "  log(c3.chOff);\n"
166             + "</script>\n"
167             + "</body></html>";
168 
169         loadPageVerifyTitle2(html);
170     }
171 
172     /**
173      * @throws Exception if an error occurs
174      */
175     @Test
176     @Alerts({"1", "2", "1", "5", "1", "1"})
177     public void span() throws Exception {
178         final String html = DOCTYPE_HTML
179             + "<html><body><table>\n"
180             + "  <col id='c1' span='0'></col>\n"
181             + "  <col id='c2' span='2'></col>\n"
182             + "  <col id='c3'></col>\n"
183             + "  <tr>\n"
184             + "    <td>a</td>\n"
185             + "    <td>b</td>\n"
186             + "    <td>c</td>\n"
187             + "  </tr>\n"
188             + "</table>\n"
189             + "<script>\n"
190             + LOG_TITLE_FUNCTION
191             + "  function set(e, value) {\n"
192             + "    try {\n"
193             + "      e.span = value;\n"
194             + "    } catch(e) { logEx(e); }\n"
195             + "  }\n"
196             + "  var c1 = document.getElementById('c1');\n"
197             + "  var c2 = document.getElementById('c2');\n"
198             + "  var c3 = document.getElementById('c3');\n"
199             + "  log(c1.span);\n"
200             + "  log(c2.span);\n"
201             + "  log(c3.span);\n"
202             + "  set(c1, '5.2');\n"
203             + "  set(c2, '-3');\n"
204             + "  set(c3, 'abc');\n"
205             + "  log(c1.span);\n"
206             + "  log(c2.span);\n"
207             + "  log(c3.span);\n"
208             + "</script>\n"
209             + "</body></html>";
210 
211         loadPageVerifyTitle2(html);
212     }
213 
214     /**
215      * @throws Exception if an error occurs
216      */
217     @Test
218     @Alerts({"top", "baseline", "3", "middle", "8", "BOTtom"})
219     public void vAlign() throws Exception {
220         final String html = DOCTYPE_HTML
221             + "<html><body><table>\n"
222             + "  <col id='c1' valign='top'></col>\n"
223             + "  <col id='c2' valign='baseline'></col>\n"
224             + "  <col id='c3' valign='3'></col>\n"
225             + "  <tr>\n"
226             + "    <td>a</td>\n"
227             + "    <td>b</td>\n"
228             + "    <td>c</td>\n"
229             + "  </tr>\n"
230             + "</table>\n"
231             + "<script>\n"
232             + LOG_TITLE_FUNCTION
233             + "  function set(e, value) {\n"
234             + "    try {\n"
235             + "      e.vAlign = value;\n"
236             + "    } catch(e) { logEx(e); }\n"
237             + "  }\n"
238             + "  var c1 = document.getElementById('c1');\n"
239             + "  var c2 = document.getElementById('c2');\n"
240             + "  var c3 = document.getElementById('c3');\n"
241             + "  log(c1.vAlign);\n"
242             + "  log(c2.vAlign);\n"
243             + "  log(c3.vAlign);\n"
244             + "  set(c1, 'middle');\n"
245             + "  set(c2, 8);\n"
246             + "  set(c3, 'BOTtom');\n"
247             + "  log(c1.vAlign);\n"
248             + "  log(c2.vAlign);\n"
249             + "  log(c3.vAlign);\n"
250             + "</script>\n"
251             + "</body></html>";
252 
253         loadPageVerifyTitle2(html);
254     }
255 
256     /**
257      * @throws Exception if an error occurs
258      */
259     @Test
260     @Alerts({"50", "75%", "foo", "-7", "20.2", "", "80", "40", "abc", "-10", "30%", "33.3"})
261     public void width() throws Exception {
262         final String html = DOCTYPE_HTML
263             + "<html><body><table>\n"
264             + "  <col id='c1' width='50'></col>\n"
265             + "  <col id='c2' width='75%'></col>\n"
266             + "  <col id='c3' width='foo'></col>\n"
267             + "  <col id='c4' width='-7'></col>\n"
268             + "  <col id='c5' width='20.2'></col>\n"
269             + "  <col id='c6'></col>\n"
270             + "  <tr>\n"
271             + "    <td>a</td>\n"
272             + "    <td>b</td>\n"
273             + "    <td>c</td>\n"
274             + "    <td>d</td>\n"
275             + "  </tr>\n"
276             + "</table>\n"
277             + "<script>\n"
278             + LOG_TITLE_FUNCTION
279             + "  function set(e, value) {\n"
280             + "    try {\n"
281             + "      e.width = value;\n"
282             + "    } catch(e) { logEx(e); }\n"
283             + "  }\n"
284             + "  var c1 = document.getElementById('c1');\n"
285             + "  var c2 = document.getElementById('c2');\n"
286             + "  var c3 = document.getElementById('c3');\n"
287             + "  var c4 = document.getElementById('c4');\n"
288             + "  var c5 = document.getElementById('c5');\n"
289             + "  var c6 = document.getElementById('c6');\n"
290             + "  log(c1.width);\n"
291             + "  log(c2.width);\n"
292             + "  log(c3.width);\n"
293             + "  log(c4.width);\n"
294             + "  log(c5.width);\n"
295             + "  log(c6.width);\n"
296             + "  set(c1, '80');\n"
297             + "  set(c2, 40);\n"
298             + "  set(c3, 'abc');\n"
299             + "  set(c4, -10);\n"
300             + "  set(c5, '30%');\n"
301             + "  set(c6, 33.3);\n"
302             + "  log(c1.width);\n"
303             + "  log(c2.width);\n"
304             + "  log(c3.width);\n"
305             + "  log(c4.width);\n"
306             + "  log(c5.width);\n"
307             + "  log(c6.width);\n"
308             + "</script>\n"
309             + "</body></html>";
310 
311         loadPageVerifyTitle2(html);
312     }
313 
314     /**
315      * @throws Exception if an error occurs
316      */
317     @Test
318     @Alerts("128px")
319     public void width_px() throws Exception {
320         final String html = DOCTYPE_HTML
321             + "<html><head>\n"
322             + "<script>\n"
323             + LOG_TITLE_FUNCTION
324             + "  function test() {\n"
325             + "    myCol.width = '128px';\n"
326             + "    log(myCol.width);\n"
327             + "  }\n"
328             + "</script>\n"
329             + "<body onload='test()'>\n"
330             + "<table>\n"
331             + "  <col id='myCol'></col>\n"
332             + "</table>\n"
333             + "</body></html>";
334 
335         loadPageVerifyTitle2(html);
336     }
337 
338     /**
339      * Regression test for bug 2948498.
340      * @throws Exception if an error occurs
341      */
342     @Test
343     @Alerts({"null", "string"})
344     public void width_null() throws Exception {
345         final String html = DOCTYPE_HTML
346             + "<html><head>\n"
347             + "<script>\n"
348             + LOG_TITLE_FUNCTION
349             + "  function test() {\n"
350             + "    myCol.width = null;\n"
351             + "    log(myCol.width);\n"
352             + "    log(typeof myCol.width);\n"
353             + "  }\n"
354             + "</script>\n"
355             + "<body onload='test()'>\n"
356             + "<table>\n"
357             + "  <col id='myCol'></col>\n"
358             + "</table>\n"
359             + "</body></html>";
360 
361         loadPageVerifyTitle2(html);
362     }
363 
364     /**
365      * @throws Exception if an error occurs
366      */
367     @Test
368     @Alerts("<table><colgroup><col></colgroup></table>")
369     public void parsing() throws Exception {
370         final String html = DOCTYPE_HTML
371             + "<html><body><div><table><colgroup><col></colgroup></table></div>\n"
372             + "<script>\n"
373             + LOG_TITLE_FUNCTION
374             + "  log(document.body.firstChild.innerHTML);\n"
375             + "</script>\n"
376             + "</body></html>";
377 
378         loadPageVerifyTitle2(html);
379     }
380 }