( container: HTMLElement, cleanTrack: string, )
| 204 | |
| 205 | /** Caret range at the live stream insertion point — end of text, not after closed blocks. */ |
| 206 | export function resolveStreamCaretRange( |
| 207 | container: HTMLElement, |
| 208 | cleanTrack: string, |
| 209 | ): { range: Range; fallback: Element } { |
| 210 | const range = document.createRange(); |
| 211 | |
| 212 | const tableStart = findTableStart(cleanTrack); |
| 213 | if (tableStart !== -1) { |
| 214 | const table = container.querySelector("table"); |
| 215 | if (table) { |
| 216 | const tableEnd = firstCompleteTableEnd(cleanTrack, tableStart); |
| 217 | if (tableEnd === -1 || cleanTrack.length <= tableEnd) { |
| 218 | const partialRow = table.querySelector("tr[data-sh-partial]"); |
| 219 | const tailRoot = partialRow ?? table; |
| 220 | const text = findLastTextNode(tailRoot); |
| 221 | if (text) { |
| 222 | collapseRangeAtTextEnd(range, text); |
| 223 | return { range, fallback: tailRoot as Element }; |
| 224 | } |
| 225 | collapseRangeAtElementStart(range, tailRoot as Element); |
| 226 | return { range, fallback: tailRoot as Element }; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | const stack = getOpenTagStack(cleanTrack); |
| 232 | const target = |
| 233 | stack.length > 0 ? findDeepOpenElement(container, stack) : container; |
| 234 | |
| 235 | if (!target) { |
| 236 | const text = findLastTextNode(container); |
| 237 | if (text) { |
| 238 | collapseRangeAtTextEnd(range, text); |
| 239 | return { range, fallback: container }; |
| 240 | } |
| 241 | collapseRangeAtElementStart(range, container); |
| 242 | return { range, fallback: container }; |
| 243 | } |
| 244 | |
| 245 | const text = findLastTextNode(target); |
| 246 | if (text) { |
| 247 | collapseRangeAtTextEnd(range, text); |
| 248 | return { range, fallback: target }; |
| 249 | } |
| 250 | |
| 251 | collapseRangeAtElementStart(range, target); |
| 252 | return { range, fallback: target }; |
| 253 | } |
| 254 | |
| 255 | /** Remove text nodes wrongly appended at container root during earlier frames */ |
| 256 | function removeOrphanContainerText(container: HTMLElement): void { |
no test coverage detected