From: <mgu...@us...> - 2013-03-21 15:39:26
|
Revision: 8182 http://sourceforge.net/p/htmlunit/code/8182 Author: mguillem Date: 2013-03-21 15:39:23 +0000 (Thu, 21 Mar 2013) Log Message: ----------- CSS selector: handle escaped characters 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:17:47 UTC (rev 8181) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-03-21 15:39:23 UTC (rev 8182) @@ -474,7 +474,11 @@ case Condition.SAC_ATTRIBUTE_CONDITION: final AttributeCondition ac1 = (AttributeCondition) condition; if (ac1.getSpecified()) { - return element.getAttribute(ac1.getLocalName()).equals(ac1.getValue()); + String value = ac1.getValue(); + if (value.contains("\\")) { // handle \[, \] and \. (are there others?) + value = value.replaceAll("\\\\([\\[\\]\\.])", "$1"); + } + return element.getAttribute(ac1.getLocalName()).equals(value); } return element.hasAttribute(ac1.getLocalName()); case Condition.SAC_BEGIN_HYPHEN_ATTRIBUTE_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:17:47 UTC (rev 8181) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java 2013-03-21 15:39:23 UTC (rev 8182) @@ -1086,4 +1086,25 @@ getMockWebConnection().setDefaultResponse(html); loadPageWithAlerts2(UrlUtils.getUrlWithNewRef(URL_FIRST, "id3")); } + + /** + * @throws Exception if an error occurs + */ + @Test + @Alerts(DEFAULT = { "first", "second" }, IE8 = "exception") + public void escapedAttributeValue() throws Exception { + final String html = "<html><head>\n" + + "<meta http-equiv='X-UA-Compatible' content='IE=9'>\n" + + "</head><body>\n" + + " <input id='first' name='foo[bar]'>\n" + + " <input id='second' name='foo.bar'>\n" + + "<script>\n" + + "try {\n" + + " alert(document.querySelectorAll('input[name=foo\\\\[bar\\\\]]')[0].id);\n" + + " alert(document.querySelectorAll('input[name=foo\\\\.bar]')[0].id);\n" + + "} catch(e) {alert('exception')}\n" + + "</script></body></html>"; + + loadPageWithAlerts2(html); + } } |