UNPKG

3.24 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = pickAttrs;
8var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
9var attributes = "accept acceptCharset accessKey action allowFullScreen allowTransparency\n alt async autoComplete autoFocus autoPlay capture cellPadding cellSpacing challenge\n charSet checked classID className colSpan cols content contentEditable contextMenu\n controls coords crossOrigin data dateTime default defer dir disabled download draggable\n encType form formAction formEncType formMethod formNoValidate formTarget frameBorder\n headers height hidden high href hrefLang htmlFor httpEquiv icon id inputMode integrity\n is keyParams keyType kind label lang list loop low manifest marginHeight marginWidth max maxLength media\n mediaGroup method min minLength multiple muted name noValidate nonce open\n optimum pattern placeholder poster preload radioGroup readOnly rel required\n reversed role rowSpan rows sandbox scope scoped scrolling seamless selected\n shape size sizes span spellCheck src srcDoc srcLang srcSet start step style\n summary tabIndex target title type useMap value width wmode wrap";
10var eventsName = "onCopy onCut onPaste onCompositionEnd onCompositionStart onCompositionUpdate onKeyDown\n onKeyPress onKeyUp onFocus onBlur onChange onInput onSubmit onClick onContextMenu onDoubleClick\n onDrag onDragEnd onDragEnter onDragExit onDragLeave onDragOver onDragStart onDrop onMouseDown\n onMouseEnter onMouseLeave onMouseMove onMouseOut onMouseOver onMouseUp onSelect onTouchCancel\n onTouchEnd onTouchMove onTouchStart onScroll onWheel onAbort onCanPlay onCanPlayThrough\n onDurationChange onEmptied onEncrypted onEnded onError onLoadedData onLoadedMetadata\n onLoadStart onPause onPlay onPlaying onProgress onRateChange onSeeked onSeeking onStalled onSuspend onTimeUpdate onVolumeChange onWaiting onLoad onError";
11var propList = "".concat(attributes, " ").concat(eventsName).split(/[\s\n]+/);
12
13/* eslint-enable max-len */
14var ariaPrefix = 'aria-';
15var dataPrefix = 'data-';
16function match(key, prefix) {
17 return key.indexOf(prefix) === 0;
18}
19/**
20 * Picker props from exist props with filter
21 * @param props Passed props
22 * @param ariaOnly boolean | { aria?: boolean; data?: boolean; attr?: boolean; } filter config
23 */
24function pickAttrs(props) {
25 var ariaOnly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
26 var mergedConfig;
27 if (ariaOnly === false) {
28 mergedConfig = {
29 aria: true,
30 data: true,
31 attr: true
32 };
33 } else if (ariaOnly === true) {
34 mergedConfig = {
35 aria: true
36 };
37 } else {
38 mergedConfig = (0, _objectSpread2.default)({}, ariaOnly);
39 }
40 var attrs = {};
41 Object.keys(props).forEach(function (key) {
42 if (
43 // Aria
44 mergedConfig.aria && (key === 'role' || match(key, ariaPrefix)) ||
45 // Data
46 mergedConfig.data && match(key, dataPrefix) ||
47 // Attr
48 mergedConfig.attr && propList.includes(key)) {
49 attrs[key] = props[key];
50 }
51 });
52 return attrs;
53}
\No newline at end of file