{"record":{"id":"32d438bf57ff9af3","repo":"siyuan-note/siyuan","slug":"the-tracked-range-must-contain-semantic-content","errorCode":null,"errorMessage":"The tracked range must contain semantic content","messagePattern":"The tracked range must contain semantic content","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"app/src/protyle/util/trackedRange.ts","lineNumber":1151,"sourceCode":"    if (destroyedProtyles.has(protyle)) {\n        throw new Error(\"Cannot track a range in a destroyed Protyle instance\");\n    }\n    if (!options?.owner || ![\"function\", \"object\"].includes(typeof options.owner)) {\n        throw new TypeError(\"The tracked range owner is required\");\n    }\n    if (options.affinity && ![\"after\", \"before\"].includes(options.affinity)) {\n        throw new TypeError(\"The tracked range affinity must be before or after\");\n    }\n    if (unloadingPlugins.has(options.owner)) {\n        throw new Error(\"Cannot track a range for an unloaded plugin\");\n    }\n    const rangeSnapshot = getRangeSnapshot(protyle, range);\n    if (!rangeSnapshot) {\n        throw new TypeError(\"The range must be inside one editable source block of this Protyle instance\");\n    }\n    const targetTokens = rangeSnapshot.stream.tokens.slice(rangeSnapshot.start, rangeSnapshot.end);\n    if (!range.collapsed && targetTokens.length === 0) {\n        throw new TypeError(\"The tracked range must contain semantic content\");\n    }\n    const handle = Object.freeze({id: `tracked-range-${++handleSequence}`});\n    const trackedRange = range.cloneRange();\n    const state: ITrackedRangeState = {\n        range: trackedRange,\n        startContainer: trackedRange.startContainer,\n        endContainer: trackedRange.endContainer,\n        exactTarget: getExactlySelectedElement(trackedRange),\n        collapsed: trackedRange.collapsed,\n        affinity: options.affinity || \"before\",\n        targetTokens,\n        tokens: rangeSnapshot.stream.tokens,\n        start: rangeSnapshot.start,\n        end: rangeSnapshot.end,\n        sourceBlockID: rangeSnapshot.snapshot.block.getAttribute(\"data-node-id\") || \"\",\n        sourceAncestorIDs: getAncestorBlockIDs(protyle.wysiwyg.element, rangeSnapshot.snapshot.block),\n        rootIDs: rangeSnapshot.stream.roots.map(root => root.getAttribute(\"data-node-id\") || \"\").filter(Boolean),\n        rootBlockIDs: getRootBlockIDs(rangeSnapshot.stream.roots),","sourceCodeStart":1133,"sourceCodeEnd":1169,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/app/src/protyle/util/trackedRange.ts#L1133-L1169","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check range.collapsed before creating a tracked range and skip/collapse the call when true","Ensure the selection actually covers text content of the editable block (not only whitespace or element gaps)","Re-derive the range after DOM re-renders instead of reusing stale offsets","Wrap the call in try/catch and fall back to a degraded non-tracked selection path"],"exampleFix":"// before\nconst handle = createTrackedRange(protyle, range);\n// after\nif (!range.collapsed) {\n    const handle = createTrackedRange(protyle, range);\n}\nconst collapsedHandle = createTrackedRange(protyle, range);","handlingStrategy":"validation","validationCode":"// guard before creating a tracked range\nif (range.collapsed || range.toString().trim().length === 0) {\n    return; // nothing semantic to track\n}","typeGuard":null,"tryCatchPattern":"try { const h = createTrackedRange(protyle, range); } catch (e) { if (e.message.includes(\"semantic content\")) return null; throw e; }","preventionTips":["Never create tracked ranges from cloned/stale DOM ranges after re-render","Check range.collapsed and non-empty selected text first","Restrict tracked-range creation to user-driven selections of real text"],"tags":["editor","selection","invalid-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}