website/js/search.js

website/js/search.js

the website's source · 496 lines · read it on GitHub

WHAT IT DOES

Search across the whole repository and this website.

The index is built by tools/search-index/generate.py and committed, so this file only has to read it. CI regenerates and compares, which means a stale index fails the build instead of quietly answering questions about code that no longer looks like that.

# This file is pure ASCII, on purpose

website/js/*.js is served raw and people are invited to open it. A viewer that guesses CP1252 turns an em dash into mojibake in the middle of a sentence promising the code is honest, so anything non-ASCII is written as a \uXXXX escape. tools/site-tests/characters.test.js fails the build otherwise.

# Nothing from the index is ever HTML

The index contains text taken verbatim from source files -- including, by construction, this project's own tests for hostile markup, which contain <script> and onerror= as ordinary content. Every value out of the index therefore reaches the page through textContent or createTextNode and never through innerHTML. Match highlighting is done by splitting a string and appending text nodes and <mark> elements built with createElement, which is why it looks more long-winded than a replace would.

# Bounded work

F-22 and F-23 were quadratic blow-ups in the Markdown renderer on text fetched over the network, and the lesson generalises: this file bounds the query, the number of terms, the number of results scored and the number rendered, so no input makes the tab do an unbounded amount of work. Scoring is a linear pass with a plain indexOf -- no regular expression is ever built from user input, so there is no pattern for a query to blow up.

IN PLAIN WORDS

This is the search box. It looks through every file in the project -- the code, the documents and this website -- and shows you the lines that match.

The searching happens in your browser, on a list that ships with the site. Nothing you type is sent anywhere, and there is nothing here that could collect it.

WHAT CALLS WHAT

byId line 64 text line 66 clear line 73 appendHighlighted line 81 parseQuery line 115 score line 133 search line 182 sortResults line 212 resultUrl line 239 renderResult line 247 shapeOf line 292 render line 297 scheduleRender line 350 fillSelect line 360 readQueryFromUrl line 374 fail line 382 fold line 401 start line 414

Read out of the source: an edge means the callee’s name appears, called, inside the caller’s body. A syntactic reading, not a resolved one.

FunctionLine
byId64
text66
clear73
appendHighlighted81
parseQuery115
score133
search182
sortResults212
resultUrl239
renderResult247
shapeOf292
render297
scheduleRender350
fillSelect360
readQueryFromUrl374
fail382
fold401
start414