Skip to content

Commit 696e9b5

Browse files
committedJun 30, 2020
Fix issues with number filtering for <input type="number">
DEVSIX-4198
·
6.2.03.0.1
1 parent 90ad459 commit 696e9b5

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed
 

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ This file is part of the iText (R) project.
5858
import com.itextpdf.html2pdf.html.AttributeConstants;
5959
import com.itextpdf.styledxmlparser.css.util.CssUtils;
6060
import com.itextpdf.styledxmlparser.node.IElementNode;
61+
62+
import java.util.regex.Pattern;
6163
import org.slf4j.Logger;
6264
import org.slf4j.LoggerFactory;
6365

@@ -66,6 +68,9 @@ This file is part of the iText (R) project.
6668
*/
6769
public class InputTagWorker implements ITagWorker, IDisplayAware {
6870

71+
private static final Pattern NUMBER_INPUT_ALLOWED_VALUES =
72+
Pattern.compile("^(((-?[0-9]+)(\\.[0-9]+)?)|(-?\\.[0-9]+))$");
73+
6974
/**
7075
* The form element.
7176
*/
@@ -190,8 +195,9 @@ public IPropertyContainer getElementResult() {
190195
return formElement;
191196
}
192197

193-
private static String preprocessInputValue(String value, String inputType) {
194-
if (AttributeConstants.NUMBER.equals(inputType) && value != null && !value.matches("[0-9.]*")) {
198+
static String preprocessInputValue(String value, String inputType) {
199+
if (AttributeConstants.NUMBER.equals(inputType) && value != null &&
200+
!NUMBER_INPUT_ALLOWED_VALUES.matcher(value).matches()) {
195201
value = "";
196202
}
197203
return value;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2020 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.html2pdf.attach.impl.tags;
24+
25+
import com.itextpdf.html2pdf.html.AttributeConstants;
26+
import com.itextpdf.test.ExtendedITextTest;
27+
import com.itextpdf.test.annotations.type.UnitTest;
28+
29+
import org.junit.Assert;
30+
import org.junit.Test;
31+
import org.junit.experimental.categories.Category;
32+
33+
@Category(UnitTest.class)
34+
public class InputTagWorkerTest extends ExtendedITextTest {
35+
36+
@Test
37+
public void testNumberInputPreprocessing() {
38+
Assert.assertEquals("", InputTagWorker.preprocessInputValue("bear", AttributeConstants.NUMBER));
39+
Assert.assertEquals("4", InputTagWorker.preprocessInputValue("4", AttributeConstants.NUMBER));
40+
Assert.assertEquals("04", InputTagWorker.preprocessInputValue("04", AttributeConstants.NUMBER));
41+
Assert.assertEquals("", InputTagWorker.preprocessInputValue("4.", AttributeConstants.NUMBER));
42+
Assert.assertEquals("", InputTagWorker.preprocessInputValue(".", AttributeConstants.NUMBER));
43+
Assert.assertEquals(".9", InputTagWorker.preprocessInputValue(".9", AttributeConstants.NUMBER));
44+
Assert.assertEquals("4.4", InputTagWorker.preprocessInputValue("4.4", AttributeConstants.NUMBER));
45+
Assert.assertEquals("-4", InputTagWorker.preprocessInputValue("-4", AttributeConstants.NUMBER));
46+
Assert.assertEquals("", InputTagWorker.preprocessInputValue("", AttributeConstants.NUMBER));
47+
Assert.assertEquals("", InputTagWorker.preprocessInputValue("-", AttributeConstants.NUMBER));
48+
Assert.assertEquals("-.9", InputTagWorker.preprocessInputValue("-.9", AttributeConstants.NUMBER));
49+
Assert.assertEquals("", InputTagWorker.preprocessInputValue("0-.9", AttributeConstants.NUMBER));
50+
}
51+
}

0 commit comments

Comments
 (0)
Please sign in to comment.