This document explains the technical architecture, behavior, and URL encoding schemes used for Focused Views, specifically for tracking and resolving selected elements and highlighted text when links are shared. CustardUI's text highlighting is built to be extremely robust, enduring minor content edits and code updates without breaking.
?cv-highlight=)Standard browser text fragments can be fragile. CustardUI solves this by snapshotting selections as a list of TextRangeDescriptor objects.
Text highlights are stored in the URL under the cv-highlight query parameter. To keep the URL length manageable, the descriptors use a dense, colon-separated format instead of raw JSON. Multiple highlights are joined together using a comma , as the delimiter.
For each highlight descriptor, the encoding falls into one of three formats depending on the context available in the DOM:
e:)Used when the text container has a direct id attribute.
Format: e:<startEnc>:<endEnc>:<textLen>:<elementId>:<containerHash>:<textHash>[:<color>]
e: Prefix indicating the Element ID format.startEnc: URI-encoded snippet of the first 16 characters of the highlighted text (whitespace-normalized, spaces converted to +).endEnc: URI-encoded snippet of the last 16 characters of the highlighted text (whitespace-normalized, spaces converted to +). If the total highlight is 16 characters or less, this is omitted (left empty) to prevent duplication, resulting in ::.textLen: Character length of the highlighted text.elementId: The direct DOM id of the container.containerHash: A numeric hash of the container's stable text content (used to verify if the container structure changed).textHash: A numeric hash of the full highlighted text.color (optional): The color key (e.g., yellow, blue). Defaults to yellow.c:)Used when the text container doesn't have an id, but one of its ancestor elements does.
Format: c:<startEnc>:<endEnc>:<textLen>:<containerId>:<containerTag>:<containerIndex>:<containerHash>:<textHash>[:<color>]
c: Prefix indicating the Container ID format.startEnc: URI-encoded snippet of the first 16 characters of the highlighted text (whitespace-normalized, spaces converted to +).endEnc: URI-encoded snippet of the last 16 characters of the highlighted text (whitespace-normalized, spaces converted to +).textLen: Character length of the highlighted text.containerId: The DOM id of the nearest ancestor.containerTag: The HTML tag name of the text container (e.g., P, DIV).containerIndex: The zero-based index of the container among siblings of the same tag within the scope of the containerId.containerHash, textHash, color) follow the exact same format as above.Used when no id is available anywhere in the ancestor chain. It encodes the properties into a minified JSON object and converts it to Base64 to ensure it remains URL-safe.
Format: [Base64 Encoded JSON]
JSON Payload:
{
"ct": "containerTag",
"ci": 0, // containerIndex
"ch": 123456, // containerHash
"s": "start", // start text snippet
"e": "end", // end text snippet
"th": 654321, // textHash
"tl": 42, // textLength
"c": "yellow" // color (optional)
}
To ensure the parsing delimiter : doesn't conflict with text content, the text snippets (startEnc and endEnc) are strictly encoded using encodeURIComponent() during serialization and decoded with decodeURIComponent() upon resolution.
When a recipient visits a CustardUI page with a ?cv-highlight=... query parameter:
Extraction: FocusService extracts the parameter on page load or popstate.
Fuzzy Container Lookup:
elementId.containerId, containerTag, containerIndex).containerHash doesn't match, it scans neighboring sibling elements of the same tag to find a container with a matching hash.Text Range Matching:
startText (exact matching first, then whitespace-normalized fallback).endText near the expected offset (startOffset + textLength). For short ranges, it advances by textLength.Range.Integrity Validation:
textHash exactly, the highlight is verified.Rendering Path:
CSS.highlights.set(). The ranges are painted using CSS pseudo-elements:::highlight(cv-hl-yellow) { background-color: oklch(91% 0.19 96); color: oklch(22% 0.05 60); }
This method requires zero changes to the DOM tree, guaranteeing super-fast performance and preventing layout thrashing.<mark> element:<mark class="cv-hl-fallback cv-hl-fallback--yellow">highlighted text</mark>
Viewport Scroll:
firstRange.startContainer.parentElement?.scrollIntoView({
behavior: 'smooth',
block: 'center'
});