[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit ContentConcealer.java,NONE,1.1 HTMLPage.j
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-03-09 20:35:50
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory sc8-pr-cvs1:/tmp/cvs-serv26420/src/com/meterware/httpunit
Modified Files:
HTMLPage.java ParsedHTML.java WebResponse.java
Added Files:
ContentConcealer.java
Log Message:
support <noscript> tags
***** Error reading new file[Errno 2] No such file or directory: 'ContentConcealer.java'
Index: HTMLPage.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HTMLPage.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- HTMLPage.java 28 Feb 2003 17:29:51 -0000 1.25
+++ HTMLPage.java 9 Mar 2003 20:35:46 -0000 1.26
@@ -31,7 +31,6 @@
import java.io.IOException;
import java.net.URL;
-import java.util.ArrayList;
import java.util.Vector;
@@ -69,43 +68,6 @@
NodeList nl = ((Document) getOriginalDOM()).getElementsByTagName( "body" );
if (nl.getLength() == 0) return "";
return ((Element) nl.item(0)).getAttribute( "onload" );
- }
-
-
- /**
- * Returns the contents of any script tags on the page, concatenated.
- */
- public String[] getScripts() throws SAXException {
- NodeList nl = ((Document) getOriginalDOM()).getElementsByTagName( "script" );
- ArrayList scripts = new ArrayList();
- for (int i = 0; i < nl.getLength(); i++) {
- Node scriptNode = nl.item(i);
- String language = NodeUtils.getNodeAttribute( scriptNode, "language", null );
- if (language != null && !language.startsWith( "JavaScript")) continue;
- String scriptLocation = NodeUtils.getNodeAttribute( scriptNode, "src", null );
- if (scriptLocation == null) {
- scripts.add( NodeUtils.asText( scriptNode.getChildNodes() ) );
- } else {
- try {
- scripts.add( getIncludedScript( scriptLocation ) );
- } catch (IOException e) {
- throw new RuntimeException( "Error loading included script: " + e );
- }
- }
- }
- return (String[]) scripts.toArray( new String[ scripts.size() ] );
- }
-
-
- /**
- * Returns the contents of an included script, given its src attribute.
- * @param srcAttribute
- * @return the contents of the script.
- * @throws IOException if there is a problem retrieving the script
- */
- String getIncludedScript( String srcAttribute ) throws IOException {
- WebRequest req = new GetMethodWebRequest( getBaseURL(), srcAttribute );
- return getResponse().getWindow().getResource( req ).getText();
}
Index: ParsedHTML.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/ParsedHTML.java,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -r1.44 -r1.45
--- ParsedHTML.java 4 Feb 2003 05:32:03 -0000 1.44
+++ ParsedHTML.java 9 Mar 2003 20:35:46 -0000 1.45
@@ -24,6 +24,7 @@
import java.net.URL;
import java.util.*;
+import java.io.IOException;
import com.meterware.httpunit.scripting.ScriptableDelegate;
@@ -47,6 +48,8 @@
private boolean _updateElements = true;
+ private boolean _enableNoScriptNodes;
+
/** map of element IDs to elements. **/
private HashMap _elementsByID = new HashMap();
@@ -191,6 +194,55 @@
}
+ private void interpretScriptElement( Element element ) {
+ String script = getScript( element );
+ if (script != null) {
+ try {
+ _updateElements = false;
+ String language = NodeUtils.getNodeAttribute( element, "language", null );
+ if (!getResponse().getScriptableObject().supportsScript( language )) _enableNoScriptNodes = true;
+ getResponse().getScriptableObject().runScript( language, script );
+ } finally {
+ setRootNode( _rootNode );
+ }
+ }
+ }
+
+
+ private String getScript( Node scriptNode ) {
+ String scriptLocation = NodeUtils.getNodeAttribute( scriptNode, "src", null );
+ if (scriptLocation == null) {
+ return NodeUtils.asText( scriptNode.getChildNodes() );
+ } else {
+ try {
+ return getIncludedScript( scriptLocation );
+ } catch (IOException e) {
+ throw new RuntimeException( "Error loading included script: " + e );
+ }
+ }
+ }
+
+
+ /**
+ * Returns the contents of an included script, given its src attribute.
+ * @param srcAttribute
+ * @return the contents of the script.
+ * @throws java.io.IOException if there is a problem retrieving the script
+ */
+ String getIncludedScript( String srcAttribute ) throws IOException {
+ WebRequest req = new GetMethodWebRequest( getBaseURL(), srcAttribute );
+ return getResponse().getWindow().getResource( req ).getText();
+ }
+
+
+ /**
+ * If noscript node content is enabled, returns null - otherwise returns a concealing element.
+ */
+ private HTMLElement toNoscriptElement( Element element ) {
+ return _enableNoScriptNodes ? null : new NoScriptElement( element );
+ }
+
+
abstract static class HTMLElementFactory {
abstract HTMLElement toHTMLElement( NodeUtils.PreOrderTraversal pot, ParsedHTML parsedHTML, Element element );
@@ -271,6 +323,30 @@
}
+ static class ScriptFactory extends HTMLElementFactory {
+
+ HTMLElement toHTMLElement( NodeUtils.PreOrderTraversal pot, ParsedHTML parsedHTML, Element element ) {
+ return null;
+ }
+
+ void recordElement( NodeUtils.PreOrderTraversal pot, Element element, ParsedHTML parsedHTML ) {
+ parsedHTML.interpretScriptElement( element );
+ }
+ }
+
+
+ static class NoScriptFactory extends HTMLElementFactory {
+
+ HTMLElement toHTMLElement( NodeUtils.PreOrderTraversal pot, ParsedHTML parsedHTML, Element element ) {
+ return parsedHTML.toNoscriptElement( element );
+ }
+
+ protected boolean addToContext() {
+ return true;
+ }
+ }
+
+
static class WebFrameFactory extends HTMLElementFactory {
HTMLElement toHTMLElement( NodeUtils.PreOrderTraversal pot, ParsedHTML parsedHTML, Element element ) {
return parsedHTML.toWebFrame( element );
@@ -280,7 +356,7 @@
static class WebIFrameFactory extends HTMLElementFactory {
HTMLElement toHTMLElement( NodeUtils.PreOrderTraversal pot, ParsedHTML parsedHTML, Element element ) {
- return parsedHTML.toWebFrame( element );
+ return parsedHTML.toWebIFrame( element );
}
@@ -372,17 +448,19 @@
private static HTMLElementFactory _defaultFactory = new DefaultElementFactory();
static {
- _htmlFactoryClasses.put( "a", new WebLinkFactory() );
- _htmlFactoryClasses.put( "area", new WebLinkFactory() );
- _htmlFactoryClasses.put( "form", new WebFormFactory() );
- _htmlFactoryClasses.put( "img", new WebImageFactory() );
- _htmlFactoryClasses.put( "applet", new WebAppletFactory() );
- _htmlFactoryClasses.put( "table", new WebTableFactory() );
- _htmlFactoryClasses.put( "tr", new TableRowFactory() );
- _htmlFactoryClasses.put( "td", new TableCellFactory() );
- _htmlFactoryClasses.put( "th", new TableCellFactory() );
- _htmlFactoryClasses.put( "frame", new WebFrameFactory() );
- _htmlFactoryClasses.put( "iframe", new WebIFrameFactory() );
+ _htmlFactoryClasses.put( "a", new WebLinkFactory() );
+ _htmlFactoryClasses.put( "area", new WebLinkFactory() );
+ _htmlFactoryClasses.put( "form", new WebFormFactory() );
+ _htmlFactoryClasses.put( "img", new WebImageFactory() );
+ _htmlFactoryClasses.put( "applet", new WebAppletFactory() );
+ _htmlFactoryClasses.put( "table", new WebTableFactory() );
+ _htmlFactoryClasses.put( "tr", new TableRowFactory() );
+ _htmlFactoryClasses.put( "td", new TableCellFactory() );
+ _htmlFactoryClasses.put( "th", new TableCellFactory() );
+ _htmlFactoryClasses.put( "frame", new WebFrameFactory() );
+ _htmlFactoryClasses.put( "iframe", new WebIFrameFactory() );
+ _htmlFactoryClasses.put( "script", new ScriptFactory() );
+ _htmlFactoryClasses.put( "noscript", new NoScriptFactory() );
for (Iterator i = Arrays.asList( FormControl.getControlElementTags() ).iterator(); i.hasNext();) {
_htmlFactoryClasses.put( i.next(), new FormControlFactory() );
@@ -402,7 +480,7 @@
public boolean processElement( NodeUtils.PreOrderTraversal pot, Element element ) {
HTMLElementFactory factory = getHTMLElementFactory( element.getNodeName().toLowerCase() );
if (factory == null || !factory.isRecognized( getClientProperties() )) return true;
- if (pot.getClosestContext( WebFrame.class ) != null) return true;
+ if (pot.getClosestContext( ContentConcealer.class ) != null) return true;
if (!_elements.containsKey( element )) factory.recordElement( pot, element, ParsedHTML.this );
if (factory.addToContext()) pot.pushContext( _elements.get( element ) );
@@ -436,6 +514,11 @@
}
+ private WebFrame toWebIFrame( Element element ) {
+ return new WebIFrame( _baseURL, element, _frameName );
+ }
+
+
private WebLink toLinkAnchor( Element child ) {
return (!isWebLink( child )) ? null : new WebLink( _response, _baseURL, _baseTarget, child );
}
@@ -723,4 +806,31 @@
return null;
}
-}
\ No newline at end of file
+
+
+ class WebIFrame extends WebFrame implements ContentConcealer {
+
+ public WebIFrame( URL baseURL, Node frameNode, String parentFrameName ) {
+ super( baseURL, frameNode, parentFrameName );
+ }
+ }
+
+
+ class NoScriptElement extends HTMLElementBase implements ContentConcealer {
+
+ public NoScriptElement( Node node ) {
+ super( node );
+ }
+
+
+ protected ScriptableDelegate newScriptable() {
+ return null;
+ }
+
+
+ protected ScriptableDelegate getParentDelegate() {
+ return null;
+ }
+ }
+
+}
Index: WebResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v
retrieving revision 1.111
retrieving revision 1.112
diff -u -r1.111 -r1.112
--- WebResponse.java 7 Mar 2003 05:03:59 -0000 1.111
+++ WebResponse.java 9 Mar 2003 20:35:46 -0000 1.112
@@ -564,10 +564,11 @@
public void load() throws SAXException {
if (isHTML()) {
- final String[] scripts = getReceivedPage().getScripts();
- for (int i = 0; i < scripts.length; i++) {
- runScript( scripts[i] );
- }
+ getReceivedPage().getForms(); // TODO be more explicit here - don't care about forms, after all
+// final String[] scripts = getReceivedPage().getScripts(); TODO delete this
+// for (int i = 0; i < scripts.length; i++) {
+// runScript( scripts[i] );
+// }
doEvent( getReceivedPage().getOnLoadEvent() );
}
}
|