1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import org.jaxen.JaxenException; 41 import org.mozilla.javascript.Context; 42 43 import com.gargoylesoftware.htmlunit.html.DomNode; 44 import com.gargoylesoftware.htmlunit.html.HtmlElement; 45 import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; 46 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath; 47 import com.gargoylesoftware.htmlunit.javascript.ElementArray; 48 49 61 public class NodeImpl extends SimpleScriptable { 62 63 private ElementArray childNodes_; private static final long serialVersionUID = -5695262053081637445L; 65 66 67 70 public NodeImpl() { 71 } 72 73 74 78 public void jsConstructor() { 79 } 80 81 82 86 public short jsxGet_nodeType() { 87 return getDomNodeOrDie().getNodeType(); 88 } 89 90 91 95 public String jsxGet_nodeName() { 96 final DomNode domNode = getDomNodeOrDie(); 97 String nodeName = domNode.getNodeName(); 98 99 if( domNode instanceof HtmlElement ) { 103 nodeName = nodeName.toUpperCase(); 104 } 105 return nodeName; 106 } 107 108 109 113 public String jsxGet_nodeValue() { 114 return getDomNodeOrDie().getNodeValue(); 115 } 116 117 118 122 public void jsxSet_nodeValue( final String newValue ) { 123 getDomNodeOrDie().setNodeValue( newValue ); 124 } 125 126 127 132 public Object jsxFunction_appendChild(final Object childObject) { 133 134 Object appendedChild = null; 135 if (childObject instanceof NodeImpl) { 136 final DomNode childDomNode = 138 ((NodeImpl) childObject).getDomNodeOrDie(); 139 140 final DomNode parentNode = getDomNodeOrDie(); 142 143 parentNode.appendChild(childDomNode); 145 appendedChild = childObject; 146 } 147 return appendedChild; 148 } 149 150 151 157 public Object jsxFunction_cloneNode(final boolean deep) { 158 final DomNode domNode = getDomNodeOrDie(); 159 final DomNode clonedNode = domNode.cloneNode( deep ); 160 return getJavaScriptNode(clonedNode); 161 } 162 163 164 171 public Object jsxFunction_insertBefore( 172 final Object newChildObject, final Object refChildObject) { 173 Object appendedChild = null; 174 175 if (newChildObject instanceof NodeImpl) { 176 177 final DomNode newChildNode = 178 ((NodeImpl) newChildObject).getDomNodeOrDie(); 179 180 final DomNode refChildNode; 181 if(refChildObject != null) { 182 refChildNode = ((NodeImpl) refChildObject).getDomNodeOrDie(); 183 } 184 else { 185 refChildNode = null; 186 } 187 188 if (refChildNode != null ) { 190 refChildNode.insertBefore(newChildNode); 191 appendedChild = newChildObject; 192 } 193 else { 194 getDomNodeOrDie().appendChild(newChildNode); 195 } 196 } 197 return appendedChild; 198 } 199 200 201 206 public Object jsxFunction_removeChild(final Object childObject) { 207 Object removedChild = null; 208 209 if (childObject instanceof NodeImpl) { 210 final DomNode childNode = 212 ((NodeImpl) childObject).getDomNodeOrDie(); 213 214 childNode.remove(); 216 removedChild = childObject; 217 } 218 return removedChild; 219 } 220 221 225 public boolean jsxFunction_hasChildNodes() { 226 return getDomNodeOrDie().getChildIterator().hasNext(); 227 } 228 229 233 public Object jsxGet_childNodes() { 234 if (childNodes_ == null) { 235 childNodes_ = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME); 236 try { 237 childNodes_.init(getDomNodeOrDie(), new HtmlUnitXPath("./* | text()")); 238 } 239 catch (final JaxenException je) { 240 throw Context.reportRuntimeError("Failed to initialize collection element.childNodes: " 241 + je.getMessage()); 242 } 243 } 244 return childNodes_; 245 } 246 247 248 254 public Object jsxFunction_replaceChild( 255 final Object newChildObject, final Object oldChildObject) { 256 Object removedChild = null; 257 258 if (newChildObject instanceof NodeImpl && 259 oldChildObject instanceof NodeImpl) { 260 final DomNode newChildNode = 262 ((NodeImpl) newChildObject).getDomNodeOrDie(); 263 264 final DomNode oldChildNode; 265 if(oldChildObject != null) { 266 oldChildNode = ((NodeImpl) oldChildObject).getDomNodeOrDie(); 268 oldChildNode.replace(newChildNode); 269 removedChild = oldChildObject; 270 } 271 } 272 return removedChild; 273 } 274 275 276 281 public Object jsxGet_parentNode() { 282 return getJavaScriptNode( getDomNodeOrDie().getParentNode() ); 283 } 284 285 286 292 public Object jsxGet_nextSibling() { 293 return getJavaScriptNode( getDomNodeOrDie().getNextSibling() ); 294 } 295 296 297 303 public Object jsxGet_previousSibling() { 304 return getJavaScriptNode( getDomNodeOrDie().getPreviousSibling() ); 305 } 306 307 308 314 public Object jsxGet_firstChild() { 315 return getJavaScriptNode( getDomNodeOrDie().getFirstChild() ); 316 } 317 318 319 325 public Object jsxGet_lastChild() { 326 return getJavaScriptNode( getDomNodeOrDie().getLastChild() ); 327 } 328 329 330 335 protected Object getJavaScriptNode( final DomNode domNode ) { 336 if ( domNode == null ) { 337 return null; 338 } 339 return getScriptableFor( domNode ); 340 } 341 } 342 | Popular Tags |