From: <mgu...@us...> - 2013-03-21 16:06:33
|
Revision: 8183 http://sourceforge.net/p/htmlunit/code/8183 Author: mguillem Date: 2013-03-21 16:06:29 +0000 (Thu, 21 Mar 2013) Log Message: ----------- CSS selector: handle escaped characters in class name Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-03-21 15:39:23 UTC (rev 8182) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-03-21 16:06:29 UTC (rev 8183) @@ -464,7 +464,7 @@ return ac4.getValue().equals(element.getId()); case Condition.SAC_CLASS_CONDITION: final AttributeCondition ac3 = (AttributeCondition) condition; - final String v3 = ac3.getValue(); + final String v3 = unescape(ac3.getValue()); final String a3 = element.getAttribute("class"); return selects(v3, a3, ' '); case Condition.SAC_AND_CONDITION: @@ -474,10 +474,7 @@ case Condition.SAC_ATTRIBUTE_CONDITION: final AttributeCondition ac1 = (AttributeCondition) condition; if (ac1.getSpecified()) { - String value = ac1.getValue(); - if (value.contains("\\")) { // handle \[, \] and \. (are there others?) - value = value.replaceAll("\\\\([\\[\\]\\.])", "$1"); - } + final String value = unescape(ac1.getValue()); return element.getAttribute(ac1.getLocalName()).equals(value); } return element.hasAttribute(ac1.getLocalName()); @@ -530,6 +527,13 @@ } } + private static String unescape(final String value) { + if (value.indexOf('\\') == -1) { + return value; + } + return value.replaceAll("\\\\([\\[\\]\\.:])", "$1"); + } + private static boolean selects(final String condition, final String attribute, final char separator) { // attribute.equals(condition) // || attribute.startsWith(condition + " ") || attriubte.endsWith(" " + condition) Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java 2013-03-21 15:39:23 UTC (rev 8182) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java 2013-03-21 16:06:29 UTC (rev 8183) @@ -1107,4 +1107,27 @@ loadPageWithAlerts2(html); } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts({ "first", "second", "third" }) + public void escapedClassName() throws Exception { + final String html = "<html><head>\n" + + "<meta http-equiv='X-UA-Compatible' content='IE=9'>\n" + + "</head><body>\n" + + " <input id='first' class='foo[bar]'>\n" + + " <input id='second' class='foo.bar'>\n" + + " <input id='third' class='foo:bar'>\n" + + "<script>\n" + + "try {\n" + + " alert(document.querySelectorAll('.foo\\\\[bar\\\\]')[0].id);\n" + + " alert(document.querySelectorAll('.foo\\\\.bar')[0].id);\n" + + " alert(document.querySelectorAll('.foo\\\\:bar')[0].id);\n" + + "} catch(e) {alert('exception')}\n" + + "</script></body></html>"; + + loadPageWithAlerts2(html); + } } |