epiphany/node_modules/@ronilaukkarinen/stylelint-a11y/dist/rules/media-prefers-reduced-motion/index.js
2023-12-09 22:48:07 -08:00

143 lines
No EOL
7.1 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = _default;
exports.ruleName = exports.messages = void 0;
var _stylelint = require("stylelint");
var _postcss = require("postcss");
var _isStandardSyntaxRule = _interopRequireDefault(require("stylelint/lib/utils/isStandardSyntaxRule"));
var _isStandardSyntaxSelector = _interopRequireDefault(require("stylelint/lib/utils/isStandardSyntaxSelector"));
var _isStandardSyntaxAtRule = _interopRequireDefault(require("stylelint/lib/utils/isStandardSyntaxAtRule"));
var _isCustomSelector = _interopRequireDefault(require("stylelint/lib/utils/isCustomSelector"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var ruleName = 'a11y/media-prefers-reduced-motion';
exports.ruleName = ruleName;
var messages = _stylelint.utils.ruleMessages(ruleName, {
expected: function expected(selector) {
return "Expected ".concat(selector, " is used with @media (prefers-reduced-motion)");
}
});
exports.messages = messages;
var targetProperties = ['transition', 'animation', 'animation-name'];
function checkChildrenNodes(childrenNodes, currentSelector, parentNode) {
return childrenNodes.some(function (declaration) {
var index = targetProperties.indexOf(declaration.prop);
if (currentSelector === 'animation-name' && targetProperties[index] === 'animation') return true;
if (currentSelector !== targetProperties[index]) return false;
if (declaration.value !== 'none') return false;
return index >= 0 && parentNode.params.indexOf('prefers-reduced-motion') >= 0;
});
}
function check(selector, node) {
var declarations = node.nodes;
var params = node.parent.params;
var parentNodes = node.parent.nodes;
if (!declarations) return true;
if (!(0, _isStandardSyntaxSelector["default"])(selector)) {
return true;
}
if ((0, _isCustomSelector["default"])(selector)) {
return true;
}
var currentSelector = null;
var declarationsIsMatched = declarations.some(function (declaration) {
var noMatchedParams = !params || params.indexOf('prefers-reduced-motion') === -1;
var index = targetProperties.indexOf(declaration.prop);
currentSelector = targetProperties[index];
if (targetProperties.indexOf(declaration.prop) >= 0 && declaration.value === 'none') {
return false;
}
return index >= 0 && noMatchedParams;
});
if (!declarationsIsMatched) return true;
if (declarationsIsMatched) {
var parentMatchedNode = parentNodes.some(function (parentNode) {
if (!parentNode || !parentNode.nodes) return;
return parentNode.nodes.some(function (childrenNode) {
var childrenNodes = childrenNode.nodes;
if (childrenNode.type === 'atrule' && childrenNode.params.indexOf('prefers-reduced-motion') >= 0) {
return childrenNodes.some(function (declaration) {
var index = targetProperties.indexOf(declaration.prop);
if (currentSelector === 'animation-name' && targetProperties[index] === 'animation') return true;
if (currentSelector !== targetProperties[index]) return false;
if (declaration.value !== 'none') return false;
return index >= 0;
});
}
if (!parentNode.params || !Array.isArray(childrenNodes) || selector !== childrenNode.selector) return false;
return checkChildrenNodes(childrenNodes, currentSelector, parentNode);
});
});
if (!parentMatchedNode) return false;
return true;
}
return true;
}
function _default(actual, _, context) {
return function (root, result) {
var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
actual: actual
});
if (!validOptions || !actual) {
return;
}
root.walk(function (node) {
var selector = null;
if (node.type === 'rule') {
if (!(0, _isStandardSyntaxRule["default"])(node)) {
return;
}
selector = node.selector;
} else if (node.type === 'atrule' && node.name === 'page' && node.params) {
if (!(0, _isStandardSyntaxAtRule["default"])(node)) {
return;
}
selector = node.params;
}
if (!selector) {
return;
}
var isAccepted = check(selector, node);
if (context.fix && !isAccepted) {
var media = (0, _postcss.parse)('@media screen and (prefers-reduced-motion: reduce) {}');
media.nodes.forEach(function (o) {
o.raws.after = '\n';
});
var cloneRule = node.clone();
cloneRule.raws = _objectSpread(_objectSpread({}, cloneRule.raws), {}, {
before: '\n',
after: '\n',
semicolon: true
});
cloneRule.nodes.forEach(function (o) {
if (o.prop === 'animation-name') {
o.prop = 'animation';
}
if (targetProperties.indexOf(o.prop) >= 0) {
o.value = 'none';
}
});
media.first.append(cloneRule);
node.before(media);
return;
}
if (!isAccepted) {
_stylelint.utils.report({
index: node.lastEach,
message: messages.expected(selector),
node: node,
ruleName: ruleName,
result: result
});
}
});
};
}