| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | exports.__esModule = true;
|
|---|
| 4 | exports.default = void 0;
|
|---|
| 5 |
|
|---|
| 6 | var t = _interopRequireWildcard(require("@babel/types"));
|
|---|
| 7 |
|
|---|
| 8 | var _util = require("./util");
|
|---|
| 9 |
|
|---|
| 10 | var _stringToObjectStyle = _interopRequireDefault(require("./stringToObjectStyle"));
|
|---|
| 11 |
|
|---|
| 12 | var _mappings = require("./mappings");
|
|---|
| 13 |
|
|---|
| 14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|---|
| 15 |
|
|---|
| 16 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|---|
| 17 |
|
|---|
| 18 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|---|
| 19 |
|
|---|
| 20 | function convertAriaAttribute(kebabKey) {
|
|---|
| 21 | const [aria, ...parts] = kebabKey.split('-');
|
|---|
| 22 | return `${aria}-${parts.join('').toLowerCase()}`;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | function getKey(key, value, node) {
|
|---|
| 26 | const lowerCaseKey = key.toLowerCase();
|
|---|
| 27 | const mappedElementAttribute = _mappings.ELEMENT_ATTRIBUTE_MAPPING[node.name] && _mappings.ELEMENT_ATTRIBUTE_MAPPING[node.name][lowerCaseKey];
|
|---|
| 28 | const mappedAttribute = _mappings.ATTRIBUTE_MAPPING[lowerCaseKey];
|
|---|
| 29 |
|
|---|
| 30 | if (mappedElementAttribute || mappedAttribute) {
|
|---|
| 31 | return t.jsxIdentifier(mappedElementAttribute || mappedAttribute);
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | const kebabKey = (0, _util.kebabCase)(key);
|
|---|
| 35 |
|
|---|
| 36 | if (kebabKey.startsWith('aria-')) {
|
|---|
| 37 | return t.jsxIdentifier(convertAriaAttribute(kebabKey));
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | if (kebabKey.startsWith('data-')) {
|
|---|
| 41 | return t.jsxIdentifier(kebabKey);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | return t.jsxIdentifier(key);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | function getValue(key, value) {
|
|---|
| 48 | // Handle className
|
|---|
| 49 | if (Array.isArray(value)) {
|
|---|
| 50 | return t.stringLiteral((0, _util.replaceSpaces)(value.join(' ')));
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | if (key === 'style') {
|
|---|
| 54 | return t.jsxExpressionContainer((0, _stringToObjectStyle.default)(value));
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if ((0, _util.isNumeric)(value)) {
|
|---|
| 58 | return t.jsxExpressionContainer(t.numericLiteral(Number(value)));
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | return t.stringLiteral((0, _util.replaceSpaces)(value));
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | const getAttributes = node => {
|
|---|
| 65 | const keys = Object.keys(node.properties);
|
|---|
| 66 | const attributes = [];
|
|---|
| 67 | let index = -1;
|
|---|
| 68 |
|
|---|
| 69 | while (++index < keys.length) {
|
|---|
| 70 | const key = keys[index];
|
|---|
| 71 | const value = node.properties[key];
|
|---|
| 72 | const attribute = t.jsxAttribute(getKey(key, value, node), getValue(key, value, node));
|
|---|
| 73 | attributes.push(attribute);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | return attributes;
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|
| 79 | var _default = getAttributes;
|
|---|
| 80 | exports.default = _default; |
|---|