Skip to content

Commit 88bc826

Browse files
committed
invalid numbers are processed correct now when using the rowSpan/colSpan properties
1 parent eecf51d commit 88bc826

4 files changed

Lines changed: 165 additions & 21 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
Jetty 9 websocket-client.
1919
</action>
2020

21+
<action type="fix" dev="Lai Quang Duong">
22+
Invalid numbers are processed correct now when using the rowSpan/colSpan properties.
23+
</action>
2124
<action type="fix" dev="Lai Quang Duong">
2225
Whitespace gets removed from rowspan/colspan attributes when using rowSpan/colSpan properties.
2326
</action>

src/main/java/org/htmlunit/html/HtmlTableCell.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
package org.htmlunit.html;
1616

17+
import static org.htmlunit.BrowserVersionFeatures.JS_TABLE_SPAN_SET_ZERO_IF_INVALID;
18+
1719
import java.util.Map;
1820

1921
import org.apache.commons.lang3.StringUtils;
@@ -46,33 +48,56 @@ protected HtmlTableCell(final String qualifiedName, final SgmlPage page,
4648
}
4749

4850
/**
49-
* Returns the value of the colspan attribute, or <code>1</code> if the attribute wasn't specified.
5051
* @return the value of the colspan attribute, or <code>1</code> if the attribute wasn't specified
5152
*/
5253
public int getColumnSpan() {
53-
final String spanString = StringUtils.replaceChars(getAttributeDirect("colspan"), "\r\n", null);
54+
final String spanString = StringUtils.replaceChars(getAttributeDirect("colspan"), "\r\n\t ", null);
5455
if (spanString == null || spanString.isEmpty()) {
5556
return 1;
5657
}
5758
try {
58-
return Integer.parseInt(spanString);
59+
final int span = (int) Double.parseDouble(spanString);
60+
if (span < 1) {
61+
return 1;
62+
}
63+
if (span > 1000) {
64+
return 1000;
65+
}
66+
return span;
5967
}
6068
catch (final NumberFormatException e) {
6169
return 1;
6270
}
6371
}
6472

