Skip to content

Commit 4450268

Browse files
ars18wrwiText-CI
authored andcommitted
Cover invalid selector with some tests
DEVSIX-6158 Autoported commit. Original commit hash: [ef778b41e]
1 parent bba113a commit 4450268

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 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 System;
24+
using iText.Commons.Utils;
25+
using iText.StyledXmlParser.Exceptions;
26+
using iText.Test;
27+
28+
namespace iText.StyledXmlParser.Css.Parse {
29+
public class CssSelectorParserTest : ExtendedITextTest {
30+
[NUnit.Framework.Test]
31+
public virtual void SelectorBeginsWithSpaceTest() {
32+
String space = " ";
33+
String selectorWithSpaceAtTheBeginning = space + ".spaceBefore";
34+
Exception expectedException = NUnit.Framework.Assert.Catch(typeof(ArgumentException), () => CssSelectorParser
35+
.ParseSelectorItems(selectorWithSpaceAtTheBeginning));
36+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(StyledXmlParserExceptionMessage.INVALID_TOKEN_AT_THE_BEGINNING_OF_SELECTOR
37+
, space), expectedException.Message);
38+
}
39+
}
40+
}

itext/itext.styledxmlparser/itext/styledxmlparser/css/parse/CssSelectorParser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ source product.
4646
using iText.Commons.Utils;
4747
using iText.StyledXmlParser.Css.Selector.Item;
4848
using iText.StyledXmlParser.Css.Util;
49+
using iText.StyledXmlParser.Exceptions;
4950

5051
namespace iText.StyledXmlParser.Css.Parse {
5152
/// <summary>Utilities class to parse a CSS selector.</summary>
@@ -120,7 +121,7 @@ public static IList<ICssSelectorItem> ParseSelectorItems(String selector) {
120121
case '>':
121122
case '~': {
122123
if (selectorItems.Count == 0) {
123-
throw new ArgumentException(MessageFormatUtil.Format("Invalid token detected in the start of the selector string: {0}"
124+
throw new ArgumentException(MessageFormatUtil.Format(StyledXmlParserExceptionMessage.INVALID_TOKEN_AT_THE_BEGINNING_OF_SELECTOR
124125
, firstChar));
125126
}
126127
ICssSelectorItem lastItem = selectorItems[selectorItems.Count - 1];

itext/itext.styledxmlparser/itext/styledxmlparser/exceptions/StyledXmlParserExceptionMessage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ You should have received a copy of the GNU Affero General Public License
2525
namespace iText.StyledXmlParser.Exceptions {
2626
/// <summary>Class containing constants to be used in exceptions in the SXP module.</summary>
2727
public sealed class StyledXmlParserExceptionMessage {
28+
public const String INVALID_TOKEN_AT_THE_BEGINNING_OF_SELECTOR = "Invalid token detected at the beginning of the selector string: \"{0}\"";
29+
2830
public const String READING_BYTE_LIMIT_MUST_NOT_BE_LESS_ZERO = "The reading byte limit argument must not be less than zero.";
2931

3032
private StyledXmlParserExceptionMessage() {

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
06dd11de03686fefd5ebd4d4d714550250cd3316
1+
ef778b41e56e96fea5aedf97a544866e7fe6cbb6

0 commit comments

Comments
 (0)