Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Found a bug, should throw an error if the selector contains ++, +

  • Loading branch information...
commit a71e8e2452cefaf9bf39bfc70d78a6e144973350 1 parent 6114972
Joel Johnson authored
4 src/com/joelj/jcss/parse/CssUtils.java
@@ -17,6 +17,10 @@ public static String convertCssToXpath(String cssSelector) {
17 17 selector = selector.replaceAll("\\s*\\+\\s*", "+"); //remove padding around +
18 18 selector = selector.replaceAll("\\s+", " "); //normalize whitespace
19 19
  20 + if(selector.matches(".*?[\\>\\+]{2,}.*?")) {
  21 + throw new CssParseException("Invalid CSS Selector: ''" + cssSelector + "''");
  22 + }
  23 +
20 24 List<String> sections = split(selector);
21 25
22 26 StringBuilder sb = new StringBuilder("//");
11 test/com/joelj/jcss/dom/CssDocumentTest.java
@@ -87,4 +87,15 @@ public void testDocument_findElements_doesntExist() throws Exception {
87 87 Assert.assertEquals(elements.size(), 0);
88 88 }
89 89
  90 + @Test
  91 + public void testDocument_findOnlyByAttribute() throws CssExpressionException {
  92 + List<CssNode> elements = document.findElements(".class1");
  93 + Assert.assertEquals(elements.size(), 2);
  94 +
  95 + elements = document.findElements("#anId");
  96 + Assert.assertEquals(elements.size(), 1);
  97 +
  98 + elements = document.findElements(".class2 > .class1");
  99 + Assert.assertEquals(elements.size(), 1);
  100 + }
90 101 }
28 test/com/joelj/jcss/parse/CssUtilsTest.java
... ... @@ -1,6 +1,7 @@
1 1 package com.joelj.jcss.parse;
2 2
3 3 import com.joelj.DelayedAssert;
  4 +import com.joelj.jcss.dom.CssExpressionException;
4 5 import org.testng.Assert;
5 6 import org.testng.annotations.Test;
6 7
@@ -200,4 +201,31 @@ public void testSplit() {
200 201
201 202 DelayedAssert.assertDelayed();
202 203 }
  204 +
  205 + @Test
  206 + public void testDocument_malformed() throws CssExpressionException {
  207 + try {
  208 + CssUtils.convertCssToXpath(".class2>>.class1");
  209 + Assert.fail("Should throw an exception CssParseException.");
  210 + } catch(CssParseException ignore) {
  211 + }
  212 +
  213 + try {
  214 + CssUtils.convertCssToXpath(".class2++.class1");
  215 + Assert.fail("Should throw an exception CssParseException.");
  216 + } catch(CssParseException ignore) {
  217 + }
  218 +
  219 + try {
  220 + CssUtils.convertCssToXpath(".class2+>.class1");
  221 + Assert.fail("Should throw an exception CssParseException.");
  222 + } catch(CssParseException ignore) {
  223 + }
  224 +
  225 + try {
  226 + CssUtils.convertCssToXpath(".class2>+.class1");
  227 + Assert.fail("Should throw an exception CssParseException.");
  228 + } catch(CssParseException ignore) {
  229 + }
  230 + }
203 231 }

0 comments on commit a71e8e2

Please sign in to comment.
Something went wrong with that request. Please try again.