siyuan-note/siyuan · error · TypeError

The tracked range must contain semantic content

Error message

The tracked range must contain semantic content

What it means

trackedRange.ts validates that a non-collapsed tracked range in a Protyle editor actually covers tokens with semantic content. When the selection spans no stream tokens (e.g. only whitespace/zero tokens inside one editable block), the library refuses to create a tracked-range handle and throws this TypeError. This guards the tracking API from producing handles that would map to an empty token slice.

Solutions

  1. Check range.collapsed before creating a tracked range and skip/collapse the call when true
  2. Ensure the selection actually covers text content of the editable block (not only whitespace or element gaps)
  3. Re-derive the range after DOM re-renders instead of reusing stale offsets
  4. Wrap the call in try/catch and fall back to a degraded non-tracked selection path

Example fix

// before
const handle = createTrackedRange(protyle, range);
// after
if (!range.collapsed) {
    const handle = createTrackedRange(protyle, range);
}
const collapsedHandle = createTrackedRange(protyle, range);
Defensive patterns

Strategy: validation

Validate before calling

// guard before creating a tracked range
if (range.collapsed || range.toString().trim().length === 0) {
    return; // nothing semantic to track
}

Try / catch

try { const h = createTrackedRange(protyle, range); } catch (e) { if (e.message.includes("semantic content")) return null; throw e; }

Prevention

When it happens

Trigger: Calling createTrackedRange (the API right after this guard, app/src/protyle/util/trackedRange.ts:1151) with a range that is not collapsed but whose snapshot token slice rangeSnapshot.stream.tokens.slice(start, end) is empty — i.e. selection start == end offset-wise within the token stream, or the selection sits entirely between tokens.

Common situations: Programmatic selection code that clones a range across inline element boundaries; restoring a saved selection whose underlying DOM was re-rendered so the offsets now resolve to zero tokens; selecting trailing whitespace or an empty inline node inside a block.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/32d438bf57ff9af3. Report an issue: GitHub.

Appendix: source

Thrown at app/src/protyle/util/trackedRange.ts:1151

    if (destroyedProtyles.has(protyle)) {
        throw new Error("Cannot track a range in a destroyed Protyle instance");
    }
    if (!options?.owner || !["function", "object"].includes(typeof options.owner)) {
        throw new TypeError("The tracked range owner is required");
    }
    if (options.affinity && !["after", "before"].includes(options.affinity)) {
        throw new TypeError("The tracked range affinity must be before or after");
    }
    if (unloadingPlugins.has(options.owner)) {
        throw new Error("Cannot track a range for an unloaded plugin");
    }
    const rangeSnapshot = getRangeSnapshot(protyle, range);
    if (!rangeSnapshot) {
        throw new TypeError("The range must be inside one editable source block of this Protyle instance");
    }
    const targetTokens = rangeSnapshot.stream.tokens.slice(rangeSnapshot.start, rangeSnapshot.end);
    if (!range.collapsed && targetTokens.length === 0) {
        throw new TypeError("The tracked range must contain semantic content");
    }
    const handle = Object.freeze({id: `tracked-range-${++handleSequence}`});
    const trackedRange = range.cloneRange();
    const state: ITrackedRangeState = {
        range: trackedRange,
        startContainer: trackedRange.startContainer,
        endContainer: trackedRange.endContainer,
        exactTarget: getExactlySelectedElement(trackedRange),
        collapsed: trackedRange.collapsed,
        affinity: options.affinity || "before",
        targetTokens,
        tokens: rangeSnapshot.stream.tokens,
        start: rangeSnapshot.start,
        end: rangeSnapshot.end,
        sourceBlockID: rangeSnapshot.snapshot.block.getAttribute("data-node-id") || "",
        sourceAncestorIDs: getAncestorBlockIDs(protyle.wysiwyg.element, rangeSnapshot.snapshot.block),
        rootIDs: rangeSnapshot.stream.roots.map(root => root.getAttribute("data-node-id") || "").filter(Boolean),
        rootBlockIDs: getRootBlockIDs(rangeSnapshot.stream.roots),

View on GitHub (pinned to 9f775e8a12)