| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports["default"] = void 0;
|
|---|
| 7 | var _ariaQuery = require("aria-query");
|
|---|
| 8 | var _jsxAstUtils = require("jsx-ast-utils");
|
|---|
| 9 | var _schemas = require("../util/schemas");
|
|---|
| 10 | function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|---|
| 11 | function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|---|
| 12 | function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|---|
| 13 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 14 | function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /**
|
|---|
| 15 | * @fileoverview Enforce onmouseover/onmouseout are
|
|---|
| 16 | * accompanied by onfocus/onblur.
|
|---|
| 17 | * @author Ethan Cohen
|
|---|
| 18 | *
|
|---|
| 19 | */ // ----------------------------------------------------------------------------
|
|---|
| 20 | // Rule Definition
|
|---|
| 21 | // ----------------------------------------------------------------------------
|
|---|
| 22 | var schema = (0, _schemas.generateObjSchema)({
|
|---|
| 23 | hoverInHandlers: _objectSpread(_objectSpread({}, _schemas.arraySchema), {}, {
|
|---|
| 24 | description: 'An array of events that need to be accompanied by `onFocus`'
|
|---|
| 25 | }),
|
|---|
| 26 | hoverOutHandlers: _objectSpread(_objectSpread({}, _schemas.arraySchema), {}, {
|
|---|
| 27 | description: 'An array of events that need to be accompanied by `onBlur`'
|
|---|
| 28 | })
|
|---|
| 29 | });
|
|---|
| 30 |
|
|---|
| 31 | // Use `onMouseOver` and `onMouseOut` by default if no config is
|
|---|
| 32 | // passed in for backwards compatibility
|
|---|
| 33 | var DEFAULT_HOVER_IN_HANDLERS = ['onMouseOver'];
|
|---|
| 34 | var DEFAULT_HOVER_OUT_HANDLERS = ['onMouseOut'];
|
|---|
| 35 | var _default = exports["default"] = {
|
|---|
| 36 | meta: {
|
|---|
| 37 | docs: {
|
|---|
| 38 | url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/mouse-events-have-key-events.md',
|
|---|
| 39 | description: 'Enforce that `onMouseOver`/`onMouseOut` are accompanied by `onFocus`/`onBlur` for keyboard-only users.'
|
|---|
| 40 | },
|
|---|
| 41 | schema: [schema]
|
|---|
| 42 | },
|
|---|
| 43 | create: function create(context) {
|
|---|
| 44 | return {
|
|---|
| 45 | JSXOpeningElement: function JSXOpeningElement(node) {
|
|---|
| 46 | var _options$0$hoverInHan, _options$, _options$0$hoverOutHa, _options$2;
|
|---|
| 47 | var name = node.name.name;
|
|---|
| 48 | if (!_ariaQuery.dom.get(name)) {
|
|---|
| 49 | return;
|
|---|
| 50 | }
|
|---|
| 51 | var options = context.options;
|
|---|
| 52 | var hoverInHandlers = (_options$0$hoverInHan = (_options$ = options[0]) === null || _options$ === void 0 ? void 0 : _options$.hoverInHandlers) !== null && _options$0$hoverInHan !== void 0 ? _options$0$hoverInHan : DEFAULT_HOVER_IN_HANDLERS;
|
|---|
| 53 | var hoverOutHandlers = (_options$0$hoverOutHa = (_options$2 = options[0]) === null || _options$2 === void 0 ? void 0 : _options$2.hoverOutHandlers) !== null && _options$0$hoverOutHa !== void 0 ? _options$0$hoverOutHa : DEFAULT_HOVER_OUT_HANDLERS;
|
|---|
| 54 | var attributes = node.attributes;
|
|---|
| 55 |
|
|---|
| 56 | // Check hover in / onfocus pairing
|
|---|
| 57 | var firstHoverInHandlerWithValue = hoverInHandlers.find(function (handler) {
|
|---|
| 58 | var prop = (0, _jsxAstUtils.getProp)(attributes, handler);
|
|---|
| 59 | var propValue = (0, _jsxAstUtils.getPropValue)(prop);
|
|---|
| 60 | return propValue != null;
|
|---|
| 61 | });
|
|---|
| 62 | if (firstHoverInHandlerWithValue != null) {
|
|---|
| 63 | var hasOnFocus = (0, _jsxAstUtils.getProp)(attributes, 'onFocus');
|
|---|
| 64 | var onFocusValue = (0, _jsxAstUtils.getPropValue)(hasOnFocus);
|
|---|
| 65 | if (hasOnFocus === false || onFocusValue === null || onFocusValue === undefined) {
|
|---|
| 66 | context.report({
|
|---|
| 67 | node: (0, _jsxAstUtils.getProp)(attributes, firstHoverInHandlerWithValue),
|
|---|
| 68 | message: "".concat(firstHoverInHandlerWithValue, " must be accompanied by onFocus for accessibility.")
|
|---|
| 69 | });
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | // Check hover out / onblur pairing
|
|---|
| 74 | var firstHoverOutHandlerWithValue = hoverOutHandlers.find(function (handler) {
|
|---|
| 75 | var prop = (0, _jsxAstUtils.getProp)(attributes, handler);
|
|---|
| 76 | var propValue = (0, _jsxAstUtils.getPropValue)(prop);
|
|---|
| 77 | return propValue != null;
|
|---|
| 78 | });
|
|---|
| 79 | if (firstHoverOutHandlerWithValue != null) {
|
|---|
| 80 | var hasOnBlur = (0, _jsxAstUtils.getProp)(attributes, 'onBlur');
|
|---|
| 81 | var onBlurValue = (0, _jsxAstUtils.getPropValue)(hasOnBlur);
|
|---|
| 82 | if (hasOnBlur === false || onBlurValue === null || onBlurValue === undefined) {
|
|---|
| 83 | context.report({
|
|---|
| 84 | node: (0, _jsxAstUtils.getProp)(attributes, firstHoverOutHandlerWithValue),
|
|---|
| 85 | message: "".concat(firstHoverOutHandlerWithValue, " must be accompanied by onBlur for accessibility.")
|
|---|
| 86 | });
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | };
|
|---|
| 91 | }
|
|---|
| 92 | };
|
|---|
| 93 | module.exports = exports.default; |
|---|