[Bug?] Editor: cannot select a heading text only

I made the following QuickAdd user script, which selects the first H1 heading of the active note. The important thing is it excludes # at the top of the line from the selection. For example, for # heading, only heading is selected.

module.exports = ({ app }) => {
    const editor = app.workspace.activeEditor?.editor;
    if (!editor) return;
    let anchor = null;
    let head = null;
    for (let line = 0; line < editor.lineCount(); line++) {
        const lineText = editor.getLine(line);
        if (lineText.startsWith('# ')) {
            anchor = { line: line, ch: 2 };
            head = { line: line, ch: lineText.length };
            editor.setSelection(anchor, head);
            break;
        }
    }
}

It works as expected in SOURCE MODE:

Screen Recording 2023-10-17 at 1.04.04

However, when I run this code in LIVE PREVIEW, the entire line including the first # is selected:

Screen Recording 2023-10-17 at 1.06.35

I suspect this is due to the CodeMirror decoration used to hide # in Live preview.

I hope this behavior will be fixed in the near future. Thanks!

Edit: I replaced editor.setSelection(anchor, head); with the following CodeMirror counterpart, but the result was the same, as expected:

            editor.cm.dispatch({
                selection: { 
                    anchor: editor.posToOffset(anchor), 
                    head: editor.posToOffset(head) 
                }
            })

Update: I found a workaround: insert editor.setCursor(head); right before editor.setSelection(anchor, head);.