(locale, data, originalInput)
| 113 | }, |
| 114 | |
| 115 | async push(locale, data, originalInput) { |
| 116 | // Parse original HTML |
| 117 | const handler = new DomHandler(); |
| 118 | const parser = new htmlparser2.Parser(handler, { |
| 119 | lowerCaseTags: false, |
| 120 | lowerCaseAttributeNames: false, |
| 121 | }); |
| 122 | parser.write( |
| 123 | originalInput ?? |
| 124 | "<!DOCTYPE html><html><head></head><body></body></html>", |
| 125 | ); |
| 126 | parser.end(); |
| 127 | |
| 128 | const dom = handler.dom; |
| 129 | |
| 130 | // Find HTML element and set lang attribute |
| 131 | const html = domutils.findOne( |
| 132 | (elem) => elem.type === "tag" && elem.name.toLowerCase() === "html", |
| 133 | dom, |
| 134 | true, |
| 135 | ) as Element | null; |
| 136 | |
| 137 | if (html) { |
| 138 | html.attribs = html.attribs || {}; |
| 139 | html.attribs.lang = locale; |
| 140 | } |
| 141 | |
| 142 | // Helper to traverse child elements by numeric indices |
| 143 | function traverseByIndices( |
| 144 | element: Element | null, |
| 145 | indices: string[], |
| 146 | ): Element | null { |
| 147 | let current = element; |
| 148 | |
| 149 | for (const indexStr of indices) { |
| 150 | if (!current) return null; |
| 151 | |
| 152 | const index = parseInt(indexStr, 10); |
| 153 | const children: Element[] = current.children.filter( |
| 154 | (child): child is Element => child.type === "tag", |
| 155 | ); |
| 156 | |
| 157 | if (index >= children.length) { |
| 158 | return null; // Path doesn't exist |
| 159 | } |
| 160 | |
| 161 | current = children[index]; |
| 162 | } |
| 163 | |
| 164 | return current; |
| 165 | } |
| 166 | |
| 167 | // Resolve path to element in the DOM |
| 168 | function resolvePathToElement(path: string): Element | null { |
| 169 | const parts = path.split("/"); |
| 170 | const [rootTag, ...indices] = parts; |
| 171 | |
| 172 | let current: Element | null = null; |
nothing calls this directly
no test coverage detected