Skip to content

Commit fdfbac2

Browse files
SnipxiText-CI
authored andcommitted
Fix issues with number filtering for <input type="number">
DEVSIX-4198 Autoported commit. Original commit hash: [696e9b58]
1 parent cd76580 commit fdfbac2

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed
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+
using iText.Html2pdf.Html;
24+
using iText.Test;
25+
26+
namespace iText.Html2pdf.Attach.Impl.Tags {
27+
public class InputTagWorkerTest : ExtendedITextTest {
28+
[NUnit.Framework.Test]
29+
public virtual void TestNumberInputPreprocessing() {
30+
NUnit.Framework.Assert.AreEqual("", InputTagWorker.PreprocessInputValue("bear", AttributeConstants.NUMBER)
31+
);
32+
NUnit.Framework.Assert.AreEqual("4", InputTagWorker.PreprocessInputValue("4", AttributeConstants.NUMBER));
33+
NUnit.Framework.Assert.AreEqual("04", InputTagWorker.PreprocessInputValue("04", AttributeConstants.NUMBER)
34+
);
35+
NUnit.Framework.Assert.AreEqual("", InputTagWorker.PreprocessInputValue("4.", AttributeConstants.NUMBER));
36+
NUnit.Framework.Assert.AreEqual("", InputTagWorker.PreprocessInputValue(".", AttributeConstants.NUMBER));
37+
NUnit.Framework.Assert.AreEqual(".9", InputTagWorker.PreprocessInputValue(".9", AttributeConstants.NUMBER)
38+
);
39+
NUnit.Framework.Assert.AreEqual("4.4", InputTagWorker.PreprocessInputValue("4.4", AttributeConstants.NUMBER
40+
));
41+
NUnit.Framework.Assert.AreEqual("-4", InputTagWorker.PreprocessInputValue("-4", AttributeConstants.NUMBER)
42+
);
43+
NUnit.Framework.Assert.AreEqual("", InputTagWorker.PreprocessInputValue("", AttributeConstants.NUMBER));
44+
NUnit.Framework.Assert.AreEqual("", InputTagWorker.PreprocessInputValue("-", AttributeConstants.NUMBER));
45+
NUnit.Framework.Assert.AreEqual("-.9", InputTagWorker.PreprocessInputValue("-.9", AttributeConstants.NUMBER
46+
));
47+
NUnit.Framework.Assert.AreEqual("", InputTagWorker.PreprocessInputValue("0-.9", AttributeConstants.NUMBER)
48+
);
49+
}
50+
}
51+
}

itext/itext.html2pdf/itext/html2pdf/attach/impl/tags/InputTagWorker.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ source product.
4141
4242
*/
4343
using System;
44+
using System.Text.RegularExpressions;
4445
using Common.Logging;
4546
using iText.Html2pdf.Attach;
4647
using iText.Html2pdf.Attach.Impl.Layout;
@@ -60,6 +61,9 @@ namespace iText.Html2pdf.Attach.Impl.Tags {
6061
/// element.
6162
/// </summary>
6263
public class InputTagWorker : ITagWorker, IDisplayAware {
64+
private static readonly Regex NUMBER_INPUT_ALLOWED_VALUES = iText.IO.Util.StringUtil.RegexCompile("^(((-?[0-9]+)(\\.[0-9]+)?)|(-?\\.[0-9]+))$"
65+
);
66+
6367
/// <summary>The form element.</summary>
6468
private IElement formElement;
6569

@@ -190,8 +194,9 @@ public virtual IPropertyContainer GetElementResult() {
190194
return formElement;
191195
}
192196

193-
private static String PreprocessInputValue(String value, String inputType) {
194-
if (AttributeConstants.NUMBER.Equals(inputType) && value != null && !value.Matches("[0-9.]*")) {
197+
internal static String PreprocessInputValue(String value, String inputType) {
198+
if (AttributeConstants.NUMBER.Equals(inputType) && value != null && !iText.IO.Util.StringUtil.Match(NUMBER_INPUT_ALLOWED_VALUES
199+
, value).Success) {
195200
value = "";
196201
}
197202
return value;

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
90ad4596fcb7988917ccd8c52bf6658bc48693bd
1+
696e9b5872fb5120ee80f337552ad78d21dc000c

0 commit comments

Comments
 (0)