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
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.
| Function | Line |
|---|---|
byId | 64 |
text | 66 |
clear | 73 |
appendHighlighted | 81 |
parseQuery | 115 |
score | 133 |
search | 182 |
sortResults | 212 |
resultUrl | 239 |
renderResult | 247 |
shapeOf | 292 |
render | 297 |
scheduleRender | 350 |
fillSelect | 360 |
readQueryFromUrl | 374 |
fail | 382 |
fold | 401 |
start | 414 |