From: <no...@us...> - 2003-09-03 19:41:32
|
Log Message: ----------- Added support for window frames - patch from Jun Chen Modified Files: -------------- /cvsroot/htmlunit/htmlunit/src/xdocs: changes.xml /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host: Window.java /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript: JavaScriptEngine.java Added Files: ----------- /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript: WindowFramesArray.java Revision Data ------------- Index: changes.xml =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/xdocs/changes.xml,v retrieving revision 1.123 retrieving revision 1.124 diff -u -d -r1.123 -r1.124 --- changes.xml 3 Sep 2003 19:15:56 -0000 1.123 +++ changes.xml 3 Sep 2003 19:41:29 -0000 1.124 @@ -13,7 +13,9 @@ <action type="update" dev="mbowler" id="797144 "> Added support for the about: protocol - patch from Jun Chen </action> - + <action type="update" dev="mbowler" id="797144 "> + Added support for window frames - patch from Jun Chen + </action> </release> Index: Window.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/host/Window.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -d -r1.23 -r1.24 --- Window.java 3 Sep 2003 15:55:23 -0000 1.23 +++ Window.java 3 Sep 2003 19:41:29 -0000 1.24 @@ -48,13 +48,15 @@ import com.gargoylesoftware.htmlunit.html.HtmlInlineFrame; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; +import com.gargoylesoftware.htmlunit.javascript.WindowFramesArray; + import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Collections; -import java.util.List; + import org.mozilla.javascript.Context; import org.mozilla.javascript.Function; import org.mozilla.javascript.Scriptable; @@ -64,6 +66,7 @@ * * @version $Revision$ * @author <a href="mailto:mb...@Ga...">Mike Bowler</a> + * @author <a href="mailto:che...@us...">Chen Jun</a> */ public final class Window extends SimpleScriptable { @@ -73,7 +76,7 @@ private Screen screen_; private History history_; private Location location_; - + private WindowFramesArray windowFramesArray_; /** * Create an instance. The rhino engine requires all host objects * to have a default constructor. @@ -352,6 +355,9 @@ location_ = (Location)makeJavaScriptObject("Location"); location_.initialize(this); + + windowFramesArray_ =(WindowFramesArray)makeJavaScriptObject("WindowFramesArray"); + windowFramesArray_.initialize(htmlPage); } @@ -408,27 +414,12 @@ /** - * Return the value of the frames property. Currently not implemented + * Return the value of the frames property. * @return The value of window.frames */ - public SimpleScriptable[] jsGet_frames() { - final Page page = webWindow_.getEnclosedPage(); - if( page == null || page instanceof HtmlPage == false ) { - return new SimpleScriptable[0]; - } - - final HtmlPage htmlPage = (HtmlPage)page; - final List frames = htmlPage.getFrames(); - final int frameCount = frames.size(); - final SimpleScriptable[] jsFrames = new SimpleScriptable[frameCount]; - - for( int i=0; i<frameCount; i++ ) { - jsFrames[i] = (SimpleScriptable)((WebWindow)frames.get(i)).getScriptObject(); - } - - return jsFrames; + public WindowFramesArray jsGet_frames() { + return windowFramesArray_; } - /** * Return the WebWindow associated with this Window --- NEW FILE: WindowFramesArray.java --- /* * Copyright (c) 2002, 2003 Gargoyle Software Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: * * "This product includes software developed by Gargoyle Software Inc. * (http://www.GargoyleSoftware.com/)." * * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. * 4. The name "Gargoyle Software" must not be used to endorse or promote * products derived from this software without prior written permission. * For written permission, please contact in...@Ga.... * 5. Products derived from this software may not be called "HtmlUnit", nor may * "HtmlUnit" appear in their name, without prior written permission of * Gargoyle Software Inc. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.gargoylesoftware.htmlunit.javascript; import java.util.List; import org.mozilla.javascript.Scriptable; import com.gargoylesoftware.htmlunit.Assert; import com.gargoylesoftware.htmlunit.WebWindow; import com.gargoylesoftware.htmlunit.html.HtmlPage; /** * An array returned by frames property of Window * * @author <a href="mailto:che...@us...>Chen Jun</a> * @version 1.0, 2003-8-23 */ public class WindowFramesArray extends SimpleScriptable { private HtmlPage htmlPage_; /** * Create an instance. Javascript objects must have a default constructor. */ public WindowFramesArray() { } /** * <p>Return the object at the specified index.</p> * * <p>TODO: This implementation is particularly inefficient but without a way * to detect if an element has been inserted or removed, it isn't safe to * cache the array/<p> * * @param index The index * @param start The object that get is being called on. * @return The object or NOT_FOUND */ public Object get(final int index, final Scriptable start) { final HtmlPage htmlPage = ((WindowFramesArray) start).htmlPage_; if (htmlPage == null) { return super.get(index, start); } final List frames = htmlPage.getFrames(); if( frames == null ) { return NOT_FOUND; } final int frameCount = frames.size(); if (index < 0 || index >= frameCount) { return NOT_FOUND; } return ((WebWindow) frames.get(index)).getScriptObject(); } /** * Return the frame at the specified name or NOT_FOUND. * * @param name The name. * @param start The object that get is being called on. * @return The object or NOT_FOUND */ public Object get(final String name, final Scriptable start) { final HtmlPage htmlPage = ((WindowFramesArray) start).htmlPage_; if (htmlPage == null) { return super.get(name, start); } final List frames = htmlPage.getFrames(); if (frames == null) { return NOT_FOUND; } final int frameCount = frames.size(); for (int i = 0; i < frameCount; i++) { if (((WebWindow) frames.get(i)).getName().equals(name)) { return ((WebWindow) frames.get(i)).getScriptObject(); } } return NOT_FOUND; } /** * Initialize this object * @param page The HtmlPage that this object will retrive elements from. */ public void initialize(final HtmlPage page) { Assert.notNull("page", page); htmlPage_ = page; } /** * Javascript constructor. This must be declared in every javascript file because * the rhino engine won't walk up the hierarchy looking for constructors. */ public final void jsConstructor() { } /** * <p>Return the number of elements in this array</p> * * @return The number of elements in the array */ public int jsGet_length() { Assert.notNull("htmlpage", htmlPage_); return htmlPage_.getFrames().size(); } } Index: JavaScriptEngine.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java,v retrieving revision 1.19 retrieving revision 1.20 diff -u -d -r1.19 -r1.20 --- JavaScriptEngine.java 3 Sep 2003 15:55:24 -0000 1.19 +++ JavaScriptEngine.java 3 Sep 2003 19:41:29 -0000 1.20 @@ -58,6 +58,7 @@ * * @version $Revision$ * @author <a href="mailto:mb...@Ga...">Mike Bowler</a> + * @author <a href="mailto:che...@us...">Chen Jun</a> */ public final class JavaScriptEngine extends ScriptEngine { /** Information specific to the javascript engine */ @@ -150,6 +151,7 @@ ScriptableObject.defineClass(parentScope, DocumentAllArray.class); ScriptableObject.defineClass(parentScope, FormElementsArray.class); + ScriptableObject.defineClass(parentScope, WindowFramesArray.class); final Window window = (Window)newPageInfo.getContext().newObject( parentScope, "Window", new Object[0]); |