source: frontend/node_modules/@svgr/hast-util-to-babel-ast/lib/getAttributes.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.0 KB
Line 
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var t = _interopRequireWildcard(require("@babel/types"));
7
8var _util = require("./util");
9
10var _stringToObjectStyle = _interopRequireDefault(require("./stringToObjectStyle"));
11
12var _mappings = require("./mappings");
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
17
18function _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
20function convertAriaAttribute(kebabKey) {
21 const [aria, ...parts] = kebabKey.split('-');
22 return `${aria}-${parts.join('').toLowerCase()}`;
23}
24
25function 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
47function 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
64const 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
79var _default = getAttributes;
80exports.default = _default;
Note: See TracBrowser for help on using the repository browser.