getAttributes.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var t = _interopRequireWildcard(require("@babel/types"));
  5. var _util = require("./util");
  6. var _stringToObjectStyle = _interopRequireDefault(require("./stringToObjectStyle"));
  7. var _mappings = require("./mappings");
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  10. 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; }
  11. function convertAriaAttribute(kebabKey) {
  12. const [aria, ...parts] = kebabKey.split('-');
  13. return `${aria}-${parts.join('').toLowerCase()}`;
  14. }
  15. function getKey(key, value, node) {
  16. const lowerCaseKey = key.toLowerCase();
  17. const mappedElementAttribute = _mappings.ELEMENT_ATTRIBUTE_MAPPING[node.name] && _mappings.ELEMENT_ATTRIBUTE_MAPPING[node.name][lowerCaseKey];
  18. const mappedAttribute = _mappings.ATTRIBUTE_MAPPING[lowerCaseKey];
  19. if (mappedElementAttribute || mappedAttribute) {
  20. return t.jsxIdentifier(mappedElementAttribute || mappedAttribute);
  21. }
  22. const kebabKey = (0, _util.kebabCase)(key);
  23. if (kebabKey.startsWith('aria-')) {
  24. return t.jsxIdentifier(convertAriaAttribute(kebabKey));
  25. }
  26. if (kebabKey.startsWith('data-')) {
  27. return t.jsxIdentifier(kebabKey);
  28. }
  29. return t.jsxIdentifier(key);
  30. }
  31. function getValue(key, value) {
  32. // Handle className
  33. if (Array.isArray(value)) {
  34. return t.stringLiteral((0, _util.replaceSpaces)(value.join(' ')));
  35. }
  36. if (key === 'style') {
  37. return t.jsxExpressionContainer((0, _stringToObjectStyle.default)(value));
  38. }
  39. if ((0, _util.isNumeric)(value)) {
  40. return t.jsxExpressionContainer(t.numericLiteral(Number(value)));
  41. }
  42. return t.stringLiteral((0, _util.replaceSpaces)(value));
  43. }
  44. const getAttributes = node => {
  45. const keys = Object.keys(node.properties);
  46. const attributes = [];
  47. let index = -1;
  48. while (++index < keys.length) {
  49. const key = keys[index];
  50. const value = node.properties[key];
  51. const attribute = t.jsxAttribute(getKey(key, value, node), getValue(key, value, node));
  52. attributes.push(attribute);
  53. }
  54. return attributes;
  55. };
  56. var _default = getAttributes;
  57. exports.default = _default;