pickAttrs.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = pickAttrs;
  7. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  8. const attributes = `accept acceptcharset accesskey action allowfullscreen allowtransparency
  9. alt async autocomplete autofocus autoplay capture cellpadding cellspacing challenge
  10. charset checked classid classname colspan cols content contenteditable contextmenu
  11. controls coords crossorigin data datetime default defer dir disabled download draggable
  12. enctype form formaction formenctype formmethod formnovalidate formtarget frameborder
  13. headers height hidden high href hreflang htmlfor for httpequiv icon id inputmode integrity
  14. is keyparams keytype kind label lang list loop low manifest marginheight marginwidth max maxlength media
  15. mediagroup method min minlength multiple muted name novalidate nonce open
  16. optimum pattern placeholder poster preload radiogroup readonly rel required
  17. reversed role rowspan rows sandbox scope scoped scrolling seamless selected
  18. shape size sizes span spellcheck src srcdoc srclang srcset start step style
  19. summary tabindex target title type usemap value width wmode wrap`;
  20. const eventsName = `onCopy onCut onPaste onCompositionend onCompositionstart onCompositionupdate onKeydown
  21. onKeypress onKeyup onFocus onBlur onChange onInput onSubmit onClick onContextmenu onDoubleclick onDblclick
  22. onDrag onDragend onDragenter onDragexit onDragleave onDragover onDragstart onDrop onMousedown
  23. onMouseenter onMouseleave onMousemove onMouseout onMouseover onMouseup onSelect onTouchcancel
  24. onTouchend onTouchmove onTouchstart onTouchstartPassive onTouchmovePassive onScroll onWheel onAbort onCanplay onCanplaythrough
  25. onDurationchange onEmptied onEncrypted onEnded onError onLoadeddata onLoadedmetadata
  26. onLoadstart onPause onPlay onPlaying onProgress onRatechange onSeeked onSeeking onStalled onSuspend onTimeupdate onVolumechange onWaiting onLoad onError`;
  27. const propList = `${attributes} ${eventsName}`.split(/[\s\n]+/);
  28. /* eslint-enable max-len */
  29. const ariaPrefix = 'aria-';
  30. const dataPrefix = 'data-';
  31. function match(key, prefix) {
  32. return key.indexOf(prefix) === 0;
  33. }
  34. /**
  35. * Picker props from exist props with filter
  36. * @param props Passed props
  37. * @param ariaOnly boolean | { aria?: boolean; data?: boolean; attr?: boolean; } filter config
  38. */
  39. function pickAttrs(props) {
  40. let ariaOnly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  41. let mergedConfig;
  42. if (ariaOnly === false) {
  43. mergedConfig = {
  44. aria: true,
  45. data: true,
  46. attr: true
  47. };
  48. } else if (ariaOnly === true) {
  49. mergedConfig = {
  50. aria: true
  51. };
  52. } else {
  53. mergedConfig = (0, _extends2.default)({}, ariaOnly);
  54. }
  55. const attrs = {};
  56. Object.keys(props).forEach(key => {
  57. if (
  58. // Aria
  59. mergedConfig.aria && (key === 'role' || match(key, ariaPrefix)) ||
  60. // Data
  61. mergedConfig.data && match(key, dataPrefix) ||
  62. // Attr
  63. mergedConfig.attr && (propList.includes(key) || propList.includes(key.toLowerCase()))) {
  64. attrs[key] = props[key];
  65. }
  66. });
  67. return attrs;
  68. }