@dark-engine/platform-server
Version:
Dark renderer for server
102 lines (101 loc) • 3.61 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
exports.createNativeChildrenNodes =
exports.createChunk =
exports.finishCommit =
exports.commit =
exports.createNativeElement =
void 0;
const core_1 = require('@dark-engine/core');
const platform_browser_1 = require('@dark-engine/platform-browser');
const native_element_1 = require('../native-element');
let chunkIds = {};
function createNativeElement(vNode) {
switch (vNode.type) {
case core_1.NodeType.TAG:
return new native_element_1.TagNativeElement(vNode.name);
case core_1.NodeType.TEXT:
return new native_element_1.TextNativeElement(vNode.value);
case core_1.NodeType.COMMENT:
return new native_element_1.CommentNativeElement(vNode.value);
}
}
exports.createNativeElement = createNativeElement;
function addAttributes(element, vNode) {
const tagElement = element;
for (const attrName in vNode.attrs) {
const attrValue = vNode.attrs[attrName];
if (
attrName === core_1.REF_ATTR ||
attrName === platform_browser_1.PREVENT ||
(0, core_1.detectIsFunction)(attrValue)
) {
continue;
} else if (!(0, core_1.detectIsUndefined)(attrValue) && !core_1.ATTR_BLACK_LIST[attrName]) {
!patchAttributes(tagElement, attrName, attrValue) && tagElement.setAttribute(attrName, attrValue);
}
}
}
function patchAttributes(element, attrName, attrValue) {
const fn = specialCasesMap[element.name];
const stop = fn ? fn(element, attrName, attrValue) : false;
return stop;
}
const specialCasesMap = {
[platform_browser_1.TEXTAREA_TAG]: (element, attrName, attrValue) => {
if (attrName === platform_browser_1.VALUE_ATTR && attrValue) {
const textElement = new native_element_1.TextNativeElement(String(attrValue));
element.children = [textElement];
textElement.parentElement = element;
return true;
}
return false;
},
};
const commit = core_1.dummyFn;
exports.commit = commit;
const finishCommit = () => (chunkIds = {});
exports.finishCommit = finishCommit;
function createChunk(fiber) {
let chunk = '';
const tagNode = fiber?.inst;
const tagElement = fiber?.el;
if (!fiber || !fiber.el || tagNode.name === core_1.ROOT) return chunk;
if (!chunkIds[fiber.id]) {
if ((0, core_1.detectIsTagVirtualNode)(fiber.inst)) {
addAttributes(tagElement, fiber.inst);
chunk = tagElement.render(true);
} else if ((0, core_1.detectIsPlainVirtualNode)(fiber.inst)) {
chunk = fiber.el.render();
}
} else if ((0, core_1.detectIsTagVirtualNode)(fiber.inst)) {
chunk = tagElement.render(false);
}
chunkIds[fiber.id] = true;
return chunk;
}
exports.createChunk = createChunk;
function createNativeChildrenNodes(children, parent) {
const elements = [];
for (const child of children) {
const isTag = (0, core_1.detectIsTagVirtualNode)(child);
const isText = (0, core_1.detectIsTextVirtualNode)(child);
const content =
isTag || isText
? child
: (0, core_1.detectIsTextBased)(child)
? (0, core_1.Text)(child)
: (0, core_1.createReplacer)();
const element = createNativeElement(content);
isTag && addAttributes(element, child);
parent && appendNativeElement(element, parent);
if (isTag && child.children.length > 0) {
createNativeChildrenNodes(child.children, element);
}
elements.push(element);
}
return elements;
}
exports.createNativeChildrenNodes = createNativeChildrenNodes;
const appendNativeElement = (element, parent) => parent.appendChild(element);
//# sourceMappingURL=dom.js.map