Closed Bug 1101385 Opened 8 years ago Closed 8 years ago

document.importNode does not execute JavaScript in SVG

Categories

(Core :: SVG, defect)

33 Branch
x86_64
Windows 7
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 752231

People

(Reporter: simon.eu, Unassigned)

Details

Attachments

(2 files)

Attached file importNodeDemo.html
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0
Build ID: 20141106120505

Steps to reproduce:

Please see the attached file. The SVG appended to the DOM contains JavaScript code to show an alert window.

Shouldn't the script code be executed? If not, is there an alternative to importNode?


Actual results:

Firefox 33: Nothing happens
IE 11: Nothing happens
Chrome 38: Alert is shown
Component: Untriaged → SVG
Product: Firefox → Core
This looks like a dupe of bug 752231. I'm not 100% sure, so please reopen if you disagree, but do carefully read the dupe and relevant bits of spec.

AIUI,

https://dom.spec.whatwg.org/#dom-document-importnode

says to run the cloning steps, which will set the "already started" flag on the script element, and which means it no longer gets executed. IE11 matches Gecko's behaviour. Current Chrome doesn't, which looks like a regression to my (amateur) eyes.
Status: UNCONFIRMED → RESOLVED
Closed: 8 years ago
Resolution: --- → DUPLICATE
Duplicate of bug: 752231
Thank you Gijs, yes, both describe the same bug/fact. It is a bit unfortunate that the specs define the behaviour such that scripts are not executed. Is there any way to work around this? 

Thanks also for the link to the whatwg reference.
(In reply to Simon A. Eugster from comment #2)
> Thank you Gijs, yes, both describe the same bug/fact. It is a bit
> unfortunate that the specs define the behaviour such that scripts are not
> executed. Is there any way to work around this? 
> 
> Thanks also for the link to the whatwg reference.

My initial observation would be that in the code in the testcase, you don't need to use importNode at all, you could just assign to the innerHTML of #target instead of the temp div... I assume the real situation is more complex.

Is using adoptNode an option considering the real situation?

Finally, you could resort to something like target.innerHTML = scriptEl.outerHTML... but that's pretty ugly. :-)
Attached file innerHtmlDemo.html
Actually the real situation is such that I can quite much do what I want in the code, but the script tag in the SVG has to be executed. No way around that.

I have attached a demo which uses innerHTML: The script is not executed here either, nor is it when using adoptNode (unless I used it in an incorrect way).

There seems to be no information about what to do with script tags with adoptNode in the DOM standard. Maybe I just missed it?
Huh! I swear I tested this and it worked, but you're completely right, that won't help. :-\

The best I can think of, in that case, is to manually create new elements and insert them in place of the 'dead' script elements using replaceChild (or .remove() and .appendChild()). I just doublechecked and that should work.
You need to log in before you can comment on or make changes to this bug.