Since htmlunit supports frames, don't execute Javascript contained in <noframes> and <iframe> elements.</p> <p>Actually I think that <noframes>, <iframe> content shouldn't get loaded into the domtree at all. (see the tests 3 and 4 in the attached unit test).</p></noframes>
Logged In: YES
user_id=1612820
Originator: YES
File Added: NoFramesTest.java
Reupload od the test - a stronger one
Logged In: YES
user_id=950730
Originator: NO
I don't think <noframes>, and <iframe> content shouldn't get loaded, otherwise HtmlPage.asXml() will not retrieve them (and that affects what the user expects).</p> <p>Below is a patch (can't be uploaded). However, HtmlInlineFrameTest.testSetSrcAttribute_ViaJavaScript fails and seems to be invalid test, please verify why it is there.</p> <p>Many thanks,<br> Ahmed Ashour</p> <hr> <p>Index: src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java</p> <p>===================================================================</p> <p>--- src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java (revision 1363)</p> <p>+++ src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java (working copy)</p> <p>@@ -87,6 +87,7 @@</p> <ul> <li><a class="user-mention" href="/u/author/profile/">@author</a> <a href="mailto:cse@dynabean.de">Christian Sell</a></li> <li><a class="user-mention" href="/u/author/profile/">@author</a> Chris Erskine</li> <li><a class="user-mention" href="/u/author/profile/">@author</a> Marc Guillemot</li> <li> <ul> <li><a class="user-mention" href="/u/author/profile/">@author</a> Ahmed Ashour<br> */<br> public final class HtmlPage extends DomNode implements Page {</li> </ul> </li> </ul> <p>@@ -1407,7 +1408,16 @@</p> <div class="codehilite"><pre><span></span><code><span class="w"> </span><span class="nt">if</span><span class="w"> </span><span class="o">(</span><span class="nt">elem</span><span class="w"> </span><span class="nt">instanceof</span><span class="w"> </span><span class="nt">HtmlScript</span><span class="o">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="err">final</span><span class="w"> </span><span class="err">HtmlScript</span><span class="w"> </span><span class="err">scriptNode</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">(HtmlScript)</span><span class="w"> </span><span class="err">node</span><span class="p">;</span> <span class="w"> </span><span class="err">getLog().debug("Script</span><span class="w"> </span><span class="err">node</span><span class="w"> </span><span class="n">added</span><span class="p">:</span><span class="w"> </span><span class="err">"</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">scriptNode</span><span class="o">.</span><span class="nf">asXml</span><span class="p">());</span> </code></pre></div> <ul> <li>scriptNode.executeScriptIfNeeded();<br> +</li> <li>boolean preventExecution = false;</li> <li>for( DomNode n = node; n != null; n = n.getParentNode() ) {</li> <li>if( n instanceof HtmlInlineFrame || n instanceof HtmlNoFrames ) {</li> <li>preventExecution = true;</li> <li>break;</li> <li>}</li> <li>}</li> <li>if( !preventExecution )</li> <li>scriptNode.executeScriptIfNeeded();<br> } <br> else {<br> final List scripts = elem.getHtmlElementsByTagName("script");<br> Index: src/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java</li> </ul> <p>===================================================================</p> <p>--- src/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java (revision 1363)</p> <p>+++ src/java/com/gargoylesoftware/htmlunit/html/HtmlScript.java (working copy)</p> <p>@@ -53,6 +53,7 @@</p> <ul> <li><a class="user-mention" href="/u/author/profile/">@author</a> <a href="mailto:cse@dynabean.de">Christian Sell</a></li> <li><a class="user-mention" href="/u/author/profile/">@author</a> Marc Guillemot</li> <li><a class="user-mention" href="/u/author/profile/">@author</a> David K. Taylor</li> <li> <ul> <li><a class="user-mention" href="/u/author/profile/">@author</a> Ahmed Ashour</li> </ul> </li> <li><a class="user-mention" href="/u/see/profile/">@see</a> <a href="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-81598695"></li> <li>DOM Level 1</a></li> <li> <p><a class="user-mention" href="/u/see/profile/">@see</a> <a href="http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-81598695"><br> @@ -166,7 +167,15 @@</p> <p>*/<br> public DomNode appendChild(final DomNode node) {<br> final DomNode response = super.appendChild(node);</p> </li> <li> <p>executeInlineScriptIfNeeded();</p> </li> <li>boolean preventExecution = false;</li> <li>for( DomNode o = node; o != null; o = o.getParentNode() ) {</li> <li>if( o instanceof HtmlInlineFrame || o instanceof HtmlNoFrames) {</li> <li>preventExecution = true;</li> <li>break;</li> <li>}</li> <li>}</li> <li>if( !preventExecution )</li> <li>executeInlineScriptIfNeeded();<br> return response;<br> }</li> </ul> <p>Index: src/test/java/com/gargoylesoftware/htmlunit/html/HtmlFrameSetTest.java</p> <p>===================================================================</p> <p>--- src/test/java/com/gargoylesoftware/htmlunit/html/HtmlFrameSetTest.java (revision 1363)</p> <p>+++ src/test/java/com/gargoylesoftware/htmlunit/html/HtmlFrameSetTest.java (working copy)</p> <p>@@ -291,4 +291,35 @@</p> <div class="codehilite"><pre><span></span><code> assertEquals(URL_FIRST.toString(), lastAdditionalHeaders.get("Referer")); } </code></pre></div> <p>-}</p> <ul> <li>public void testScriptUnderNoFrames() throws Exception {</li> <li>final String firstContent</li> <li>= "<html><head><title>first</title></head>"</li> <li> <ul> <li>"<frameset cols='100%'>"</li> </ul> </li> <li> <ul> <li>" <frame src='http://second'' id='frame1'/>"</li> </ul> </li> <li> <ul> <li>" <noframes>"</li> </ul> </li> <li> <ul> <li>" <div><script>alert('1');</script></div>"</li> </ul> </li> <li> <ul> <li>" <script src='http://third'></script>"</li> </ul> </li> <li>+" </noframes>"
+
+
+
+}
\ No newline at end of file
Index: src/test/java/com/gargoylesoftware/htmlunit/html/HtmlInlineFrameTest.java
===================================================================
--- src/test/java/com/gargoylesoftware/htmlunit/html/HtmlInlineFrameTest.java (revision 1363)
+++ src/test/java/com/gargoylesoftware/htmlunit/html/HtmlInlineFrameTest.java (working copy)
@@ -37,8 +37,12 @@
*/
package com.gargoylesoftware.htmlunit.html;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
+import java.util.List;
+import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
import com.gargoylesoftware.htmlunit.MockWebConnection;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebTestCase;
@@ -48,6 +52,7 @@
*
@author Ahmed Ashour
/
public class HtmlInlineFrameTest extends WebTestCase {
/*
@@ -122,4 +127,33 @@
assertEquals( "http://third", iframe.getSrcAttribute() );
assertEquals( "Third", ((HtmlPage)iframe.getEnclosedPage()).getTitleText() );
}
-}
+
public void testScriptUnderIFrame() throws Exception {
+
+
+}
\ No newline at end of file
Logged In: YES
user_id=402164
Originator: NO
A quick test with Firefox (see below) shows that:
@asashour: it is a SF "feature" that only issue owner and admins can attach patch to an issue. Patches inline in comments are not really usable. Can you open a new patch issue for your patch or send it directly per mail to me?
<title>first</title> <script> function test() { for (i in {testNoScript: 0, testIFrame: 0, testNoFrame: 0}) { var oNode = document.getElementById(i); alert(i + "\ninnerHTML: " + oNode.innerHTML + "\n" + "firstChild: " + oNode.firstChild + " (" + oNode.firstChild.tagName + ")") } } </script> blibliLogged In: YES
user_id=950730
Originator: NO
Patch 1696787 has been submitted:
http://sourceforge.net/tracker/index.php?func=detail&aid=1696787&group_id=47038&atid=448268
Logged In: YES
user_id=402164
Originator: NO
Now fixed in SVN. Thanks for the patch (I've refactored it a bit to avoid duplication of logic determining if script execution should be skipped).
The failing test was due to a missing .
Logged In: YES
user_id=1612820
Originator: YES
But they are not returned by the document.getHtmlElementsByTagName() function.
<script> function checknoframes() { // the divs under <noframes> and <iframe> are not returned alert('divs:' + document.getElementsByTagName("div").length); var div = document.createElement("div"); div.id="div4"; document.getElementById("iframe1").appendChild(div); // the div appended in the previous step is returned alert('divs:' + document.getElementsByTagName("div").length); // ? alert(document.getElementById("div3").innerHTML); } </script> <noframes> <div id="div1"></div> </noframes>