All files / webview/search domHighlighter.ts

93.75% Statements 75/80
90% Branches 36/40
100% Functions 8/8
94.2% Lines 65/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                              20x 20x 20x   20x 22x 22x 22x 22x   20x       35x 35x 39x 39x 37x   36x 36x   36x 36x 36x 36x   35x               22x 22x   20x 20x   19x   19x 19x 35x 35x       35x 35x 35x 35x 35x     19x     4x 4x     28x 28x 24x       4x       19x 19x   3x 3x 4x     3x   3x 1x 1x   2x         9x 9x 1x 1x       16x 15x 15x 15x   2x         2x 2x   2x 1x      
import { buildSearchRegex, type RegexOptions } from './regexUtils';
 
export type SearchOptions = RegexOptions;
 
export interface MatchRange {
  ranges: Range[];
}
 
interface TextSegment {
  node: Text;
  start: number;
  length: number;
}
 
function collectTextSegments(root: Element): { text: string; segments: TextSegment[] } {
  const segments: TextSegment[] = [];
  let text = '';
  const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
  let node: Text | null;
  while ((node = walker.nextNode() as Text | null)) {
    const len = node.data.length;
    Iif (len === 0) continue;
    segments.push({ node, start: text.length, length: len });
    text += node.data;
  }
  return { text, segments };
}
 
function mapToRanges(from: number, to: number, segments: TextSegment[]): Range[] {
  const ranges: Range[] = [];
  for (const seg of segments) {
    const segEnd = seg.start + seg.length;
    if (segEnd <= from) continue;
    if (seg.start >= to) break;
 
    const rangeStart = Math.max(from, seg.start) - seg.start;
    const rangeEnd = Math.min(to, segEnd) - seg.start;
 
    const r = document.createRange();
    r.setStart(seg.node, rangeStart);
    r.setEnd(seg.node, rangeEnd);
    ranges.push(r);
  }
  return ranges;
}
 
export function findMatches(
  container: Element,
  query: string,
  options: SearchOptions,
): MatchRange[] {
  const regex = buildSearchRegex(query, options);
  if (!regex) return [];
 
  const { text, segments } = collectTextSegments(container);
  if (!text) return [];
 
  const matches: MatchRange[] = [];
  let m: RegExpExecArray | null;
  let iterations = 0;
  while ((m = regex.exec(text)) !== null && iterations < 50000) {
    iterations++;
    Iif (m[0].length === 0) {
      regex.lastIndex++;
      continue;
    }
    const from = m.index;
    const to = from + m[0].length;
    const ranges = mapToRanges(from, to, segments);
    Eif (ranges.length > 0) {
      matches.push({ ranges });
    }
  }
  return matches;
}
 
const MATCH_KEY = 'vd-search-match';
const CURRENT_KEY = 'vd-search-current';
 
function getHighlights(): Map<string, any> | null {
  const css = globalThis.CSS as any;
  if (css && 'highlights' in css) return css.highlights;
  return null;
}
 
function createHighlight(...ranges: AbstractRange[]): any {
  return new (globalThis as any).Highlight(...ranges);
}
 
export function applyHighlights(matches: MatchRange[], currentIndex: number): void {
  const highlights = getHighlights();
  if (!highlights) return;
 
  const allRanges: AbstractRange[] = [];
  for (const m of matches) {
    for (const r of m.ranges) allRanges.push(r);
  }
 
  highlights.set(MATCH_KEY, createHighlight(...allRanges));
 
  if (currentIndex >= 0 && currentIndex < matches.length) {
    const current = matches[currentIndex];
    highlights.set(CURRENT_KEY, createHighlight(...current.ranges));
  } else {
    highlights.delete(CURRENT_KEY);
  }
}
 
export function clearHighlights(): void {
  const highlights = getHighlights();
  if (!highlights) return;
  highlights.delete(MATCH_KEY);
  highlights.delete(CURRENT_KEY);
}
 
export function scrollToMatch(match: MatchRange): void {
  if (match.ranges.length === 0) return;
  const firstRange = match.ranges[0];
  const el = firstRange.startContainer.parentElement;
  if (!el || typeof el.scrollIntoView !== 'function') return;
 
  Iif (typeof firstRange.getBoundingClientRect !== 'function') {
    el.scrollIntoView({ block: 'center', behavior: 'smooth' });
    return;
  }
 
  const rect = firstRange.getBoundingClientRect();
  const viewportH = window.innerHeight;
 
  if (rect.top < 60 || rect.bottom > viewportH - 20) {
    el.scrollIntoView({ block: 'center', behavior: 'smooth' });
  }
}