aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Carrasco <manolo@apache.org>2013-12-02 07:57:32 -0800
committerManuel Carrasco <manolo@apache.org>2013-12-02 07:57:32 -0800
commitfc06a88c4e70fe444b1487646166fc430a2a2b0b (patch)
tree4447947cce26bb27b993084ca8e2223dd0411cc3
parent26e800d9f2328fc3dd1beb9a8ec4a1c3815134be (diff)
parentd1a13c80b3688494f332970bce8783b763de1e2d (diff)
downloadgwtquery-fc06a88c4e70fe444b1487646166fc430a2a2b0b.tar.gz
gwtquery-fc06a88c4e70fe444b1487646166fc430a2a2b0b.zip
Merge pull request #244 from manolo/mcm_issue_26
Fix for issue 26
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java15
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java16
2 files changed, 18 insertions, 13 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java
index e5ec73a6..a78c1b5d 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java
@@ -78,27 +78,29 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl {
String s1 = s.get(1);
String s2 = s.get(2);
- boolean noPrefix = s1 == null || s1.length() == 0;
+ boolean afterAttr = "]".equals(s1);
+ String prefix = afterAttr ? s1 : "*";
+ boolean noPrefix = afterAttr || s1 == null || s1.length() == 0 ;
if ("n".equals(s2)) {
return s1;
}
if ("even".equals(s2)) {
- return "*[position() mod 2=0 and position()>=0]" + (noPrefix ? "" : "/self::" + s1);
+ return prefix + "[position() mod 2=0 and position()>=0]" + (noPrefix ? "" : "/self::" + s1);
}
if ("odd".equals(s2)) {
- String prefix = noPrefix ? "" : s1;
+ prefix = afterAttr ? prefix : noPrefix ? "" : s1;
return prefix + "[(count(preceding-sibling::*) + 1) mod 2=1]";
}
if (!s2.contains("n")){
- return "*[position() = "+s2+"]" + (noPrefix ? "" : "/self::" + s1);
+ return prefix + "[position() = "+s2+"]" + (noPrefix ? "" : "/self::" + s1);
}
String[] t = s2.replaceAll("^([0-9]*)n.*?([0-9]*)?$", "$1+$2").split("\\+");
String t0 = t[0];
String t1 = t.length > 1 ? t[1] : "0";
- return "*[(position()-" + t1 + ") mod " + t0 + "=0 and position()>=" + t1 + "]" + (noPrefix ? "" : "/self::" + s1);
+ return prefix + "[(position()-" + t1 + ") mod " + t0 + "=0 and position()>=" + t1 + "]" + (noPrefix ? "" : "/self::" + s1);
}
};
@@ -135,7 +137,7 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl {
// :not
"(.+):not\\(([^\\)]*)\\)", rc_Not,
// :nth-child
- "([a-zA-Z0-9\\_\\-\\*]*):nth-child\\(([^\\)]*)\\)", rc_nth_child,
+ "([a-zA-Z0-9\\_\\-\\*]*|\\]):nth-child\\(([^\\)]*)\\)", rc_nth_child,
// :contains(selectors)
":contains\\(([^\\)]*)\\)", "[contains(string(.),'$1')]",
// |= attrib
@@ -270,5 +272,4 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl {
return elm;
}
}
-
}
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java
index efd8953d..08897f56 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java
@@ -15,17 +15,13 @@
*/
package com.google.gwt.query.client;
-import static com.google.gwt.query.client.GQuery.$;
-import static com.google.gwt.query.client.GQuery.body;
-import static com.google.gwt.query.client.GQuery.document;
+import static com.google.gwt.query.client.GQuery.*;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
-import com.google.gwt.junit.DoNotRunWith;
-import com.google.gwt.junit.Platform;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.query.client.impl.SelectorEngineCssToXPath;
import com.google.gwt.query.client.impl.SelectorEngineImpl;
@@ -407,7 +403,7 @@ public class GQuerySelectorsTestGwt extends GWTTestCase {
public void testOddEvenNthChild(){
$(e).append(
- "<div id='parent'><div id='first' class='evenClass'>branchA target</div><div id='second' class='oddClass'>branchA target</div><div id='third' class='evenClass'>branchA target</div><div id='fourth' class='oddClass'>branchA target</div></div>");
+ "<div id='parent'><div id='first' class='evenClass' role='treeItem'>branchA target</div><div id='second' class='oddClass' role='treeItem'>branchA target</div><div id='third' class='evenClass' role='treeItem'>branchA target</div><div id='fourth' class='oddClass' role='treeItem'>branchA target</div></div>");
GQuery odd = $("#parent > div:odd",e);
assertEquals(2, odd.size());
@@ -449,6 +445,14 @@ public class GQuerySelectorsTestGwt extends GWTTestCase {
assertEquals("second", secondAndFourth.eq(0).attr("id"));
assertEquals("fourth", secondAndFourth.eq(1).attr("id"));
+ // odd and even with attribute filters
+ secondAndFourth = $("div[role=treeItem]:odd", e);
+ assertEquals("second", secondAndFourth.eq(0).attr("id"));
+ assertEquals("fourth", secondAndFourth.eq(1).attr("id"));
+
+ GQuery treeItemFirstAndThird = $("div[role=treeItem]:even", e);
+ assertEquals("first", treeItemFirstAndThird.eq(0).attr("id"));
+ assertEquals("third", treeItemFirstAndThird.eq(1).attr("id"));
}
private void assertArrayContains(Object result, Object... array) {