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