6573
/**
66-
* Returns the value of the rowspan attribute, or <code>1</code> if the attribute wasn't specified.
6774
* @return the value of the rowspan attribute, or <code>1</code> if the attribute wasn't specified
6875
*/
6976
public int getRowSpan() {
70-
final String spanString = StringUtils.replaceChars(getAttributeDirect("rowspan"), "\r\n", null);
77+
final String spanString = StringUtils.replaceChars(getAttributeDirect("rowspan"), "\r\n\t ", null);
7178
if (spanString == null || spanString.isEmpty()) {
7279
return 1;
7380
}
7481
try {
75-
return Integer.parseInt(spanString);
82+
final int span = (int) Double.parseDouble(spanString);
83+
if (getPage().getWebClient().getBrowserVersion().hasFeature(JS_TABLE_SPAN_SET_ZERO_IF_INVALID)) {
84+
if (span < 0) {
85+
return 1;
86+
}
87+
if (span < 1) {
88+
return 0;
89+
}
90+
}
91+
else {
92+
if (span < 1) {
93+
return 1;
94+
}
95+
}
96+
97+
if (span > 65534) {
98+
return 65534;
99+
}
100+
return span;
76101
}
77102
catch (final NumberFormatException e) {
78103
return 1;

src/main/java/org/htmlunit/javascript/host/html/HTMLTableCellElement.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import static org.htmlunit.BrowserVersionFeatures.JS_TABLE_SPAN_SET_ZERO_IF_INVALID;
1818

19-
import org.apache.commons.lang3.StringUtils;
2019
import org.htmlunit.css.ComputedCssStyleDeclaration;
2120
import org.htmlunit.css.StyleAttributes;
2221
import org.htmlunit.html.DomNode;
@@ -179,13 +178,7 @@ public void setBgColor(final String bgColor) {
179178
*/
180179
@JsxGetter
181180
public int getColSpan() {
182-
final String s = StringUtils.replaceChars(getDomNodeOrDie().getAttribute("colSpan"), "\r\n\t ", null);
183-
try {
184-
return Integer.parseInt(s);
185-
}
186-
catch (final NumberFormatException e) {
187-
return 1;
188-
}
181+
return ((HtmlTableCell) getDomNodeOrDie()).getColumnSpan();
189182
}
190183

191184
/**
@@ -212,13 +205,7 @@ public void setColSpan(final String colSpan) {
212205
*/
213206
@JsxGetter
214207
public int getRowSpan() {
215-
final String s = StringUtils.replaceChars(getDomNodeOrDie().getAttribute("rowSpan"), "\r\n\t ", null);
216-
try {
217-
return Integer.parseInt(s);
218-
}
219-
catch (final NumberFormatException e) {
220-
return 1;
221-
}
208+
return ((HtmlTableCell) getDomNodeOrDie()).getRowSpan();
222209
}
223210

224211
/**

src/test/java/org/htmlunit/javascript/host/html/HTMLTableCellElementTest.java

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,68 @@ public void colSpanLineBreaks() throws Exception {
350350
loadPageVerifyTitle2(html);
351351
}
352352

353+
/**
354+
* @throws Exception if an error occurs
355+
*/
356+
@Test
357+
@Alerts({"1", "1", "3", "3", "3"})
358+
public void colSpanInvalid() throws Exception {
359+
final String html
360+
= "<html><body><table>\n"
361+
+ " <tr>\n"
362+
+ " <td id='td1' colspan='-1'>b</td>\n"
363+
+ " <td id='td2' colspan='0'>b</td>\n"
364+
+ " <td id='td3' colspan='3.14'>b</td>\n"
365+
+ " <td id='td4' colspan='3.5'>b</td>\n"
366+
+ " <td id='td5' colspan='3.7'>b</td>\n"
367+
+ " </tr>\n"
368+
+ "</table>\n"
369+
+ "<script>\n"
370+
+ LOG_TITLE_FUNCTION_NORMALIZE
371+
+ " var td1 = document.getElementById('td1');\n"
372+
+ " var td2 = document.getElementById('td2');\n"
373+
+ " var td3 = document.getElementById('td3');\n"
374+
+ " var td4 = document.getElementById('td4');\n"
375+
+ " var td5 = document.getElementById('td5');\n"
376+
+ " log(td1.colSpan);\n"
377+
+ " log(td2.colSpan);\n"
378+
+ " log(td3.colSpan);\n"
379+
+ " log(td4.colSpan);\n"
380+
+ " log(td5.colSpan);\n"
381+
+ "</script>\n"
382+
+ "</body></html>";
383+
384+
loadPageVerifyTitle2(html);
385+
}
386+
387+
/**
388+
* @throws Exception if an error occurs
389+
*/
390+
@Test
391+
@Alerts({"999", "1000", "1000"})
392+
public void colSpanLarge() throws Exception {
393+
final String html
394+
= "<html><body><table>\n"
395+
+ " <tr>\n"
396+
+ " <td id='td1' colspan='999'>b</td>\n"
397+
+ " <td id='td2' colspan='1000'>b</td>\n"
398+
+ " <td id='td3' colspan='1001'>b</td>\n"
399+
+ " </tr>\n"
400+
+ "</table>\n"
401+
+ "<script>\n"
402+
+ LOG_TITLE_FUNCTION_NORMALIZE
403+
+ " var td1 = document.getElementById('td1');\n"
404+
+ " var td2 = document.getElementById('td2');\n"
405+
+ " var td3 = document.getElementById('td3');\n"
406+
+ " log(td1.colSpan);\n"
407+
+ " log(td2.colSpan);\n"
408+
+ " log(td3.colSpan);\n"
409+
+ "</script>\n"
410+
+ "</body></html>";
411+
412+
loadPageVerifyTitle2(html);
413+
}
414+
353415
/**
354416
* @throws Exception if an error occurs
355417
*/
@@ -426,6 +488,73 @@ public void rowSpanLineBreaks() throws Exception {
426488
loadPageVerifyTitle2(html);
427489
}
428490

491+
/**
492+
* @throws Exception if an error occurs
493+
*/
494+
@Test
495+
@Alerts(DEFAULT = {"1", "0", "3", "3", "3"},
496+
FF = {"1", "1", "3", "3", "3"},
497+
FF_ESR = {"1", "1", "3", "3", "3"})
498+
public void rowSpanInvalid() throws Exception {
499+
final String html
500+
= "<html><body><table>\n"
501+
+ " <tr>\n"
502+
+ " <td id='td1' rowspan='-1'>b</td>\n"
503+
+ " <td id='td2' rowspan='0'>b</td>\n"
504+
+ " <td id='td3' rowspan='3.14'>b</td>\n"
505+
+ " <td id='td4' rowspan='3.5'>b</td>\n"
506+
+ " <td id='td5' rowspan='3.7'>b</td>\n"
507+
+ " </tr>\n"
508+
+ "</table>\n"
509+
+ "<script>\n"
510+
+ LOG_TITLE_FUNCTION_NORMALIZE
511+
+ " var td1 = document.getElementById('td1');\n"
512+
+ " var td2 = document.getElementById('td2');\n"
513+
+ " var td3 = document.getElementById('td3');\n"
514+
+ " var td4 = document.getElementById('td4');\n"
515+
+ " var td5 = document.getElementById('td5');\n"
516+
+ " log(td1.rowSpan);\n"
517+
+ " log(td2.rowSpan);\n"
518+
+ " log(td3.rowSpan);\n"
519+
+ " log(td4.rowSpan);\n"
520+
+ " log(td5.rowSpan);\n"
521+
+ "</script>\n"
522+
+ "</body></html>";
523+
524+
loadPageVerifyTitle2(html);
525+
}
526+
527+
/**
528+
* @throws Exception if an error occurs
529+
*/
530+
@Test
531+
@Alerts({"999", "1001", "65534", "65534"})
532+
public void rowSpanLarge() throws Exception {
533+
final String html
534+
= "<html><body><table>\n"
535+
+ " <tr>\n"
536+
+ " <td id='td1' rowspan='999'>b</td>\n"
537+
+ " <td id='td2' rowspan='1001'>b</td>\n"
538+
+ " <td id='td3' rowspan='65534'>b</td>\n"
539+
+ " <td id='td4' rowspan='65535'>b</td>\n"
540+
+ " </tr>\n"
541+
+ "</table>\n"
542+
+ "<script>\n"
543+
+ LOG_TITLE_FUNCTION_NORMALIZE
544+
+ " var td1 = document.getElementById('td1');\n"
545+
+ " var td2 = document.getElementById('td2');\n"
546+
+ " var td3 = document.getElementById('td3');\n"
547+
+ " var td4 = document.getElementById('td4');\n"
548+
+ " log(td1.rowSpan);\n"
549+
+ " log(td2.rowSpan);\n"
550+
+ " log(td3.rowSpan);\n"
551+
+ " log(td4.rowSpan);\n"
552+
+ "</script>\n"
553+
+ "</body></html>";
554+
555+
loadPageVerifyTitle2(html);
556+
}
557+
429558
/**
430559
* @throws Exception if an error occurs
431560
*/

0 commit comments

Comments
 (0)