Skip to content

Commit d3a694b

Browse files
committed
Process <input type="number"> correctly
1 parent 0dffa42 commit d3a694b

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

src/main/java/com/itextpdf/html2pdf/attach/impl/tags/InputTagWorker.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public InputTagWorker(IElementNode element, ProcessorContext context) {
8181
String name = context.getFormFieldNameResolver().resolveFormName(element.getAttribute(AttributeConstants.NAME));
8282
// Default input type is text
8383
if (inputType == null || AttributeConstants.TEXT.equals(inputType) || AttributeConstants.EMAIL.equals(inputType)
84-
|| AttributeConstants.PASSWORD.equals(inputType)) {
84+
|| AttributeConstants.PASSWORD.equals(inputType) || AttributeConstants.NUMBER.equals(inputType)) {
8585
Integer size = CssUtils.parseInteger(element.getAttribute(AttributeConstants.SIZE));
8686
formElement = new InputField(name);
87-
formElement.setProperty(Html2PdfProperty.FORM_FIELD_VALUE, value);
87+
formElement.setProperty(Html2PdfProperty.FORM_FIELD_VALUE, preprocessInputValue(value, inputType));
8888
formElement.setProperty(Html2PdfProperty.FORM_FIELD_SIZE, size);
8989
if (AttributeConstants.PASSWORD.equals(inputType)) {
9090
formElement.setProperty(Html2PdfProperty.FORM_FIELD_PASSWORD_FLAG, true);
@@ -141,4 +141,12 @@ public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext conte
141141
public IPropertyContainer getElementResult() {
142142
return formElement;
143143
}
144+
145+
private static String preprocessInputValue(String value, String inputType) {
146+
if (AttributeConstants.NUMBER.equals(inputType) && value != null && !value.matches("[0-9.]*")) {
147+
value = "";
148+
}
149+
return value;
150+
}
151+
144152
}

src/main/java/com/itextpdf/html2pdf/html/AttributeConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ private AttributeConstants() {
124124
/** The Constant NOSHADE. */
125125
public static final String NOSHADE = "noshade";
126126

127+
/** The Constant NUMBER. */
128+
public static final String NUMBER = "number";
129+
127130
/** The Constant REL. */
128131
public static final String REL = "rel";
129132

src/test/java/com/itextpdf/html2pdf/element/InputTest.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ This file is part of the iText (R) project.
4646
import com.itextpdf.kernel.utils.CompareTool;
4747
import com.itextpdf.test.ExtendedITextTest;
4848
import com.itextpdf.test.annotations.type.IntegrationTest;
49-
50-
import java.io.File;
51-
import java.io.IOException;
52-
5349
import org.junit.Assert;
5450
import org.junit.BeforeClass;
5551
import org.junit.Test;
5652
import org.junit.experimental.categories.Category;
5753

54+
import java.io.File;
55+
import java.io.IOException;
56+
5857
@Category(IntegrationTest.class)
5958
public class InputTest extends ExtendedITextTest {
6059

@@ -68,20 +67,28 @@ public static void beforeClass() {
6867

6968
@Test
7069
public void input01Test() throws IOException, InterruptedException {
71-
HtmlConverter.convertToPdf(new File(sourceFolder + "inputTest01.html"), new File(destinationFolder + "inputTest01.pdf"));
72-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "inputTest01.pdf", sourceFolder + "cmp_inputTest01.pdf", destinationFolder, "diff01_"));
70+
runTest("inputTest01");
7371
}
7472

7573
@Test
7674
public void input02Test() throws IOException, InterruptedException {
77-
HtmlConverter.convertToPdf(new File(sourceFolder + "inputTest02.html"), new File(destinationFolder + "inputTest02.pdf"));
78-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "inputTest02.pdf", sourceFolder + "cmp_inputTest02.pdf", destinationFolder, "diff02_"));
75+
runTest("inputTest02");
7976
}
8077

8178
@Test
8279
public void input03Test() throws IOException, InterruptedException {
83-
HtmlConverter.convertToPdf(new File(sourceFolder + "inputTest03.html"), new File(destinationFolder + "inputTest03.pdf"));
84-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "inputTest03.pdf", sourceFolder + "cmp_inputTest03.pdf", destinationFolder, "diff03_"));
80+
runTest("inputTest03");
8581
}
8682

83+
@Test
84+
public void input04Test() throws IOException, InterruptedException {
85+
runTest("inputTest04");
86+
}
87+
88+
private void runTest(String testName) throws IOException, InterruptedException {
89+
HtmlConverter.convertToPdf(new File(sourceFolder + testName + ".html"), new File(destinationFolder + testName + ".pdf"));
90+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, "diff_" + testName));
91+
}
92+
93+
8794
}
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style>
5+
input {
6+
color: red;
7+
}
8+
</style>
9+
</head>
10+
<body>
11+
12+
Quantity: <input type="number" name="test1" value="12345.67890"><br>
13+
Invalid quantity: <input type="number" name="test2" value="Mouse"><br>
14+
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)