tsbase
Version:
Base class libraries for TypeScript
96 lines • 4.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsxRenderer = exports.ParseJsx = exports.Fragment = void 0;
var tslib_1 = require("tslib");
var Guid_1 = require("../../System/Guid");
var Strings_1 = require("../../System/Strings");
var voidElementTagNames = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
exports.Fragment = 'fragment';
function ParseJsx(nodeName, attributes) {
var _a;
var children = [];
for (var _i = 2; _i < arguments.length; _i++) {
children[_i - 2] = arguments[_i];
}
return { nodeName: nodeName, attributes: attributes, children: (_a = []).concat.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(children), false)) };
}
exports.ParseJsx = ParseJsx;
var JsxRenderer = /** @class */ (function () {
function JsxRenderer() {
}
JsxRenderer.RenderJsx = function (jsx, documentRef, globalAttributes) {
if (documentRef === void 0) { documentRef = globalThis.document || null; }
if (globalAttributes === void 0) { globalAttributes = {}; }
return JsxRenderer.transformJsxToHtml(jsx, documentRef, globalAttributes)
.replace(/<(f|.f)ragment>/g, Strings_1.Strings.Empty);
};
JsxRenderer.addElementEventListener = function (attributeName, handler, element, documentRef) {
if (documentRef) {
var event_1 = attributeName.split('on')[1];
var id_1;
if (element.includes(' id')) {
id_1 = element.split(' id="')[1].split('"')[0];
}
else {
id_1 = Guid_1.Guid.NewGuid();
element += " id=\"".concat(id_1, "\"");
}
setTimeout(function () {
var _a;
try {
(_a = documentRef.querySelector("[id=\"".concat(id_1, "\"]"))) === null || _a === void 0 ? void 0 : _a.addEventListener(event_1, handler);
}
catch ( /* empty */_b) { /* empty */ }
});
}
return element;
};
// eslint-disable-next-line complexity
JsxRenderer.transformJsxToHtml = function (jsx, documentRef, globalAttributes) {
var e_1, _a;
if (globalAttributes === void 0) { globalAttributes = {}; }
jsx = typeof jsx.nodeName === 'function' ? jsx.nodeName(tslib_1.__assign(tslib_1.__assign({}, globalAttributes), jsx.attributes), jsx.children) : jsx;
var element = "<".concat(jsx.nodeName);
for (var key in jsx.attributes) {
var value = jsx.attributes[key];
if (key.startsWith('on')) {
element = this.addElementEventListener(key, value, element, documentRef);
}
else {
var shouldAddAttribute = value !== undefined && value !== null && !(typeof value === 'boolean' && value === false);
if (shouldAddAttribute) {
element += " ".concat(key, "=\"").concat(value.toString(), "\"");
}
}
}
element += '>';
try {
for (var _b = tslib_1.__values(jsx.children || []), _c = _b.next(); !_c.done; _c = _b.next()) {
var child = _c.value;
if (typeof child === 'string' || typeof child === 'number') {
element += child.toString()
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
else if (child) {
element += JsxRenderer.transformJsxToHtml(child, documentRef, globalAttributes);
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return "".concat(element).concat(typeof jsx.nodeName === 'string' && !voidElementTagNames.includes(jsx.nodeName) ?
"</".concat(jsx.nodeName, ">") : Strings_1.Strings.Empty);
};
return JsxRenderer;
}());
exports.JsxRenderer = JsxRenderer;
//# sourceMappingURL=Jsx.js.map