smartssr/ts/smartssr.function.serialize.ts

86 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-02-12 21:31:22 +00:00
declare var document: Document;
export function serializeFunction(rootNode) {
const uuidv4 = () => {
2020-08-06 15:13:38 +00:00
return 'unixxxxxxxxxxx'.replace(/[xy]/g, (c) => {
2020-02-12 21:31:22 +00:00
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};
const prependCss = (uuidID: string, styleTemplate: string) => {
if (!styleTemplate.includes(':host')) {
styleTemplate = `:host {}\n\n${styleTemplate}`;
}
styleTemplate = styleTemplate.replace(/}[ \t\n]+\./g, `}\n\n.${uuidID} .`);
styleTemplate = styleTemplate.replace(/}[ \t\n]+\*/g, `}\n\n.${uuidID} *`);
styleTemplate = styleTemplate.replace(/\(\[/g, `[`);
styleTemplate = styleTemplate.replace(/\]\)/g, `]`);
styleTemplate = styleTemplate.replace(/:host/g, `.${uuidID}`);
styleTemplate = styleTemplate.replace(/:host/g, `.${uuidID}`);
2020-02-12 21:32:25 +00:00
2020-02-12 21:31:22 +00:00
styleTemplate = styleTemplate.replace(/{[ \t\n]+\./g, `{\n\n.${uuidID} .`);
styleTemplate = styleTemplate.replace(/}[ \t\n]+img/g, `}\n\n.${uuidID} img`);
styleTemplate = styleTemplate.replace(/}[ \t\n]+div/g, `}\n\n.${uuidID} div`);
return styleTemplate;
};
2020-08-06 15:13:38 +00:00
const loopProtection: any[] = [];
2020-02-12 21:31:22 +00:00
2020-08-06 15:13:38 +00:00
function serializeNode(nodeArg: HTMLElement, logThis = false) {
if (loopProtection.includes(nodeArg)) {
2020-02-12 21:31:22 +00:00
return;
}
2020-08-06 15:13:38 +00:00
loopProtection.push(nodeArg);
// console.log(nodeArg.nodeName);
if (nodeArg.shadowRoot) {
nodeArg.setAttribute('smartssr', 'yes');
2020-08-06 15:17:01 +00:00
2020-08-06 15:13:38 +00:00
// lets handle the current node
const nodeUUID = uuidv4();
2020-02-12 21:31:22 +00:00
2020-08-06 15:13:38 +00:00
nodeArg.classList.add(nodeUUID);
const slots = nodeArg.shadowRoot.querySelectorAll('slot');
2020-02-12 21:31:22 +00:00
2020-08-06 15:13:38 +00:00
// handle slot element
2020-08-06 17:50:00 +00:00
const slotsForMove: HTMLSlotElement[] = [];
2020-08-06 15:13:38 +00:00
slots.forEach((slot) => {
2020-08-06 17:50:00 +00:00
slotsForMove.push(slot);
2020-08-06 15:13:38 +00:00
});
2020-02-12 21:31:22 +00:00
2020-08-06 17:50:00 +00:00
for (const slot of slotsForMove) {
const slottedLightNodesForMove = [];
slot.assignedNodes().forEach((lightNode) => slottedLightNodesForMove.push(lightNode));
2021-01-08 22:08:01 +00:00
slottedLightNodesForMove.forEach((lightNode) =>
slot.parentNode.insertBefore(lightNode, slot)
);
2020-08-06 17:50:00 +00:00
}
2020-08-06 15:13:38 +00:00
// lets modify the css
const childNodes = nodeArg.shadowRoot.childNodes;
// tslint:disable-next-line: prefer-for-of
const noteForAppending: HTMLElement[] = [];
childNodes.forEach((childNode) => {
if (childNode instanceof HTMLElement) {
if (childNode.tagName === 'STYLE') {
childNode.textContent = prependCss(nodeUUID, childNode.textContent);
} else {
serializeNode(childNode, logThis);
}
noteForAppending.push(childNode);
}
});
2020-08-06 15:17:01 +00:00
noteForAppending.forEach((childNode) => {
2020-08-06 15:13:38 +00:00
nodeArg.append(childNode);
});
} else {
nodeArg.childNodes.forEach((nodeArg2: any) => {
serializeNode(nodeArg2, logThis);
});
2020-02-12 21:31:22 +00:00
}
}
2020-08-06 15:13:38 +00:00
rootNode.childNodes.forEach((nodeArg) => {
serializeNode(nodeArg);
});
2020-02-12 21:31:22 +00:00
}