Skip to content

Commit ef778b4

Browse files
committed
Cover invalid selector with some tests
DEVSIX-6158
1 parent 06dd11d commit ef778b4

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/parse/CssSelectorParser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This file is part of the iText (R) project.
4343
package com.itextpdf.styledxmlparser.css.parse;
4444

4545
import com.itextpdf.commons.utils.MessageFormatUtil;
46+
import com.itextpdf.styledxmlparser.exceptions.StyledXmlParserExceptionMessage;
4647
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
4748
import com.itextpdf.styledxmlparser.css.selector.item.CssAttributeSelectorItem;
4849
import com.itextpdf.styledxmlparser.css.selector.item.CssClassSelectorItem;
@@ -129,7 +130,9 @@ public static List<ICssSelectorItem> parseSelectorItems(String selector) {
129130
case '>':
130131
case '~':
131132
if (selectorItems.size() == 0) {
132-
throw new IllegalArgumentException(MessageFormatUtil.format("Invalid token detected in the start of the selector string: {0}", firstChar));
133+
throw new IllegalArgumentException(MessageFormatUtil.format(
134+
StyledXmlParserExceptionMessage.INVALID_TOKEN_AT_THE_BEGINNING_OF_SELECTOR,
135+
firstChar));
133136
}
134137
ICssSelectorItem lastItem = selectorItems.get(selectorItems.size() - 1);
135138
CssSeparatorSelectorItem curItem = new CssSeparatorSelectorItem(firstChar);

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/exceptions/StyledXmlParserExceptionMessage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ This file is part of the iText (R) project.
2626
* Class containing constants to be used in exceptions in the SXP module.
2727
*/
2828
public final class StyledXmlParserExceptionMessage {
29+
public static final String INVALID_TOKEN_AT_THE_BEGINNING_OF_SELECTOR
30+
= "Invalid token detected at the beginning of the selector string: \"{0}\"";
2931
public static final String READING_BYTE_LIMIT_MUST_NOT_BE_LESS_ZERO = "The reading byte limit argument must not be less than zero.";
3032

3133
private StyledXmlParserExceptionMessage() {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
package com.itextpdf.styledxmlparser.css.parse;
24+
25+
import com.itextpdf.commons.utils.MessageFormatUtil;
26+
import com.itextpdf.io.exceptions.IOException;
27+
import com.itextpdf.styledxmlparser.exceptions.StyledXmlParserExceptionMessage;
28+
import com.itextpdf.test.ExtendedITextTest;
29+
import com.itextpdf.test.annotations.type.UnitTest;
30+
31+
import org.junit.Assert;
32+
import org.junit.Test;
33+
import org.junit.experimental.categories.Category;
34+
35+
@Category(UnitTest.class)
36+
public class CssSelectorParserTest extends ExtendedITextTest {
37+
38+
@Test
39+
public void selectorBeginsWithSpaceTest() throws IOException {
40+
String space = " ";
41+
String selectorWithSpaceAtTheBeginning = space + ".spaceBefore";
42+
43+
Exception expectedException = Assert.assertThrows(IllegalArgumentException.class,
44+
() -> CssSelectorParser.parseSelectorItems(selectorWithSpaceAtTheBeginning));
45+
Assert.assertEquals(
46+
MessageFormatUtil.format(StyledXmlParserExceptionMessage.INVALID_TOKEN_AT_THE_BEGINNING_OF_SELECTOR,
47+
space),
48+
expectedException.getMessage());
49+
}
50+
}

0 commit comments

Comments
 (0)