<use> target/currentTarget handling

Good morning svg crowd,

I've looked again at WebKit's SVGUseElement/SVGElementInstance  
implementation and came across a problem.
The spec is very vague about the differences between target/ 
currentTarget.

Please check the testcase below and its discussion.
Would be _great_ to get some feedback, especially it would be nice to  
get Opera/FF/IE9/WebKit on the same track.

Testcase
==================

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink 
">
    <defs>
        <rect id="rect" fill="red" width="100" height="100"  
onclick="eventHandler(evt)"/>
    </defs>

    <use id="use" xlink:href="#rect"/>
    <text x="100" y="150" font-size="50" onclick="runTest()">Click me! 
</text>

    <script>
        var rect = document.getElementById("rect");
        var use = document.getElementById("use");
        var rectInstance = use.instanceRoot;

        function eventHandler(evt)
        {
            alert('target: ' + evt.target + ' current target: ' +  
evt.currentTarget);
        }

        function fireSimulatedMouseClickEvent(eventTarget)
        {
            var event = document.createEvent("MouseEvents");
            event.initMouseEvent("click", true, true,  
document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0,  
null);

            eventTarget.dispatchEvent(event);
        }

        function runTest()
        {
            fireSimulatedMouseClickEvent(rect);
            fireSimulatedMouseClickEvent(rectInstance);
        }
    </script>
</svg>

How to test
==================

#1) Click on the red rectangle. Should result in:
alert: "target: [object SVGElementInstance] current target: [object  
SVGElementInstance]"

#2) Click on the text. _I THINK_ it should result in: (that's the path  
I've chosen for WebKit)
alert: "target: [object SVGRectElement] current target: [object  
SVGRectElement]"
alert: "target: [object SVGElementInstance] current target: [object  
SVGElementInstance]"

Discussion
==================

#1) The first one is logical, the spec says:
If event attributes are assigned to referenced elements, then the  
actual target for the event will be the SVGElementInstance object  
within the "instance tree" corresponding to the given referenced  
element.

#2) This is tricky.  think it should be possible to dispatch an event  
manually to every element in the DOM.
So dispatching directly to "rect", should work as expected, and not  
end up on a SVGElementInstance. This should explain the expected  
result for the first alert(), as stated above. Is it correct?

When dispatching manually to the "rectInstance" WebKit delivers it to  
the SVGElementInstance, and Opera does not. It says "target: [object  
SVGRectElement] current target: [object SVGRectElement]"
Can anyone explain what's happening? Just a bug in Opera? I think the  
spec clearly states here, that the event should end up on the  
SVGElementInstance.

Further questions
==================

Spec says: The event handling for the non-exposed tree works as if the  
referenced element had been textually included as a deeply cloned  
child of the 'use' element, except that events are dispatched to the  
SVGElementInstance objects. The event's target and currentTarget  
attributes are set to the SVGElementInstance that corresponds to the  
target and current target elements in the referenced subtree
.....
The currentTarget attribute of the event can be used to determine  
through which object an event listener was invoked.

What does the last sentence mean in practice? Does it talk about  
target pointing to eg. SVGRectElement, but currentTarget pointing to a  
SVGElementInstance?
Of course when the event bubbles currentTarget and target are not  
equal anymore, but I'm wondering about the target phase. The sentence  
above makes me thing there is some difference that I'm overlooking.
To rephrase: When is currentTarget != target? Only when capturing/ 
bubbling?

Can anyone enlighten me?

Thanks in advance,
Niko

Received on Wednesday, 5 May 2010 12:07:18 UTC