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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 117x 117x 117x 1x 1x 571x 571x 571x 1x 1x 1x 32x 32x 32x 32x 32x 158x 158x 55x 55x 55x 55x 55x 158x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 158x 9x 8x 8x 8x 8x 8x 8x 8x 8x 8x 9x 1x 1x 9x 9x 9x 158x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 78x 78x 78x 78x 303x 254x 254x 246x 158x 246x 246x 78x 158x 158x 158x 32x 32x 3x 3x 32x 32x 1x 1x 117x 117x 117x 75x 75x 117x 117x 117x 10x 10x 75x 75x 117x 41x 41x 41x 12x 41x 23x 23x 23x 23x 41x 117x 75x 75x 1x 1x 1x 25x 25x 25x 25x 10x 10x 15x 15x 25x 2x 2x 2x 2x 2x 2x 2x 25x 3x 3x 3x 3x 3x 3x 10x 10x | import { detectTermType } from './detectTermType';
import { extractAllPositiveTerms } from './extractPositiveTerms';
import { hasAdvancedSyntax, OPERATOR_WORDS, parseSearchQuery } from './parseSearchQuery';
/** Number of rainbow colors cycled for nested parentheses (matched pairs share a depth). */
const PAREN_PALETTE_SIZE = 5;
/**
* How a typed query is being interpreted, driving the search box's visual treatment.
* @category Utils
* @group Search
*/
export type HighlightState = 'plain' | 'advanced' | 'error';
/**
* The token classes the highlighter colorizes.
* @category Utils
* @group Search
*/
export type HighlightTokenKind = 'keyword' | 'paren' | 'quoted' | 'term' | 'whitespace';
/**
* A single highlightable span over the raw query string.
* @category Utils
* @group Search
*/
export interface HighlightToken {
kind: HighlightTokenKind;
/** The exact source text of this token (used verbatim so output round-trips). */
text: string;
/** Inclusive start offset into the raw query. */
start: number;
/** Exclusive end offset into the raw query. */
end: number;
/** Nesting depth (mod `PAREN_PALETTE_SIZE`) for a paren token's rainbow color. */
depth?: number;
/** True for an unmatched parenthesis or an unterminated quote. */
error?: boolean;
}
/**
* Result of highlighting a query: markup plus how the query is interpreted.
* @category Utils
* @group Search
*/
export interface HighlightResult {
/** Token-wrapped, HTML-escaped markup of the raw query (round-trips to the input). */
html: string;
/** Whether the query is plain, a valid advanced query, or an invalid one. */
state: HighlightState;
/** Human-readable reason, present only when `state` is `"error"`. */
message?: string;
}
/** HTML entities escaped before injecting query text into the highlight backdrop. */
const HTML_ESCAPES: Record<string, string> = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
/**
* Escapes the HTML-significant characters in a string so query text can be injected into the
* highlight backdrop without breaking markup or allowing injection.
* @param value - Raw text to escape.
* @returns The text with `& < > " '` replaced by entities.
* @example
* ```typescript
* escapeHtml('a < "b"'); // 'a < "b"'
* ```
* @source
*/
function escapeHtml(value: string): string {
return value.replace(/[&<>"']/g, (char) => HTML_ESCAPES[char]);
}
/**
* Tests whether a character is query whitespace (space, tab, or newline).
* @param char - A single character.
* @returns True when the character is whitespace.
* @source
*/
function isWhitespace(char: string): boolean {
return char === ' ' || char === '\t' || char === '\n';
}
/**
* Splits a raw query into highlightable tokens that carry source offsets and, unlike the
* parser's `tokenize`, preserve whitespace and the quote characters so the rendered markup
* round-trips exactly to the input. Bare words matching {@link OPERATOR_WORDS} are tagged as
* `keyword`; parens carry a nesting `depth` for rainbow coloring; unmatched parens and
* unterminated quotes are flagged with `error`.
* @category Utils
* @group Search
* @param input - The raw query string.
* @returns Ordered tokens covering every character of `input`.
* @example
* ```typescript
* tokenizeWithSpans("a AND (b)");
* // term "a", ws, keyword "AND", ws, paren "(" depth 0, term "b", paren ")" depth 0
* ```
* @source
*/
export function tokenizeWithSpans(input: string): HighlightToken[] {
const tokens: HighlightToken[] = [];
const openParenTokens: number[] = [];
let depth = 0;
let i = 0;
while (i < input.length) {
const char = input[i];
if (isWhitespace(char)) {
const start = i;
while (i < input.length && isWhitespace(input[i])) i++;
tokens.push({ kind: 'whitespace', text: input.slice(start, i), start, end: i });
continue;
}
if (char === '(') {
tokens.push({
kind: 'paren',
text: '(',
start: i,
end: i + 1,
depth: depth % PAREN_PALETTE_SIZE,
});
openParenTokens.push(tokens.length - 1);
depth++;
i++;
continue;
}
if (char === ')') {
if (openParenTokens.length > 0) {
openParenTokens.pop();
depth--;
tokens.push({
kind: 'paren',
text: ')',
start: i,
end: i + 1,
depth: depth % PAREN_PALETTE_SIZE,
});
} else {
tokens.push({ kind: 'paren', text: ')', start: i, end: i + 1, error: true });
}
i++;
continue;
}
if (char === '"') {
const start = i;
i++;
while (i < input.length && input[i] !== '"') i++;
const terminated = i < input.length;
if (terminated) i++; // consume the closing quote
tokens.push({
kind: 'quoted',
text: input.slice(start, i),
start,
end: i,
error: !terminated,
});
continue;
}
// Bare word: runs until whitespace, a paren, or a quote.
const start = i;
while (
i < input.length &&
!isWhitespace(input[i]) &&
input[i] !== '(' &&
input[i] !== ')' &&
input[i] !== '"'
) {
i++;
}
const text = input.slice(start, i);
const kind: HighlightTokenKind = OPERATOR_WORDS.has(text.toUpperCase()) ? 'keyword' : 'term';
tokens.push({ kind, text, start, end: i });
}
// Any parens still open at the end are unbalanced.
for (const idx of openParenTokens) {
tokens[idx].error = true;
}
return tokens;
}
/**
* Renders one token to an HTML span (or bare escaped text for whitespace), tagging it with
* the classes the stylesheet themes: `hl-<kind>`, `hl-paren-<depth>`, and `hl-error`.
* @param token - The token to render.
* @returns The token's HTML fragment.
* @example
* ```typescript
* renderToken({ kind: "keyword", text: "AND", start: 0, end: 3 });
* // '<span class="hl-keyword">AND</span>'
* ```
* @source
*/
function renderToken(token: HighlightToken, advanced: boolean): string {
const text = escapeHtml(token.text);
if (token.kind === 'whitespace') return text;
// A quoted phrase is colored exactly like the equivalent bare term — the
// quotes only affect parsing, not the term's type — so both use `hl-term`.
const isTermLike = token.kind === 'term' || token.kind === 'quoted';
const classes = [isTermLike ? 'hl-term' : `hl-${token.kind}`];
if (token.kind === 'paren' && token.depth !== undefined) {
classes.push(`hl-paren-${token.depth}`);
}
// Tint a term by what it looks like — CAS, SMILES, or formula. Detection runs
// on the inner text of a quoted token.
if (isTermLike) {
const inner = token.kind === 'quoted' ? token.text.replace(/^"|"$/g, '') : token.text;
const termType = detectTermType(inner);
if (termType !== 'string') {
classes.push(`hl-term-${termType}`);
} else if (advanced) {
// A plain-string term gets its own color, but only inside an advanced
// query (a lone plain search stays uncolored).
classes.push('hl-term-string');
}
}
if (token.error) classes.push('hl-error');
return `<span class="${classes.join(' ')}">${text}</span>`;
}
/**
* Highlights a search query for the search box, returning token-colored markup plus how the
* query is interpreted. The result mirrors {@link parseSearchQuery}'s own grammar:
* - `"plain"` — no advanced syntax (rendered without coloring by the input component).
* - `"advanced"` — a valid boolean query with at least one inclusive term.
* - `"error"` — advanced syntax that is malformed (e.g. unbalanced parens) or has no
* inclusive constraint (only `NOT`/exclusions), which would match almost everything.
* @category Utils
* @group Search
* @param input - The raw query string from the search box.
* @returns The markup, interpretation state, and an error message when invalid.
* @example
* ```typescript
* highlightSearchQuery("acetone").state; // "plain"
* highlightSearchQuery("a AND b").state; // "advanced"
* highlightSearchQuery("NOT a AND NOT b").state; // "error" (no inclusive term)
* highlightSearchQuery("a AND (b").state; // "error" (unbalanced parens)
* ```
* @source
*/
export function highlightSearchQuery(input: string): HighlightResult {
const tokens = tokenizeWithSpans(input);
const advanced = hasAdvancedSyntax(input);
const html = tokens.map((token) => renderToken(token, advanced)).join('');
if (!advanced) {
return { html, state: 'plain' };
}
const parsed = parseSearchQuery(input);
// Advanced syntax present but the parser fell back to a plain term ⇒ malformed.
if (!parsed.isAdvanced) {
const hasUnbalancedParens = tokens.some((token) => token.kind === 'paren' && token.error);
return {
html,
state: 'error',
message: hasUnbalancedParens ? 'Unbalanced parentheses.' : 'Invalid advanced query syntax.',
};
}
if (extractAllPositiveTerms(parsed.ast).length === 0) {
return {
html,
state: 'error',
message: 'Add at least one term to include — a query of only exclusions matches everything.',
};
}
return { html, state: 'advanced' };
}
|