pickAttrs.js 3.0 KB

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