1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import com.gargoylesoftware.htmlunit.Assert; 41 import com.gargoylesoftware.htmlunit.html.HtmlElement; 42 import com.gargoylesoftware.htmlunit.html.HtmlForm; 43 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath; 44 import com.gargoylesoftware.htmlunit.javascript.ElementArray; 45 46 import java.io.IOException ; 47 48 import org.jaxen.JaxenException; 49 import org.jaxen.XPath; 50 import org.mozilla.javascript.Context; 51 import org.mozilla.javascript.Function; 52 import org.mozilla.javascript.Scriptable; 53 54 65 public class Form extends HTMLElement { 66 67 private static final long serialVersionUID = -1860993922147246513L; 68 private ElementArray elements_; 70 73 public Form() { } 74 75 76 80 public final void jsConstructor() { 81 } 82 83 84 87 public void setHtmlElement( final HtmlElement htmlElement ) { 88 super.setHtmlElement( htmlElement ); 89 final HtmlForm htmlForm = getHtmlForm(); 90 htmlForm.setScriptObject( this ); 91 } 92 93 94 98 public String jsxGet_name() { 99 return getHtmlForm().getNameAttribute(); 100 } 101 102 103 107 public ElementArray jsxGet_elements() { 108 if (elements_ == null) { 109 final HtmlForm htmlForm = getHtmlForm(); 110 elements_ = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME); 111 try { 112 final XPath xpath = new HtmlUnitXPath("//*[(name() = 'input' or name() = 'button'" 113 + " or name() = 'select' or name() = 'textarea')]", 114 HtmlUnitXPath.buildSubtreeNavigator(htmlForm)); 115 elements_.init(htmlForm, xpath); 116 } 117 catch (final JaxenException e) { 118 throw Context.reportRuntimeError("Failed to initialize collection form.elements: " + e.getMessage()); 119 } 120 } 121 return elements_; 122 } 123 124 125 131 public int jsxGet_length() { 132 final int all = jsxGet_elements().jsGet_length(); 133 final int images = getHtmlForm().getHtmlElementsByAttribute("input", "type", "image").size(); 134 return all - images; 135 } 136 137 138 142 public String jsxGet_action() { 143 return getHtmlForm().getActionAttribute(); 144 } 145 146 147 151 public void jsxSet_action( final String action ) { 152 Assert.notNull("action", action); 153 getHtmlForm().setActionAttribute(action); 154 } 155 156 157 161 public String jsxGet_method() { 162 return getHtmlForm().getMethodAttribute(); 163 } 164 165 166 170 public void jsxSet_method( final String method ) { 171 Assert.notNull("method", method); 172 getHtmlForm().setMethodAttribute(method); 173 } 174 175 176 180 public String jsxGet_target() { 181 return getHtmlForm().getTargetAttribute(); 182 } 183 184 188 public Function jsxGet_onsubmit() { 189 return getHtmlForm().getEventHandler("onsubmit"); 190 } 191 192 196 public void jsxSet_onsubmit(final Function onsubmit) { 197 getHtmlForm().setEventHandler("onsubmit", onsubmit); 198 } 199 200 204 public void jsxSet_target( final String target ) { 205 Assert.notNull("target", target); 206 getHtmlForm().setTargetAttribute(target); 207 } 208 209 210 214 public String jsxGet_encoding() { 215 return getHtmlForm().getEnctypeAttribute(); 216 } 217 218 219 223 public void jsxSet_encoding( final String encoding ) { 224 Assert.notNull("encoding", encoding); 225 getHtmlForm().setEnctypeAttribute(encoding); 226 } 227 228 229 private HtmlForm getHtmlForm() { 230 return (HtmlForm)getHtmlElementOrDie(); 231 } 232 233 238 public void jsxFunction_submit() throws IOException { 239 getHtmlForm().submit(); 240 } 241 242 243 246 public void jsxFunction_reset() { 247 getHtmlForm().reset(); 248 } 249 250 251 257 public Object get( final String name, final Scriptable start ) { 258 return ((Form) start).get(name); 259 } 260 261 266 Object get(final String name) { 267 final ElementArray elements = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME); 270 final HtmlForm htmlForm = getHtmlForm(); 271 try { 272 final XPath xpath = new HtmlUnitXPath("//*[(@name = '" + name + "' or @id = '" + name + "')" 273 + " and ((name() = 'input' and translate(@type, 'IMAGE', 'image') != 'image') or name() = 'button'" 274 + " or name() = 'select' or name() = 'textarea')]", 275 HtmlUnitXPath.buildSubtreeNavigator(htmlForm)); 276 elements.init(htmlForm, xpath); 277 } 278 catch (final JaxenException e) { 279 throw Context.reportRuntimeError("Failed to initialize collection: " + e.getMessage()); 280 } 281 282 Object result = elements; 283 final int nbElements = elements.jsGet_length(); 284 if (nbElements == 0) { 285 result = NOT_FOUND; 286 } 287 else if (nbElements == 1) { 288 result = elements.get(0, elements); 289 } 290 291 if (result == NOT_FOUND) { 292 result = super.get( name, this ); 293 } 294 295 return result; 296 } 297 298 299 305 public Object get( final int index, final Scriptable start ) { 306 return jsxGet_elements().get(index, ((Form) start).jsxGet_elements()); 307 } 308 } 309 | Popular Tags |