Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

97 строки
3.3 KiB

  1. 'use strict';
  2. var _jsxAstUtils = require('jsx-ast-utils');
  3. var _schemas = require('../util/schemas');
  4. var errorMessage = 'Media elements such as <audio> and <video> must have a <track> for captions.'; /**
  5. * @fileoverview <audio> and <video> elements must have a <track> for captions.
  6. * @author Ethan Cohen
  7. *
  8. */
  9. // ----------------------------------------------------------------------------
  10. // Rule Definition
  11. // ----------------------------------------------------------------------------
  12. var MEDIA_TYPES = ['audio', 'video'];
  13. var schema = (0, _schemas.generateObjSchema)({
  14. audio: _schemas.arraySchema,
  15. video: _schemas.arraySchema,
  16. track: _schemas.arraySchema
  17. });
  18. var isMediaType = function isMediaType(context, type) {
  19. var options = context.options[0] || {};
  20. return MEDIA_TYPES.map(function (mediaType) {
  21. return options[mediaType];
  22. }).reduce(function (types, customComponent) {
  23. return types.concat(customComponent);
  24. }, MEDIA_TYPES).some(function (typeToCheck) {
  25. return typeToCheck === type;
  26. });
  27. };
  28. var isTrackType = function isTrackType(context, type) {
  29. var options = context.options[0] || {};
  30. return ['track'].concat(options.track || []).some(function (typeToCheck) {
  31. return typeToCheck === type;
  32. });
  33. };
  34. module.exports = {
  35. meta: {
  36. docs: {
  37. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/media-has-caption.md'
  38. },
  39. schema: [schema]
  40. },
  41. create: function create(context) {
  42. return {
  43. JSXElement: function JSXElement(node) {
  44. var element = node.openingElement;
  45. var type = (0, _jsxAstUtils.elementType)(element);
  46. if (!isMediaType(context, type)) {
  47. return;
  48. }
  49. var mutedProp = (0, _jsxAstUtils.getProp)(element.attributes, 'muted');
  50. var mutedPropVal = (0, _jsxAstUtils.getLiteralPropValue)(mutedProp);
  51. if (mutedPropVal === true) {
  52. return;
  53. }
  54. // $FlowFixMe https://github.com/facebook/flow/issues/1414
  55. var trackChildren = node.children.filter(function (child) {
  56. if (child.type !== 'JSXElement') {
  57. return false;
  58. }
  59. // $FlowFixMe https://github.com/facebook/flow/issues/1414
  60. return isTrackType(context, (0, _jsxAstUtils.elementType)(child.openingElement));
  61. });
  62. if (trackChildren.length === 0) {
  63. context.report({
  64. node: element,
  65. message: errorMessage
  66. });
  67. return;
  68. }
  69. var hasCaption = trackChildren.some(function (track) {
  70. var kindProp = (0, _jsxAstUtils.getProp)(track.openingElement.attributes, 'kind');
  71. var kindPropValue = (0, _jsxAstUtils.getLiteralPropValue)(kindProp) || '';
  72. return kindPropValue.toLowerCase() === 'captions';
  73. });
  74. if (!hasCaption) {
  75. context.report({
  76. node: element,
  77. message: errorMessage
  78. });
  79. }
  80. }
  81. };
  82. }
  83. };