Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Workaround <use> element event path
Browse files Browse the repository at this point in the history
The SVG <use> element has a horrifying "instance tree" related to ShadowDOM, but does not retarget.
The SVGElementInstance (the instanceRoot type) has parts of a Node's API, but not all (like .contains).
For native events the SVGElementInstance will bubble events up to the <use> element, even non-bubbling events like enter
and leave, but it will not do so when dispatchEvent is called on it with a non-bubbling event.
Only Safari, IE, and Chrome implement this monstrosity, and working group seems to agree it should be removed:
http://lists.w3.org/Archives/Public/www-svg/2014Jan/0014.html

This workaround rewites event.target and relatedTarget to refer to <use> elements and not their "instance trees".
Fixes #124
  • Loading branch information
dfreedm committed Jan 28, 2014
1 parent b0dd7bd commit fdf80d6
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
0
];

var HAS_SVG_INSTANCE = (typeof SVGElementInstance !== 'undefined');

/**
* This module is for normalizing events. Mouse and Touch events will be
* collected here, and fire PointerEvents that have the same semantics, no
Expand Down Expand Up @@ -246,6 +248,14 @@
for (var i = 0; i < CLONE_PROPS.length; i++) {
p = CLONE_PROPS[i];
eventCopy[p] = inEvent[p] || CLONE_DEFAULTS[i];
// Work around SVGInstanceElement shadow tree
// Return the <use> element that is represented by the instance for Safari, Chrome, IE.
// This is the behavior implemented by Firefox.
if (HAS_SVG_INSTANCE && (p === 'target' || p === 'relatedTarget')) {
if (eventCopy[p] instanceof SVGElementInstance) {
eventCopy[p] = eventCopy[p].correspondingUseElement;
}
}
}
// keep the semantics of preventDefault
if (inEvent.preventDefault) {
Expand Down

0 comments on commit fdf80d6

Please sign in to comment.