handlers.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.element = exports.text = exports.comment = exports.root = void 0;
  4. var t = _interopRequireWildcard(require("@babel/types"));
  5. var _all = _interopRequireDefault(require("./all"));
  6. var _getAttributes = _interopRequireDefault(require("./getAttributes"));
  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. const root = (h, node) => t.program((0, _all.default)(h, node));
  12. exports.root = root;
  13. const comment = (h, node, parent) => {
  14. if (parent.type === 'root') {
  15. return null;
  16. }
  17. const expression = t.jsxEmptyExpression();
  18. t.addComment(expression, 'inner', node.value);
  19. return t.jsxExpressionContainer(expression);
  20. };
  21. exports.comment = comment;
  22. const text = (h, node, parent) => {
  23. if (parent.type === 'root') {
  24. return null;
  25. }
  26. if (node.value.match(/^\s+$/)) {
  27. return null;
  28. }
  29. return t.jsxExpressionContainer(t.stringLiteral(node.value));
  30. };
  31. exports.text = text;
  32. const element = (h, node, parent) => {
  33. const children = (0, _all.default)(h, node);
  34. const selfClosing = children.length === 0;
  35. const name = _mappings.ELEMENT_TAG_NAME_MAPPING[node.tagName] || node.tagName;
  36. const openingElement = t.jsxOpeningElement(t.jsxIdentifier(name), (0, _getAttributes.default)(node), selfClosing);
  37. const closingElement = !selfClosing ? t.jsxClosingElement(t.jsxIdentifier(name)) : null;
  38. const jsxElement = t.jsxElement(openingElement, closingElement, children);
  39. if (parent.type === 'root') {
  40. return t.expressionStatement(jsxElement);
  41. }
  42. return jsxElement;
  43. };
  44. exports.element = element;