{"record":{"id":"8ac82ece220057cc","repo":"Automattic/harper","slug":"textfieldrange-expects-an-htmltextareaelement-or-h","errorCode":null,"errorMessage":"TextFieldRange expects an HTMLTextAreaElement or HTMLInputElement","messagePattern":"TextFieldRange expects an HTMLTextAreaElement or HTMLInputElement","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/lint-framework/src/lint/TextFieldRange.ts","lineNumber":35,"sourceCode":"\t\t}\n\t> = new WeakMap();\n\n\tprivate arena: { mirror: HTMLDivElement; text: Text; refs: number };\n\n\t/**\n\t * Create a range-like object for a given text input field.\n\t * @param field - A HTMLTextAreaElement or a HTMLInputElement (of type \"text\").\n\t * @param startOffset - The starting character index.\n\t * @param endOffset - The ending character index.\n\t */\n\tconstructor(\n\t\tfield: HTMLTextAreaElement | HTMLInputElement,\n\t\tstartOffset: number,\n\t\tendOffset: number,\n\t) {\n\t\t// In this case we assume the caller provided a text field\n\t\tif (!(field instanceof HTMLTextAreaElement || field instanceof HTMLInputElement)) {\n\t\t\tthrow new Error('TextFieldRange expects an HTMLTextAreaElement or HTMLInputElement');\n\t\t}\n\t\tthis.field = field;\n\t\tthis.startOffset = startOffset;\n\t\tthis.endOffset = endOffset;\n\t\tthis.arena = TextFieldRange.ensureArena(this.field);\n\t\tthis.arena.refs++;\n\t}\n\n\t/**\n\t * Creates (or reuses) an off-screen mirror element that mimics the field's styles\n\t * and positions it exactly over the field.\n\t */\n\tprivate static ensureArena(field: HTMLTextAreaElement | HTMLInputElement): {\n\t\tmirror: HTMLDivElement;\n\t\ttext: Text;\n\t\trefs: number;\n\t} {\n\t\tconst existing = TextFieldRange.arenas.get(field);","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/Automattic/harper/blob/5fe7d5ab76492d83f3ecdbc3f1da83c75dcb6f83/packages/lint-framework/src/lint/TextFieldRange.ts#L17-L53","documentation":"The TextFieldRange constructor validates its first argument with instanceof, requiring an HTMLTextAreaElement or HTMLInputElement. Any other element (div, contenteditable span, etc.) cannot back a text-field range, so the constructor fails fast instead of misbehaving later.","triggerScenarios":"new TextFieldRange(el, start, end) where el is not a <textarea> or <input>; common with contenteditable divs, generic HTMLElements, or elements fetched by a broad selector.","commonSituations":"Attaching the linter to a rich-text/contenteditable editor instead of a plain text field, passing a wrapper div, or running in a non-DOM environment (SSR) where instanceof checks fail or the element is a stub.","solutions":["Pass the actual <textarea> or <input> element to the constructor.","If you have a contenteditable surface, use the framework's non-text-field API (addTarget with an HTMLElement) instead of TextFieldRange.","Narrow your element selection (querySelector('textarea, input')) before constructing."],"exampleFix":"// before\nconst range = new TextFieldRange(document.querySelector('.editor'), 0, 10);\n// after\nconst el = document.querySelector('textarea.editor, input.editor');\nif (!el) throw new Error('No text field found');\nconst range = new TextFieldRange(el, 0, 10);","handlingStrategy":"type-guard","validationCode":"if (!(el instanceof HTMLTextAreaElement || el instanceof HTMLInputElement)) {\n  throw new TypeError('TextFieldRange requires a textarea or input element');\n}","typeGuard":"function isTextField(el: Element): el is HTMLTextAreaElement | HTMLInputElement {\n  return el instanceof HTMLTextAreaElement || el instanceof HTMLInputElement;\n}","tryCatchPattern":"try {\n  const range = new TextFieldRange(field, start, end);\n} catch (e) {\n  console.error('TextFieldRange requires a textarea/input; got', field);\n}\n","preventionTips":["Type the parameter as HTMLTextAreaElement | HTMLInputElement so TypeScript rejects other elements at compile time.","Narrow with querySelector('textarea, input') before constructing.","Use addTarget-based APIs for contenteditable or non-input surfaces."],"tags":["javascript","typescript","dom","type-mismatch"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"5fe7d5ab76492d83f3ecdbc3f1da83c75dcb6f83","analyzedAt":"2026-09-06T11:34:38.610Z","contentChangedAt":"2026-09-06T11:34:38.610Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}