[ htmlunit-Bugs-1185389 ] document.write() does not add nodes to correct parent

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ htmlunit-Bugs-1185389 ] document.write() does not add nodes to correct parent

SourceForge.net
Bugs item #1185389, was opened at 2005-04-18 21:39
Message generated for change (Comment added) made by mguillem
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=448266&aid=1185389&group_id=47038

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
Status: Open
Resolution: Accepted
Priority: 7
Private: No
Submitted By: Bruce Faulkner (brfaul)
Assigned to: Nobody/Anonymous (nobody)
Summary: document.write() does not add nodes to correct parent

Initial Comment:
When using document.write() to create new HTML
elements to be added to the DOM, the elements are not
added to the correct parent node in the DOM tree.

The problem appears to be in the write() method of the
Document host object called by jsxFunction_write(). The
write() method sets the current node of the DOM as
follows:

// get the node at which end the parsed content should
be added
HtmlElement current = getLastHtmlElement
(page.getDocumentElement());

The write() method then makes use of the current node
to call the following code to insert the new node:
((HTMLElement) getJavaScriptNode(current))
.jsxFunction_insertAdjacentHTML
(HTMLElement.POSITION_BEFORE_END,
bufferedContent);

The problem occurs when multiple nodes are being
created via multiple document.write() calls in the same
script. Each successive call to document.write() adds
its node to the previously created node. We discovered
this problem when trying to add option nodes to a select
via a loop in JavaScript code, but I've created (and
attached) a simple test case which simply calls
document.write() 3 times within the same script to
create 3 paragraph nodes.


I'm not quite yet sure how to fix cleanly this situation,
but I've attached a patch which includes a new test in
DocumentTest which illustrates the failing condition.


The relevant output from the test I've created is included
here:

[java] .......................................resultPage.asXml():
com.gargoylesoftware.htmlunit.html.HtmlPage@b3c24f
[java]   <html>
[java]     <head>
[java]       <title>
[java]         foo
[java]       </title>
[java]     </head>
[java]    
[java]
[java]     <body id="theBody">
[java]      
[java]
[java]       <script>
[java]        
[java] document.write('<p id="para1">Paragraph #1</p>');
[java] document.write('<p id="para2">Paragraph #2</p>');
[java] document.write('<p id="para3">Paragraph #3</p>');
[java] [DEBUG] Document - No javascript class found for
element <p>. Using HTMLElement
[java] [DEBUG] Document - write: <p
id="para3">Paragraph #3</p>
[java] alert(document.getElementById
('para1').parentNode.id);
[java] alert(document.getElementById
('para2').parentNode.id);
[java] [DEBUG] Document - parsing buffered content: <p
id="para3">Paragraph #3</p>
[java] [DEBUG] Document - current: HtmlParagraph[<p
id="para2">]
[java] alert(document.getElementById
('para3').parentNode.id);
[java]
[java]       </script>
[java]       <p id="para1">
[java]         Paragraph #1
[java]         <p id="para2">
[java] [DEBUG] Document - No javascript class found for
element <p>. Using HTMLElement
[java]           Paragraph #2
[java]           <p id="para3">
[java]             Paragraph #3
[java]           </p>
[java]         </p>
[java]       </p>
[java]      
[java]
[java]     </body>
[java] [DEBUG] Document - No javascript class found for
element <p>. Using HTMLElement
[java] [DEBUG] HtmlPage - Script node added: <script/>
[java]
[java]   </html>


----------------------------------------------------------------------

>Comment By: Marc Guillemot (mguillem)
Date: 2006-11-01 11:45

Message:
Logged In: YES
user_id=402164

unit test added:
com.gargoylesoftware.htmlunit.javascript.host.DocumentTest#testWriteAddNodesToCorrectParent

----------------------------------------------------------------------

Comment By: Andre Soereng (asoreng)
Date: 2006-10-13 16:14

Message:
Logged In: YES
user_id=1400162

Any updates on this? This seems like a real showstopper
issue since htmlunit can't handle simple scripts like:

<script>
  document.write("<table>");
  document.write("<tr><td>hello world</td></tr>")
  document.write("<tr><td>hello universe</td></tr>")
  document.write("</table>");
</script>

Unsure if your proposed solution with delaying execution
until </script> will work in cases such as:

document.write("<span id='mySpan'/>");
document.getElementById("mySpan").tagName;




----------------------------------------------------------------------

Comment By: Marc Guillemot (mguillem)
Date: 2005-04-28 22:14

Message:
Logged In: YES
user_id=402164

I think that the right way to fix it is:
- execute the script first when the parser encounters the
closing tag </script>
- use HTMLElement.POSITION_AFTER_END instead of
HTMLElement.POSITION_BEFORE_END

We need to perform similar change for frames to trigger
frame load first after </frameset> is encountered.

----------------------------------------------------------------------

You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=448266&aid=1185389&group_id=47038

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop