From: <rb...@us...> - 2013-05-06 19:09:45
|
Revision: 8265 http://sourceforge.net/p/htmlunit/code/8265 Author: rbri Date: 2013-05-06 19:09:39 +0000 (Mon, 06 May 2013) Log Message: ----------- more fixes for the default values Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlCheckBoxInput.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlFileInput.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlRadioButtonInput.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/EventTest.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlCheckBoxInput.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlCheckBoxInput.java 2013-05-06 17:04:31 UTC (rev 8264) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlCheckBoxInput.java 2013-05-06 19:09:39 UTC (rev 8265) @@ -41,6 +41,11 @@ */ public class HtmlCheckBoxInput extends HtmlInput { + /** + * Value to use if no specified <tt>value</tt> attribute. + */ + private static final String DEFAULT_VALUE = "on"; + private boolean defaultCheckedState_; private boolean valueAtFocus_; private boolean forceChecked_; @@ -58,16 +63,36 @@ */ HtmlCheckBoxInput(final String namespaceURI, final String qualifiedName, final SgmlPage page, final Map<String, DomAttr> attributes) { - super(namespaceURI, qualifiedName, page, attributes); + // default value for both IE6 and Mozilla 1.7 even if spec says it is unspecified + super(namespaceURI, qualifiedName, page, addValueIfNeeded(page, attributes)); - //From the checkbox creator + // fix the default value in case we have set it + if (getAttribute("value") == DEFAULT_VALUE) { + setDefaultValue(ATTRIBUTE_NOT_DEFINED, false); + } + defaultCheckedState_ = hasAttribute("checked"); + } - // default value for both IE6 and Mozilla 1.7 even if spec says it is unspecified - if (getAttribute("value") == ATTRIBUTE_NOT_DEFINED) { - setAttribute("value", "on"); - setDefaultValue(ATTRIBUTE_NOT_DEFINED, false); + /** + * Add missing attribute if needed by fixing attribute map rather to add it afterwards as this second option + * triggers the instantiation of the script object at a time where the DOM node has not yet been added to its + * parent. + */ + private static Map<String, DomAttr> addValueIfNeeded(final SgmlPage page, + final Map<String, DomAttr> attributes) { + + for (final String key : attributes.keySet()) { + if ("value".equalsIgnoreCase(key)) { + return attributes; // value attribute was specified + } } + + // value attribute was not specified, add it + final DomAttr newAttr = new DomAttr(page, null, "value", DEFAULT_VALUE, true); + attributes.put("value", newAttr); + + return attributes; } /** Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlFileInput.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlFileInput.java 2013-05-06 17:04:31 UTC (rev 8264) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlFileInput.java 2013-05-06 19:09:39 UTC (rev 8265) @@ -19,6 +19,7 @@ import java.io.File; import java.net.URI; import java.net.URISyntaxException; +import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; @@ -53,14 +54,37 @@ */ HtmlFileInput(final String namespaceURI, final String qualifiedName, final SgmlPage page, final Map<String, DomAttr> attributes) { - super(namespaceURI, qualifiedName, page, attributes); - setAttribute("value", ""); + super(namespaceURI, qualifiedName, page, addValueIfNeeded(page, attributes)); + if (hasFeature(FILEINPUT_EMPTY_DEFAULT_VALUE)) { - setDefaultValue(""); + setDefaultValue("", false); } + else { + for (final Map.Entry<String, DomAttr> entry : attributes.entrySet()) { + if ("value".equalsIgnoreCase(entry.getKey())) { + setDefaultValue(entry.getValue().getNodeValue(), false); + } + } + } } /** + * Add missing attribute if needed by fixing attribute map rather to add it afterwards as this second option + * triggers the instantiation of the script object at a time where the DOM node has not yet been added to its + * parent. + */ + private static Map<String, DomAttr> addValueIfNeeded(final SgmlPage page, + final Map<String, DomAttr> attributes) { + + // we need a copy here because we have to check attributes later again + final Map<String, DomAttr> result = new HashMap<String, DomAttr>(attributes); + final DomAttr newAttr = new DomAttr(page, null, "value", "", true); + result.put("value", newAttr); + + return result; + } + + /** * Returns the in-memory data assigned to this file input element, if any. * @return <code>null</code> if {@link #setData(byte[])} hasn't be used */ Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlRadioButtonInput.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlRadioButtonInput.java 2013-05-06 17:04:31 UTC (rev 8264) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlRadioButtonInput.java 2013-05-06 19:09:39 UTC (rev 8265) @@ -45,6 +45,11 @@ */ public class HtmlRadioButtonInput extends HtmlInput { + /** + * Value to use if no specified <tt>value</tt> attribute. + */ + private static final String DEFAULT_VALUE = "on"; + private boolean defaultCheckedState_; private boolean valueAtFocus_; private boolean forceChecked_; @@ -61,11 +66,11 @@ */ HtmlRadioButtonInput(final String namespaceURI, final String qualifiedName, final SgmlPage page, final Map<String, DomAttr> attributes) { - super(namespaceURI, qualifiedName, page, attributes); + // default value for both IE6 and Mozilla 1.7 even if spec says it is unspecified + super(namespaceURI, qualifiedName, page, addValueIfNeeded(page, attributes)); - // default value for both IE6 and Mozilla 1.7 even if spec says it is unspecified - if (getAttribute("value") == ATTRIBUTE_NOT_DEFINED) { - setAttribute("value", "on"); + // fix the default value in case we have set it + if (getAttribute("value") == DEFAULT_VALUE) { setDefaultValue(ATTRIBUTE_NOT_DEFINED, false); } @@ -73,6 +78,27 @@ } /** + * Add missing attribute if needed by fixing attribute map rather to add it afterwards as this second option + * triggers the instantiation of the script object at a time where the DOM node has not yet been added to its + * parent. + */ + private static Map<String, DomAttr> addValueIfNeeded(final SgmlPage page, + final Map<String, DomAttr> attributes) { + + for (final String key : attributes.keySet()) { + if ("value".equalsIgnoreCase(key)) { + return attributes; // value attribute was specified + } + } + + // value attribute was not specified, add it + final DomAttr newAttr = new DomAttr(page, null, "value", DEFAULT_VALUE, true); + attributes.put("value", newAttr); + + return attributes; + } + + /** * {@inheritDoc} * @see SubmittableElement#reset() */ Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/EventTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/EventTest.java 2013-05-06 17:04:31 UTC (rev 8264) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/EventTest.java 2013-05-06 19:09:39 UTC (rev 8265) @@ -42,6 +42,7 @@ * @author Ahmed Ashour * @author Daniel Gredler * @author Marc Guillemot + * @author Ronald Brill */ @RunWith(BrowserRunner.class) public class EventTest extends WebDriverTestCase { @@ -708,10 +709,26 @@ @Alerts({ "from theField", "from theForm", "from document", "from window" }) public void eventHandlersParentScopeChain_formFields() throws Exception { eventHandlersParentScopeChain("<button", "</button>"); + eventHandlersParentScopeChain("<input type='text'", ""); + eventHandlersParentScopeChain("<input type='password'", ""); + eventHandlersParentScopeChain("<input type='hidden'", ""); + + eventHandlersParentScopeChain("<input type='checkbox'", ""); + eventHandlersParentScopeChain("<input type='radio'", ""); + + eventHandlersParentScopeChain("<input type='file'", ""); + eventHandlersParentScopeChain("<input type='image'", ""); + + eventHandlersParentScopeChain("<input type='button'", ""); + eventHandlersParentScopeChain("<input type='submit' value='xxx'", ""); // case without value attribute was failing first with IE due to the way the value attribute was added eventHandlersParentScopeChain("<input type='submit'", ""); + + eventHandlersParentScopeChain("<input type='reset' value='xxx'", ""); + // case without value attribute was failing first with IE due to the way the value attribute was added + eventHandlersParentScopeChain("<input type='reset'", ""); } /** @@ -725,19 +742,21 @@ } private void eventHandlersParentScopeChain(final String startTag, final String endTag) throws Exception { - final String html = "<html><body id='body'>\n" + final String html = "<html><html>\n" + + "<head><title>foo</title></head>\n" + + "<body id='body'>\n" + "<form id='theForm'>\n" - + "<div id='theDiv'>\n" - + startTag + " id='theField' onclick='alert(foo); return false;'>click me" + endTag + "\n" - + "</div>\n" + + " <div id='theDiv'>\n" + + " " + startTag + " id='theField' onclick='alert(foo); return false;'>click me" + endTag + "\n" + + " </div>\n" + "</form>\n" + "<script>\n" - + "var foo = 'from window';\n" - + "document.foo = 'from document';\n" - + "document.body.foo = 'from body';\n" - + "document.getElementById('theForm').foo = 'from theForm';\n" - + "document.getElementById('theDiv').foo = 'from theDiv';\n" - + "document.getElementById('theField').foo = 'from theField';\n" + + " var foo = 'from window';\n" + + " document.foo = 'from document';\n" + + " document.body.foo = 'from body';\n" + + " document.getElementById('theForm').foo = 'from theForm';\n" + + " document.getElementById('theDiv').foo = 'from theDiv';\n" + + " document.getElementById('theField').foo = 'from theField';\n" + "</script>\n" + "</body></html>"; |