3001* @param {Node} node - current node to process3002* @param {TextStyleOptions|null} parrentTextStyleOptions - inherited text style options from parent3003* @returns {Node} - document fragment with wrapped content3004*/3005function wrapNode(node, parrentTextStyleOptions) {3006const fragment = document.createDocumentFragment();3007const textStyleOptions = t_animate__getTextStyleOptions(node, parrentTextStyleOptions);30083009Array.from(node.childNodes).forEach(function (childNode) {3010if (childNode.nodeType === Node.TEXT_NODE) {3011const wrappedTextNode = wrapTextNode(childNode, textStyleOptions);
fragment.appendChild(wrappedTextNode); } else if (childNode.nodeType === Node.ELEMENT_NODE) { const wrappedElement = wrapElementNode(childNode, textStyleOptions); fragment.appendChild(wrappedElement); } }); return fragment; } const wrappedContent = wrapNode(node, null); node.innerHTML = ''; node.appendChild(wrappedContent);}/** * Unwrap each line of animated text element * @param {HTMLElement} element - specific element to unwrap */function t_animate__unwrapAnimatedTextElementLines(element) { const lines = element.querySelectorAll('.t-animate__line'); lines.forEach(line => { line.replaceWith(...line.childNodes); });}/** * Wrap each line of an animated text element with a span, processing its child nodes recursively. * @param {Node} node - node to process */function t_animate__wrapAnimatedTextElementLines(node) { /** * Split an element into left and right parts based on the provided path of child nodes. * @param {Node} element - the element to split * @param {Node[]} path - array of child nodes leading to the target node to split on * @returns {{ leftPart: Node, rightPart: Node }} - object containing left and right parts */ function splitElement(element, path) { if (!path.length) { return { leftPart: document.createDocumentFragment(), rightPart: element }; } const [childToSplitOn, ...restPath] = path; const elementChildList = Array.from(element.childNodes); const childToSplitOnIndex = elementChildList.findIndex(child => child === childToSplitOn); if (childToSplitOnIndex < 0) { return { leftPart: element, rightPart: document.createDocumentFragment() }; } const leftPart = element.cloneNode(false); const rightPart = element.cloneNode(false); const leftPartChildList = childToSplitOnIndex > 0 ? elementChildList.slice(0, childToSplitOnIndex) : []; const rightPartChildList = childToSplitOnIndex < elementChildList.length - 1 ? elementChildList.slice(childToSplitOnIndex + 1) : []; const { leftPart: leftChildPart, rightPart: rightChildPart } = splitElement(childToSplitOn, restPath); leftPart.append(...leftPartChildList, leftChildPart); rightPart.append(rightChildPart, ...rightPartChildList); return { leftPart, rightPart }; } /** * Split inner elements of a node by lines. Splits two lined elements into separate elements for each line * by creating new elements and replacing the original ones in the DOM. * @param {Node} node - the parent node containing the elements * @param {Node[][]} lines - array of lines, each line is an array of nodes * @returns {{ root: Node, elements: Node[] }[]} - array of objects containing the root node and its elements for each line */ function splitNodeInnerElementsByLines(node, lines) { const elementsLineInfoList = []; for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { const words = lines[lineIndex]; const { elements: prevElements } = elementsLineInfoList.at(-1) ?? {}; const elements = []; let root = null; for (let wordIndex = 0; wordIndex < words.length; wordIndex++) { const word = words[wordIndex]; const treePathInfo = t_animate__getTreePath(word); const path = treePathInfo.path; const [container = word, ...restPath] = path; const existedContainerIndex = prevElements?.findIndex(item => item === container); root = treePathInfo.root;