| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {TEXT_NODE} from '../client/HTMLNodeType'; |
| 9 | |
| 10 | /** |
| 11 | * Gets the target node from a native browser event by accounting for |
| 12 | * inconsistencies in browser DOM APIs. |
| 13 | * |
| 14 | * @param {object} nativeEvent Native browser event. |
| 15 | * @return {DOMEventTarget} Target node. |
| 16 | */ |
| 17 | function getEventTarget(nativeEvent) { |
| 18 | // Fallback to nativeEvent.srcElement for IE9 |
| 19 | // https://github.com/facebook/react/issues/12506 |
| 20 | let target = nativeEvent.target || nativeEvent.srcElement || window; |
| 21 | |
| 22 | // Normalize SVG <use> element events #4963 |
| 23 | if (target.correspondingUseElement) { |
| 24 | target = target.correspondingUseElement; |
| 25 | } |
| 26 | |
| 27 | // Safari may fire events on text nodes (Node.TEXT_NODE is 3). |
| 28 | // @see http://www.quirksmode.org/js/events_properties.html |
| 29 | return target.nodeType === TEXT_NODE ? target.parentNode : target; |
| 30 | } |
| 31 | |
| 32 | export default getEventTarget; |