From 37ecce9ad343b53e9a94478866181080645bab78 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Sat, 7 May 2011 11:27:40 +0000 Subject: [PATCH] Fixes special characters when they are in quoted string: fixes issue77 --- .../client/impl/SelectorEngineCssToXPath.java | 18 +++++++++++++++++- .../query/rebind/SelectorGeneratorsTest.java | 5 +++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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 c2109dfe..fc80085c 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 @@ -52,6 +52,14 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl { private static SelectorEngineCssToXPath instance; + private static ReplaceCallback rc_scp = new ReplaceCallback() { + public String foundMatch(ArrayList s) { + return s.get(1) + s.get(2) + + (s.get(3).startsWith(" ") ? "%S%" : s.get(3).startsWith("#") ? "%H%" : "%P%") + + s.get(4) + s.get(5); + } + }; + private static ReplaceCallback rc_$Attr = new ReplaceCallback() { public String foundMatch(ArrayList s) { return "[substring(@" + s.get(1) + ",string-length(@" + s.get(1) + ")-" + (s.get(2).replaceAll("'", "").length() - 1) + ")=" + s.get(2) + "]"; @@ -86,6 +94,8 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl { }; private static Object[] regs = new Object[]{ + // scape some dots and spaces + "(['\\[])([^'\\]]+)([\\s\\.#])([^'\\]]+)(['\\]])", rc_scp, // add @ for attrib "\\[([^@\\]~\\$\\*\\^\\|\\!]+)(=[^\\]]+)?\\]", "[@$1$2]", // multiple queries @@ -136,7 +146,13 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl { ":(checked)", "[@$1='$1']", ":(disabled)", "[@$1]", // put '*' when tag is omitted - "(^|\\|)(\\[)", "$1*$2" + "(^|\\|)(\\[)", "$1*$2", + // Replace escaped dots and spaces + "%S%"," ", + "%P%",".", + "%H%","#", + // Duplicated quotes + "'+","'", }; public static SelectorEngineCssToXPath getInstance() { diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/rebind/SelectorGeneratorsTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/rebind/SelectorGeneratorsTest.java index 5dab3c86..26434b58 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/rebind/SelectorGeneratorsTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/rebind/SelectorGeneratorsTest.java @@ -70,6 +70,11 @@ public class SelectorGeneratorsTest extends GWTTestCase { assertEquals(".//*[@checked='checked']|*[not(@disabled)]|*[@disabled]", sel.css2Xpath(":checked, :enabled, :disabled")); + assertEquals(".//table[contains(string(.),'String With | @ ~= Space Points.s and Hash#es')]", + sel.css2Xpath("table:contains('String With | @ ~= Space Points.s and Hash#es')")); + + assertEquals(".//a[starts-with(@href,'http') and (contains(@href,'youtube.com/'))]", + sel.css2Xpath("a[href^=http][href*=youtube.com/]")); } public void testReplaceAll() { -- 2.39.5