Closed Bug 628837 Opened 13 years ago Closed 13 years ago

Script in document styled with XSLT stylesheet runs twice

Categories

(Core :: XSLT, defect)

1.9.2 Branch
x86
Windows 7
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 380828

People

(Reporter: Rob.Simpson, Unassigned)

References

()

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13

When adding a cell to a row with insertCell and populating it using appendChild with a text node created with createTextNode, the text node is displayed twice. It only happens in Firefox; in Opera, Safari and Chrome the text node only appears once. And it only happens when there is a xsl-stylesheet.

Example: http://www.ExampleOnly.com/insert-cell.html

<table>
   <tr style="background-color: #ccccff"><th>Static<br/>Column</th><th>Inserted<br/>Empty Column</th><th>Inserted Column<br/>with Text Node</th></tr>
   <tr id="my-data-row"><td>Col #1</td>
<script>
var cell = document.getElementById("my-data-row").insertCell(-1);
cell = document.getElementById("my-data-row").insertCell(-1);
cell.appendChild(document.createTextNode("Col #3"));
</script>
   </tr>
</table>


Reproducible: Always

Steps to Reproduce:
1. use insertCell to add a cell to a table row
2. use createTextNode to create a text node
3. append the text node to the end of the row using appendChild
4. include a style sheet with XSLT template for the site's navigation or other common elements

Actual Results:  
Firefox output:

Static Inserted     Inserted Column
Column Empty Column With Text Node
Col #1              Col #3          Col #3


Expected Results:  
Expected output (actual output in other browsers):

Static Inserted     Inserted Column
Column Empty Column With Text Node
Col #1              Col #3


The text node "Col #3" appears twice in Firefox. It should only appear once, like it does in other browsers.
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.14pre) Gecko/20110125 Namoroka/3.6.14pre

Able to reproduce. Text node "Col#3" appears twice in firefox.
Product: Firefox → Core
QA Contact: general → general
Version: unspecified → 1.9.2 Branch
The script runs twice, presumably due to the XSLT stylesheet (presumably once in the source document and one in the result document?)...  Jonas, I thought we had bugs on this already.
Component: General → XSLT
QA Contact: general → xslt
Summary: TableRow insertCell createTextNode appendChild with style sheet results in same text node displayed twice → Script in document styled with XSLT stylesheet runs twice
Whiteboard: DUPEME
I'm pretty sure we disable the scriptloader in the source document??
Boris,

You're right. This is a dup of Bug 380828. Sorry 'bout that.
With that information ("script runs twice") I took another look at the code in the hopes of finding a workaround. What I find curious is this: before submitting the bug, I wondered if all of the script code was running twice, so to test that theory I had added the "insertCell" for the second column, without the child text node, expecting that cell to be duplicated as well, but it wasn't. Hence the original description of the bug was "TableRow insertCell createTextNode appendChild with style sheet results in same text node displayed twice" since the bug appeared to depend on _both_ the insertCell and appending the child text node.

If the underlying cause is that the whole script is run twice, I would expect the first "insertCell" would be run twice, resulting in the empty second column being duplicated also.
> I'm pretty sure we disable the scriptloader in the source document??

Don't seem to...  Or at least the script is running twice _somehow_, so there have to be different script nodes involved, right?

> I would expect the first "insertCell" would be run twice

It is.  If you look at the DOM, there are 5 cells in that table row.  There's an empty cell between the two cells that say "Col #3".  But it's empty, so doesn't take up any space....
Status: UNCONFIRMED → RESOLVED
Closed: 13 years ago
Resolution: --- → DUPLICATE
Ahhh thanks, Boris.

Adding "min-width: 1%" to the style in the "Example" now makes that more obvious.
Finally figured out the workaround for this issue. http://www.ExampleOnly.com/insert-cell.html now has the code both without and with the workaround, which is to wrap any script code like this:

a = document.createElement("a");
a.setAttribute("onclick", "return true");
if ("onclick" in a) {
   ... script code ...
}

Of course, that's pretty ugly.... :-)
It turns out, the first time the script runs the document object is an XMLDocument and the second time it runs it's an HTMLDocument as shown here:

    http://www.HTML-5.com/javascript/html-document-interface/#getelementsbyname

    (If that link does not automatically scroll down the page, put the cursor into the address bar and hit Return. Not honoring the fragment identifier in the URL when the page is initially loaded is a regression from May 2010, Bug #645075.)

Different methods are available depending on the type of the document object. For example, getElementsByName is available when it's an HTMLDocument but not when it's an XMLDocument. So a simpler workaround is:

if (document.getElementsByName) {
   ... this script code will run just once ...
}

When this is fixed, it would be preferable that the script runs later, when the document object is an HTMLDocument.
Whiteboard: DUPEME
You need to log in before you can comment on or make changes to this bug.