#ifndef EventRetargeter_h
#define EventRetargeter_h
#include "ContainerNode.h"
#include "EventContext.h"
#include "ShadowRoot.h"
#include <wtf/HashMap.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RefPtr.h>
#if ENABLE(SVG)
#include "SVGElementInstance.h"
#include "SVGNames.h"
#include "SVGUseElement.h"
#endif
namespace WebCore {
class EventTarget;
class FocusEvent;
class MouseEvent;
class Node;
#if ENABLE(TOUCH_EVENTS) && !PLATFORM(IOS)
class TouchEvent;
#endif
class TreeScope;
enum EventDispatchBehavior {
RetargetEvent,
StayInsideShadowDOM
};
class EventRetargeter {
public:
static void calculateEventPath(Node*, Event*, EventPath&);
static void adjustForMouseEvent(Node*, const MouseEvent&, EventPath&);
static void adjustForFocusEvent(Node*, const FocusEvent&, EventPath&);
#if ENABLE(TOUCH_EVENTS) && !PLATFORM(IOS)
typedef Vector<RefPtr<TouchList> > EventPathTouchLists;
static void adjustForTouchEvent(Node*, const TouchEvent&, EventPath&);
#endif
static EventTarget* eventTargetRespectingTargetRules(Node* referenceNode);
private:
typedef Vector<RefPtr<Node> > AdjustedNodes;
typedef HashMap<TreeScope*, Node*> RelatedNodeMap;
enum EventWithRelatedTargetDispatchBehavior {
StopAtBoundaryIfNeeded,
DoesNotStopAtBoundary
};
static void adjustForRelatedTarget(const Node*, EventTarget* relatedTarget, EventPath&);
static void calculateAdjustedNodes(const Node*, const Node* relatedNode, EventWithRelatedTargetDispatchBehavior, EventPath&, AdjustedNodes&);
static void buildRelatedNodeMap(const Node*, RelatedNodeMap&);
static Node* findRelatedNode(TreeScope*, RelatedNodeMap&);
#if ENABLE(TOUCH_EVENTS) && !PLATFORM(IOS)
static void adjustTouchList(const Node*, const TouchList*, const EventPath&, EventPathTouchLists&);
#endif
};
inline EventTarget* EventRetargeter::eventTargetRespectingTargetRules(Node* referenceNode)
{
ASSERT(referenceNode);
if (referenceNode->isPseudoElement())
return referenceNode->parentNode();
#if ENABLE(SVG)
if (!referenceNode->isSVGElement() || !referenceNode->isInShadowTree())
return referenceNode;
Element* shadowHostElement = toShadowRoot(referenceNode->treeScope()->rootNode())->host();
if (!shadowHostElement || !shadowHostElement->hasTagName(SVGNames::useTag))
return referenceNode;
SVGUseElement* useElement = static_cast<SVGUseElement*>(shadowHostElement);
if (SVGElementInstance* instance = useElement->instanceForShadowTreeElement(referenceNode))
return instance;
#endif
return referenceNode;
}
}
#endif // EventRetargeter_h