| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { |
| decodeFileLinkPath, |
| fileMentionLinkRe, |
| getMentionBadgeIconPaths, |
| getMentionBadgeLabel |
| } from './mention-badge'; |
| import { |
| MENTION_BADGE_CLASSNAME, |
| MENTION_BADGE_ICON_CLASSNAME, |
| MENTION_BADGE_SVG_ATTRIBUTES, |
| SETTINGS_KEYS |
| } from '$lib/constants'; |
| import { settingsStore } from '$lib/stores/settings.svelte'; |
| import { toolsStore } from '$lib/stores/tools.svelte'; |
|
|
| export type ContentToken = |
| | { kind: 'text'; text: string } |
| | { kind: 'badge'; name: string; path: string } |
| | { kind: 'inlineCode'; text: string } |
| | { kind: 'codeBlock'; text: string }; |
|
|
| |
| |
| const BLOCK_TAG_NAMES = new Set(['DIV', 'P']); |
| |
| |
| const MENTION_BADGE_RE = fileMentionLinkRe('g'); |
|
|
| function badgeSourceLength(name: string, path: string): number { |
| if (!name || !path) return 0; |
|
|
| return `[${name}](file://${path})`.length; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| const CODE_SPAN_RE = /(```[\s\S]*?```)|(`[^`\n]+`)/g; |
|
|
| |
| |
| |
| |
| |
| export function containsCodeSpan(value: string): boolean { |
| CODE_SPAN_RE.lastIndex = 0; |
|
|
| return CODE_SPAN_RE.test(value); |
| } |
|
|
| const CODE_FENCE_RE = /```/g; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function isOffsetInCodeBlock(source: string, offset: number): boolean { |
| let inside = false; |
|
|
| CODE_FENCE_RE.lastIndex = 0; |
|
|
| let match: RegExpExecArray | null; |
|
|
| while ((match = CODE_FENCE_RE.exec(source)) !== null) { |
| if (match.index + match[0].length > offset) break; |
|
|
| inside = !inside; |
| } |
|
|
| return inside; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function tokenizeContent(input: string): ContentToken[] { |
| const tokens: ContentToken[] = []; |
|
|
| let cursor = 0; |
|
|
| CODE_SPAN_RE.lastIndex = 0; |
|
|
| let match: RegExpExecArray | null; |
|
|
| while ((match = CODE_SPAN_RE.exec(input)) !== null) { |
| const start = match.index; |
|
|
| if (start > cursor) { |
| pushTextAndBadgeTokens(input.slice(cursor, start), tokens); |
| } |
|
|
| tokens.push( |
| match[1] !== undefined |
| ? { kind: 'codeBlock', text: match[1] } |
| : { kind: 'inlineCode', text: match[2] } |
| ); |
| cursor = start + match[0].length; |
| } |
|
|
| if (cursor < input.length) { |
| pushTextAndBadgeTokens(input.slice(cursor), tokens); |
| } |
|
|
| return tokens; |
| } |
|
|
| |
| |
| |
| function pushTextAndBadgeTokens(input: string, tokens: ContentToken[]) { |
| let cursor = 0; |
|
|
| MENTION_BADGE_RE.lastIndex = 0; |
|
|
| let match: RegExpExecArray | null; |
|
|
| while ((match = MENTION_BADGE_RE.exec(input)) !== null) { |
| const [whole, name, path] = match; |
| const start = match.index; |
|
|
| if (start > cursor) { |
| tokens.push({ kind: 'text', text: input.slice(cursor, start) }); |
| } |
|
|
| tokens.push({ kind: 'badge', name, path }); |
| cursor = start + whole.length; |
| } |
|
|
| if (cursor < input.length) { |
| tokens.push({ kind: 'text', text: input.slice(cursor) }); |
| } |
| } |
|
|
| function isCodeBlockElement(node: Node | null): node is HTMLElement { |
| return node instanceof HTMLElement && node.dataset.codeToken === 'block'; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function serializeContent(root: HTMLElement): string { |
| let out = ''; |
| let pendingBlockBoundary = false; |
|
|
| const walk = (parent: Node) => { |
| let first = true; |
|
|
| for (const child of Array.from(parent.childNodes)) { |
| if (child.nodeType === Node.TEXT_NODE) { |
| const text = child.textContent ?? ''; |
|
|
| if (text.length > 0) { |
| if (pendingBlockBoundary) { |
| out += '\n'; |
| pendingBlockBoundary = false; |
| } |
|
|
| out += text; |
| first = false; |
| } |
|
|
| continue; |
| } |
|
|
| if (child.nodeType !== Node.ELEMENT_NODE) continue; |
|
|
| const el = child as HTMLElement; |
|
|
| if (el.dataset.mentionBadge === 'true') { |
| const name = el.dataset.mentionName ?? ''; |
| const path = el.dataset.mentionPath ?? ''; |
|
|
| if (name && path) { |
| if (pendingBlockBoundary) { |
| out += '\n'; |
| pendingBlockBoundary = false; |
| } |
|
|
| out += `[${name}](file://${path})`; |
| first = false; |
| } |
|
|
| continue; |
| } |
|
|
| if (el.dataset.codeToken !== undefined) { |
| const isBlock = el.dataset.codeToken === 'block'; |
|
|
| if (isBlock && (pendingBlockBoundary || !first)) out += '\n'; |
|
|
| pendingBlockBoundary = false; |
| walk(el); |
| first = false; |
|
|
| if (isBlock) pendingBlockBoundary = true; |
|
|
| continue; |
| } |
|
|
| if (el.tagName === 'BR') { |
| const isHatch = |
| isCodeBlockElement(el.previousSibling) || isCodeBlockElement(el.nextSibling); |
|
|
| if (!isHatch && el.nextSibling) { |
| if (pendingBlockBoundary) { |
| out += '\n'; |
| pendingBlockBoundary = false; |
| } |
|
|
| out += '\n'; |
| first = false; |
| } |
|
|
| continue; |
| } |
|
|
| if (BLOCK_TAG_NAMES.has(el.tagName)) { |
| if (pendingBlockBoundary || !first) out += '\n'; |
|
|
| pendingBlockBoundary = false; |
| walk(el); |
| first = false; |
|
|
| continue; |
| } |
|
|
| walk(el); |
|
|
| if (pendingBlockBoundary) first = false; |
| } |
| }; |
|
|
| walk(root); |
|
|
| return out; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function domMatchesTokens(root: HTMLElement, tokens: ContentToken[]): boolean { |
| const expected = tokens.filter((token) => token.kind !== 'text'); |
|
|
| let index = 0; |
|
|
| const walk = (parent: Node): boolean => { |
| for (const child of Array.from(parent.childNodes)) { |
| if (child.nodeType !== Node.ELEMENT_NODE) continue; |
|
|
| const el = child as HTMLElement; |
| const isBadge = el.dataset.mentionBadge === 'true'; |
| const isCode = el.dataset.codeToken !== undefined; |
|
|
| if (!isBadge && !isCode) { |
| if (!walk(el)) return false; |
|
|
| continue; |
| } |
|
|
| const token = expected[index++]; |
|
|
| if (!token) return false; |
|
|
| if (isBadge) { |
| if (token.kind !== 'badge') return false; |
|
|
| if (token.name !== (el.dataset.mentionName ?? '')) return false; |
|
|
| if (token.path !== (el.dataset.mentionPath ?? '')) return false; |
|
|
| continue; |
| } |
|
|
| const codeKind = el.dataset.codeToken === 'block' ? 'codeBlock' : 'inlineCode'; |
|
|
| if (token.kind !== codeKind) return false; |
|
|
| if ( |
| (token.kind === 'inlineCode' || token.kind === 'codeBlock') && |
| token.text !== (el.textContent ?? '') |
| ) { |
| return false; |
| } |
| } |
|
|
| return true; |
| }; |
|
|
| return walk(root) && index === expected.length; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function rangeToTextOffset(root: HTMLElement, range: Range | null): number { |
| if (!range) return serializeContent(root).length; |
|
|
| |
| const pre = range.cloneRange(); |
|
|
| pre.selectNodeContents(root); |
| pre.setEnd(range.endContainer, range.endOffset); |
| const atOrBeforeCaret = (node: Node, offset: number) => pre.comparePoint(node, offset) !== 1; |
|
|
| let total = 0; |
| let done = false; |
| |
| |
| |
| let pendingPoint: { node: Node; index: number } | null = null; |
|
|
| const walk = (parent: Node) => { |
| let first = true; |
|
|
| for (const child of Array.from(parent.childNodes)) { |
| if (done) return; |
|
|
| if (child.nodeType === Node.TEXT_NODE) { |
| const text = child.textContent ?? ''; |
|
|
| if (text.length === 0) continue; |
|
|
| if (pendingPoint) { |
| const { index, node } = pendingPoint; |
|
|
| pendingPoint = null; |
|
|
| if (!atOrBeforeCaret(node, index)) { |
| done = true; |
|
|
| return; |
| } |
|
|
| total += 1; |
| } |
|
|
| if (!atOrBeforeCaret(child, 0)) { |
| done = true; |
|
|
| return; |
| } |
|
|
| if (range.endContainer === child) { |
| total += range.endOffset; |
| done = true; |
|
|
| return; |
| } |
|
|
| total += text.length; |
| first = false; |
|
|
| continue; |
| } |
|
|
| if (child.nodeType !== Node.ELEMENT_NODE) continue; |
|
|
| const el = child as HTMLElement; |
| const parentNode = el.parentNode as Node; |
| const elIndex = Array.prototype.indexOf.call(parentNode.childNodes, el); |
|
|
| if (pendingPoint) { |
| const { index, node } = pendingPoint; |
|
|
| pendingPoint = null; |
|
|
| if (!atOrBeforeCaret(node, index)) { |
| done = true; |
|
|
| return; |
| } |
|
|
| total += 1; |
| } |
|
|
| if (el.dataset.mentionBadge === 'true') { |
| const len = badgeSourceLength(el.dataset.mentionName ?? '', el.dataset.mentionPath ?? ''); |
|
|
| if (len === 0) continue; |
|
|
| if (!atOrBeforeCaret(parentNode, elIndex + 1)) { |
| done = true; |
|
|
| return; |
| } |
|
|
| total += len; |
| first = false; |
|
|
| continue; |
| } |
|
|
| if (el.dataset.codeToken !== undefined) { |
| const isBlock = el.dataset.codeToken === 'block'; |
|
|
| if (isBlock && !first) { |
| if (!atOrBeforeCaret(el, 0)) { |
| done = true; |
|
|
| return; |
| } |
|
|
| total += 1; |
| } |
|
|
| walk(el); |
| first = false; |
|
|
| if (isBlock) pendingPoint = { index: elIndex + 1, node: parentNode }; |
|
|
| continue; |
| } |
|
|
| if (el.tagName === 'BR') { |
| const isHatch = |
| isCodeBlockElement(el.previousSibling) || isCodeBlockElement(el.nextSibling); |
|
|
| if (isHatch || !el.nextSibling) continue; |
|
|
| if (!atOrBeforeCaret(parentNode, elIndex + 1)) { |
| done = true; |
|
|
| return; |
| } |
|
|
| total += 1; |
| first = false; |
|
|
| continue; |
| } |
|
|
| if (BLOCK_TAG_NAMES.has(el.tagName)) { |
| if (!first) { |
| if (!atOrBeforeCaret(el, 0)) { |
| done = true; |
|
|
| return; |
| } |
|
|
| total += 1; |
| } |
|
|
| walk(el); |
| first = false; |
|
|
| continue; |
| } |
|
|
| const before = total; |
|
|
| walk(el); |
|
|
| if (total > before) first = false; |
| } |
| }; |
|
|
| walk(root); |
|
|
| return total; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function buildFragment(tokens: ContentToken[]): DocumentFragment { |
| const fragment = document.createDocumentFragment(); |
|
|
| for (let index = 0; index < tokens.length; index++) { |
| const token = tokens[index]; |
|
|
| if (token.kind === 'text') { |
| let text = token.text; |
|
|
| |
| |
| |
| if (tokens[index - 1]?.kind === 'codeBlock' && text.startsWith('\n')) { |
| text = text.slice(1); |
| } |
|
|
| if (tokens[index + 1]?.kind === 'codeBlock' && text.endsWith('\n')) { |
| text = text.slice(0, -1); |
| } |
|
|
| if (text.length === 0) continue; |
|
|
| fragment.appendChild(document.createTextNode(text)); |
|
|
| continue; |
| } |
|
|
| if (token.kind === 'inlineCode' || token.kind === 'codeBlock') { |
| const code = document.createElement('code'); |
|
|
| code.dataset.codeToken = token.kind === 'codeBlock' ? 'block' : 'inline'; |
| code.textContent = token.text; |
| fragment.appendChild(code); |
|
|
| continue; |
| } |
|
|
| |
| |
| |
| if (!fragment.lastChild) { |
| fragment.appendChild(document.createTextNode('')); |
| } |
|
|
| const badge = document.createElement('span'); |
|
|
| badge.dataset.mentionBadge = 'true'; |
| badge.dataset.mentionName = token.name; |
| badge.dataset.mentionPath = token.path; |
| badge.title = decodeFileLinkPath(token.path); |
| badge.className = MENTION_BADGE_CLASSNAME; |
| badge.contentEditable = 'false'; |
|
|
| const svg = document.createElementNS(MENTION_BADGE_SVG_ATTRIBUTES['xmlns'], 'svg'); |
|
|
| for (const [attr, value] of Object.entries(MENTION_BADGE_SVG_ATTRIBUTES)) { |
| svg.setAttribute(attr, value); |
| } |
| for (const cls of MENTION_BADGE_ICON_CLASSNAME.split(/\s+/).filter(Boolean)) { |
| svg.classList.add(cls); |
| } |
|
|
| for (const d of getMentionBadgeIconPaths(token.path)) { |
| const path = document.createElementNS(MENTION_BADGE_SVG_ATTRIBUTES['xmlns'], 'path'); |
|
|
| path.setAttribute('d', d); |
| svg.appendChild(path); |
| } |
|
|
| const label = document.createElement('span'); |
|
|
| label.classList.add('shrink-0', 'truncate'); |
| label.textContent = getMentionBadgeLabel( |
| token.name, |
| decodeFileLinkPath(token.path), |
| settingsStore.getConfig(SETTINGS_KEYS.SHOW_FULL_PATH_IN_MENTIONS), |
| toolsStore.serverHome |
| ); |
|
|
| badge.appendChild(svg); |
| badge.appendChild(label); |
| fragment.appendChild(badge); |
| } |
|
|
| return fragment; |
| } |
|
|
| |
| |
| function hasLineBeside(node: Node | null): boolean { |
| if (!node) return false; |
|
|
| if (node.nodeType === Node.ELEMENT_NODE) return true; |
|
|
| return (node.textContent ?? '') !== ''; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function syncCodeBlockHatches(root: HTMLElement) { |
| for (const child of Array.from(root.childNodes)) { |
| if (child.nodeName !== 'BR') continue; |
|
|
| const isPlaceholder = root.childNodes.length === 1; |
| const isLeadingHatch = !child.previousSibling && isCodeBlockElement(child.nextSibling); |
| const isTrailingHatch = !child.nextSibling && isCodeBlockElement(child.previousSibling); |
|
|
| |
| |
| |
| |
| |
| let prevElement = child.previousSibling; |
|
|
| while (prevElement && prevElement.nodeType !== Node.ELEMENT_NODE) { |
| prevElement = prevElement.previousSibling; |
| } |
| const nearBlock = |
| isCodeBlockElement(child.nextSibling) || |
| isCodeBlockElement(child.previousSibling) || |
| isCodeBlockElement(prevElement); |
|
|
| if (!isPlaceholder && !isLeadingHatch && !isTrailingHatch && nearBlock) { |
| child.remove(); |
| } |
| } |
|
|
| for (const child of Array.from(root.childNodes)) { |
| if (!isCodeBlockElement(child)) continue; |
|
|
| if (!hasLineBeside(child.nextSibling)) { |
| child.after(document.createElement('br')); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function stripBlockBoundaryLineBreaks(root: HTMLElement): boolean { |
| let changed = false; |
|
|
| for (const child of Array.from(root.childNodes)) { |
| if (child.nodeType !== Node.TEXT_NODE) continue; |
|
|
| if (!isCodeBlockElement(child.previousSibling)) continue; |
|
|
| let text = child.textContent ?? ''; |
|
|
| if (!/^\n{2,}$/.test(text)) continue; |
|
|
| text = text.slice(1); |
|
|
| const atBufferEnd = !child.nextSibling || child.nextSibling.nodeName === 'BR'; |
|
|
| if (atBufferEnd) { |
| text = text.slice(0, -1); |
| } |
|
|
| child.textContent = text; |
| changed = true; |
| } |
|
|
| return changed; |
| } |
|
|
| const WORD_CHAR_RE = /[\p{L}\p{N}_]/u; |
|
|
| |
| |
| |
| |
| |
| |
| export function badgeAwareWordJump( |
| source: string, |
| offset: number, |
| direction: 'forward' | 'backward' |
| ): number | null { |
| let masked = ''; |
|
|
| const badgeSpans: Array<[number, number]> = []; |
|
|
| for (const token of tokenizeContent(source)) { |
| const len = |
| token.kind === 'badge' ? badgeSourceLength(token.name, token.path) : token.text.length; |
|
|
| if (token.kind === 'badge') badgeSpans.push([masked.length, masked.length + len]); |
|
|
| masked += token.kind === 'badge' ? 'a'.repeat(len) : token.text; |
| } |
|
|
| if (badgeSpans.length === 0) return null; |
|
|
| const isWord = (index: number) => WORD_CHAR_RE.test(masked[index]); |
| const spanStartingAt = (index: number) => badgeSpans.find(([start]) => start === index); |
| const spanEndingAt = (index: number) => badgeSpans.find(([, end]) => end === index); |
| const n = masked.length; |
|
|
| let i = offset; |
|
|
| if (direction === 'forward') { |
| |
| if (!(i < n && isWord(i))) { |
| while (i < n && !isWord(i)) i++; |
| } |
|
|
| while (i < n && isWord(i)) { |
| const span = spanStartingAt(i); |
|
|
| if (span) { |
| i = span[1]; |
|
|
| break; |
| } |
|
|
| i++; |
| } |
| } else { |
| if (!(i > 0 && isWord(i - 1))) { |
| while (i > 0 && !isWord(i - 1)) i--; |
| } |
|
|
| while (i > 0 && isWord(i - 1)) { |
| const span = spanEndingAt(i); |
|
|
| if (span) { |
| i = span[0]; |
|
|
| break; |
| } |
|
|
| i--; |
| } |
| } |
|
|
| if (i === offset) return null; |
|
|
| const lo = Math.min(offset, i); |
| const hi = Math.max(offset, i); |
|
|
| return badgeSpans.some(([start, end]) => start < hi && end > lo) ? i : null; |
| } |
|
|
| |
| |
| |
| |
| |
| export function leadingBadgeEdgeOffset(source: string, caret: number): number | null { |
| const [first] = tokenizeContent(source); |
|
|
| if (!first || first.kind !== 'badge') return null; |
|
|
| return caret === badgeSourceLength(first.name, first.path) ? 0 : null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function textOffsetToRange(root: HTMLElement, offset: number): Range { |
| const range = document.createRange(); |
|
|
| let remaining = offset; |
| let landed = false; |
| let pendingBlockBoundary = false; |
|
|
| const land = (node: Node, nodeOffset: number) => { |
| range.setStart(node, nodeOffset); |
| range.setEnd(node, nodeOffset); |
| landed = true; |
| }; |
| const walk = (parent: Node) => { |
| let first = true; |
|
|
| for (const child of Array.from(parent.childNodes)) { |
| if (landed) return; |
|
|
| if (child.nodeType === Node.TEXT_NODE) { |
| const text = child.textContent ?? ''; |
|
|
| if (text.length === 0) continue; |
|
|
| if (pendingBlockBoundary) { |
| |
| |
| pendingBlockBoundary = false; |
|
|
| if (remaining === 0) { |
| land(child, 0); |
|
|
| return; |
| } |
|
|
| remaining -= 1; |
| } |
|
|
| if (remaining <= text.length) { |
| land(child, remaining); |
|
|
| return; |
| } |
|
|
| remaining -= text.length; |
| first = false; |
|
|
| continue; |
| } |
|
|
| if (child.nodeType !== Node.ELEMENT_NODE) continue; |
|
|
| const el = child as HTMLElement; |
|
|
| if (el.dataset.mentionBadge === 'true') { |
| const len = badgeSourceLength(el.dataset.mentionName ?? '', el.dataset.mentionPath ?? ''); |
|
|
| if (len === 0) continue; |
|
|
| if (pendingBlockBoundary) { |
| pendingBlockBoundary = false; |
|
|
| if (remaining === 0) { |
| range.setStartBefore(el); |
| range.setEndBefore(el); |
| landed = true; |
|
|
| return; |
| } |
|
|
| remaining -= 1; |
| } |
|
|
| if (remaining <= len) { |
| if (remaining === 0) { |
| range.setStartBefore(el); |
| range.setEndBefore(el); |
| } else { |
| range.setStartAfter(el); |
| range.setEndAfter(el); |
| } |
|
|
| landed = true; |
|
|
| return; |
| } |
|
|
| remaining -= len; |
| first = false; |
|
|
| continue; |
| } |
|
|
| if (el.dataset.codeToken !== undefined) { |
| const isBlock = el.dataset.codeToken === 'block'; |
|
|
| if (isBlock && (pendingBlockBoundary || !first)) { |
| pendingBlockBoundary = false; |
|
|
| if (remaining === 0) { |
| range.setStartBefore(el); |
| range.setEndBefore(el); |
| landed = true; |
|
|
| return; |
| } |
|
|
| remaining -= 1; |
| } |
|
|
| const len = (el.textContent ?? '').length; |
|
|
| if (remaining === 0) { |
| range.setStartBefore(el); |
| range.setEndBefore(el); |
| landed = true; |
|
|
| return; |
| } |
|
|
| if (remaining === len) { |
| range.setStartAfter(el); |
| range.setEndAfter(el); |
| landed = true; |
|
|
| return; |
| } |
|
|
| if (remaining < len) { |
| walk(el); |
|
|
| return; |
| } |
|
|
| remaining -= len; |
|
|
| if (isBlock) remaining -= 1; |
|
|
| first = false; |
|
|
| continue; |
| } |
|
|
| if (el.tagName === 'BR') { |
| const isHatch = |
| isCodeBlockElement(el.previousSibling) || isCodeBlockElement(el.nextSibling); |
|
|
| if (isHatch) { |
| |
| |
| if (remaining === 0) { |
| range.setStartBefore(el); |
| range.setEndBefore(el); |
| landed = true; |
| } |
|
|
| continue; |
| } |
|
|
| if (!el.nextSibling) continue; |
|
|
| if (pendingBlockBoundary) { |
| pendingBlockBoundary = false; |
|
|
| if (remaining === 0) { |
| range.setStartBefore(el); |
| range.setEndBefore(el); |
| landed = true; |
|
|
| return; |
| } |
|
|
| remaining -= 1; |
| } |
|
|
| if (remaining === 0) { |
| range.setStartBefore(el); |
| range.setEndBefore(el); |
| landed = true; |
|
|
| return; |
| } |
|
|
| remaining -= 1; |
| first = false; |
|
|
| continue; |
| } |
|
|
| if (BLOCK_TAG_NAMES.has(el.tagName)) { |
| if (pendingBlockBoundary || !first) { |
| pendingBlockBoundary = false; |
|
|
| if (remaining === 0) { |
| |
| range.setStartBefore(el); |
| range.setEndBefore(el); |
| landed = true; |
|
|
| return; |
| } |
|
|
| remaining -= 1; |
| } |
|
|
| walk(el); |
| first = false; |
|
|
| continue; |
| } |
|
|
| const before = remaining; |
|
|
| walk(el); |
|
|
| if (remaining < before) first = false; |
| } |
| }; |
|
|
| walk(root); |
|
|
| if (!landed) { |
| const last = root.lastChild; |
|
|
| if (last && last.nodeName === 'BR') { |
| range.setStartBefore(last); |
| range.setEndBefore(last); |
| } else { |
| range.selectNodeContents(root); |
| range.collapse(false); |
| } |
| } |
|
|
| return range; |
| } |
|
|