Skip to content

Commit

Permalink
feat: Separate the functions truncateNode and elipsizeHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
iandebruin98 committed Oct 10, 2024
1 parent 1e26a9f commit 071ccac
Showing 1 changed file with 42 additions and 33 deletions.
75 changes: 42 additions & 33 deletions packages/lib/ui-helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,54 @@ export function elipsize(value: string, maxLength: number) {
return value;
}

export function elipsizeHTML(value: string, maxLength: number) {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = value;

let currentLength = 0;
function truncateNode(
node: Node,
maxLength: number,
currentLength: { value: number }
): string {
let truncatedHTML = '';

function truncateNode(node: any) {
if (node.nodeType === Node.TEXT_NODE) {
const text = node.nodeValue || '';
if (currentLength + text.length > maxLength) {
truncatedHTML += text.substring(0, maxLength - currentLength) + '...';
currentLength = maxLength;
} else {
truncatedHTML += text;
currentLength += text.length;
}
} else if (node.nodeType === Node.ELEMENT_NODE) {
const tagName = node.nodeName.toLowerCase();
truncatedHTML += `<${tagName}`;

for (let i = 0; i < node.attributes.length; i++) {
const attr = node.attributes[i];
truncatedHTML += ` ${attr.name}="${attr.value}"`;
}
truncatedHTML += '>';

for (let i = 0; i < node.childNodes.length; i++) {
if (currentLength >= maxLength) break;
truncateNode(node.childNodes[i]);
}

truncatedHTML += `</${tagName}>`;
if (node.nodeType === Node.TEXT_NODE) {
const text = node.nodeValue || '';
if (currentLength.value + text.length > maxLength) {
truncatedHTML += text.substring(0, maxLength - currentLength.value) + '...';
currentLength.value = maxLength;
} else {
truncatedHTML += text;
currentLength.value += text.length;
}
} else if (node.nodeType === Node.ELEMENT_NODE) {
const element = node as Element;
const tagName = element.nodeName.toLowerCase();
truncatedHTML += `<${tagName}`;

for (let i = 0; i < element.attributes.length; i++) {
const attr = element.attributes[i];
truncatedHTML += ` ${attr.name}="${attr.value}"`;
}
truncatedHTML += '>';

for (let i = 0; i < element.childNodes.length; i++) {
if (currentLength.value >= maxLength) break;
truncatedHTML += truncateNode(element.childNodes[i], maxLength, currentLength);
}

truncatedHTML += `</${tagName}>`;
}

return truncatedHTML;
}

export function elipsizeHTML(value: string, maxLength: number): string {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = value;

let currentLength = { value: 0 };
let truncatedHTML = '';

for (let i = 0; i < tempDiv.childNodes.length; i++) {
if (currentLength >= maxLength) break;
truncateNode(tempDiv.childNodes[i]);
if (currentLength.value >= maxLength) break;
truncatedHTML += truncateNode(tempDiv.childNodes[i], maxLength, currentLength);
}

return truncatedHTML;
Expand Down

0 comments on commit 071ccac

Please sign in to comment.