@dark-engine/platform-server
Version:
Dark renderer for server
100 lines (99 loc) • 3.11 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
exports.CommentNativeElement = exports.TextNativeElement = exports.TagNativeElement = exports.NativeElement = void 0;
const core_1 = require('@dark-engine/core');
const platform_browser_1 = require('@dark-engine/platform-browser');
const utils_1 = require('../utils');
class NativeElement {
type;
parentElement = null;
constructor(type) {
this.type = type;
}
}
exports.NativeElement = NativeElement;
class TagNativeElement extends NativeElement {
name = null;
attrs = {};
children = [];
constructor(name) {
super(core_1.NodeType.TAG);
this.name = name;
}
appendChild(element) {
if (this.attrs[platform_browser_1.DANGER_HTML_ATTR]) {
(0, utils_1.illegal)(`The element with danger content can't have a children!`);
}
element.parentElement = this;
this.children.push(element);
}
setAttribute(name, value) {
let $name = name === platform_browser_1.CLASS_NAME_ATTR ? platform_browser_1.CLASS_ATTR : name;
if ($name[0] === platform_browser_1.EXCLUDE_ATTR_MARK) return;
if ($name === platform_browser_1.AS_ATTR) $name = name.slice(1, platform_browser_1.AS_ATTR.length);
this.attrs[$name] =
(0, core_1.detectIsString)(value) && $name !== platform_browser_1.DANGER_HTML_ATTR
? (0, utils_1.escape)(value)
: value;
}
render(isOpening) {
const content =
this.name === platform_browser_1.TEXTAREA_TAG
? this.children[0]?.render() || ''
: this.attrs[platform_browser_1.DANGER_HTML_ATTR] || '';
const isVoid = (0, platform_browser_1.detectIsVoidElement)(this.name);
const attrs = getAttributes(this.attrs);
const chunk = isOpening
? isVoid
? `<${this.name}${attrs}>`
: `<${this.name}${attrs}>${content || ''}`
: isVoid
? ''
: `</${this.name}>`;
return chunk;
}
renderToString() {
const content = this.children.map(x => x.renderToString()).join('');
return this.render(true) + content + this.render(false);
}
}
exports.TagNativeElement = TagNativeElement;
class TextNativeElement extends NativeElement {
value = '';
constructor(text) {
super(core_1.NodeType.TEXT);
this.value = (0, utils_1.escape)(text);
}
render() {
return this.value;
}
renderToString() {
return this.render();
}
}
exports.TextNativeElement = TextNativeElement;
class CommentNativeElement extends NativeElement {
value = '';
constructor(text) {
super(core_1.NodeType.COMMENT);
this.value = `<!--${(0, utils_1.escape)(text)}-->`;
}
render() {
return this.value;
}
renderToString() {
return this.render();
}
}
exports.CommentNativeElement = CommentNativeElement;
function getAttributes(map) {
let attrs = '';
for (const key of Object.keys(map)) {
if (key === platform_browser_1.DANGER_HTML_ATTR) continue;
const attr =
' ' + ((0, core_1.detectIsBoolean)(map[key]) ? (map[key] === true ? key : '') : `${key}="${map[key]}"`);
attrs += attr;
}
return attrs;
}
//# sourceMappingURL=native-element.js.map