From: <rb...@us...> - 2013-03-23 08:15:35
|
Revision: 8191 http://sourceforge.net/p/htmlunit/code/8191 Author: rbri Date: 2013-03-23 08:15:31 +0000 (Sat, 23 Mar 2013) Log Message: ----------- css selector fix for IE Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 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/BrowserVersionFeatures.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 2013-03-22 19:36:33 UTC (rev 8190) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 2013-03-23 08:15:31 UTC (rev 8191) @@ -1139,6 +1139,10 @@ @BrowserFeature(@WebBrowser(value = IE, minVersion = 8)) QUERYSELECTORALL_NOT_IN_QUIRKS, + /** Indicates that escaping in attrubute selectors is supported. */ + @BrowserFeature({ @WebBrowser(FF), @WebBrowser(CHROME) }) + SELECTOR_ATTRIBUTE_ESCAPING, + /** * Indicates that all options of a select are deselected, * if the select state is changed for an unknown option. 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-22 19:36:33 UTC (rev 8190) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSStyleSheet.java 2013-03-23 08:15:31 UTC (rev 8191) @@ -16,6 +16,7 @@ import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.CSS_SELECTOR_LANG; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.QUERYSELECTORALL_NOT_IN_QUIRKS; +import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.SELECTOR_ATTRIBUTE_ESCAPING; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.STYLESHEET_HREF_EXPANDURL; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.STYLESHEET_HREF_STYLE_EMPTY; import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.STYLESHEET_HREF_STYLE_NULL; @@ -465,7 +466,10 @@ return ac4.getValue().equals(element.getId()); case Condition.SAC_CLASS_CONDITION: final AttributeCondition ac3 = (AttributeCondition) condition; - final String v3 = unescape(ac3.getValue()); + String v3 = ac3.getValue(); + if (v3.indexOf('\\') > -1) { + v3 = UNESCAPE_SELECTOR.matcher(v3).replaceAll("$1"); + } final String a3 = element.getAttribute("class"); return selects(v3, a3, ' '); case Condition.SAC_AND_CONDITION: @@ -475,7 +479,13 @@ case Condition.SAC_ATTRIBUTE_CONDITION: final AttributeCondition ac1 = (AttributeCondition) condition; if (ac1.getSpecified()) { - final String value = unescape(ac1.getValue()); + String value = ac1.getValue(); + if (value.indexOf('\\') > -1) { + if (!browserVersion.hasFeature(SELECTOR_ATTRIBUTE_ESCAPING)) { + throw new CSSException("Invalid selectors: '" + value + "'"); + } + value = UNESCAPE_SELECTOR.matcher(value).replaceAll("$1"); + } return element.getAttribute(ac1.getLocalName()).equals(value); } return element.hasAttribute(ac1.getLocalName()); @@ -528,13 +538,6 @@ } } - private static String unescape(final String value) { - if (value.indexOf('\\') == -1) { - return value; - } - return UNESCAPE_SELECTOR.matcher(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-22 19:36:33 UTC (rev 8190) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/CSSSelectorTest.java 2013-03-23 08:15:31 UTC (rev 8191) @@ -1091,7 +1091,7 @@ * @throws Exception if an error occurs */ @Test - @Alerts(DEFAULT = { "first", "second" }, IE8 = "exception") + @Alerts(DEFAULT = { "first", "second" }, IE8 = { "exception", "exception" }) public void escapedAttributeValue() throws Exception { final String html = "<html><head>\n" + "<meta http-equiv='X-UA-Compatible' content='IE=9'>\n" @@ -1101,6 +1101,8 @@ + "<script>\n" + "try {\n" + " alert(document.querySelectorAll('input[name=foo\\\\[bar\\\\]]')[0].id);\n" + + "} catch(e) {alert('exception')}\n" + + "try {\n" + " alert(document.querySelectorAll('input[name=foo\\\\.bar]')[0].id);\n" + "} catch(e) {alert('exception')}\n" + "</script></body></html>"; |