ianstormtaylor/slate · error · Error

The <selection> hyperscript tag must have an <anchor> tag as

Error message

The <selection> hyperscript tag must have an <anchor> tag as a child with `path` and `offset` attributes defined.

What it means

Thrown by the <selection> hyperscript tag when no <anchor> child with both path and offset attributes was found. A Slate selection requires a defined anchor point, and hyperscript validates that anchor path/offset were provided.

Source

Thrown at packages/slate-hyperscript/src/creators.ts:165

  children: any[]
): PointToken {
  return new PointToken(attributes)
}

/**
 * Create a `Selection` object.
 */

export function createSelection(
  tagName: string,
  attributes: { [key: string]: any },
  children: any[]
): Range {
  const anchor: AnchorToken = children.find(c => c instanceof AnchorToken)
  const focus: FocusToken = children.find(c => c instanceof FocusToken)

  if (!anchor || anchor.offset == null || anchor.path == null) {
    throw new Error(
      `The <selection> hyperscript tag must have an <anchor> tag as a child with \`path\` and \`offset\` attributes defined.`
    )
  }

  if (!focus || focus.offset == null || focus.path == null) {
    throw new Error(
      `The <selection> hyperscript tag must have a <focus> tag as a child with \`path\` and \`offset\` attributes defined.`
    )
  }

  return {
    anchor: {
      offset: anchor.offset,
      path: anchor.path,
    },
    focus: {
      offset: focus.offset,
      path: focus.path,

View on GitHub (pinned to 72a37c701e)

Solutions

  1. Add <anchor path={[...]} offset={n} /> inside <selection>
  2. If you meant a collapsed selection, use a single <cursor /> tag with path/offset instead

Example fix

// before
<selection>
  <focus path={[0,0]} offset={0} />
</selection>
// after
<selection>
  <anchor path={[0,0]} offset={0} />
  <focus path={[0,1]} offset={2} />
</selection>
Defensive patterns

Strategy: validation

Validate before calling

// fixture-level check before createSelection:
const hasAnchor = children.some(c => c instanceof AnchorToken && c.path != null && c.offset != null)
if (!hasAnchor) throw new Error('fixture missing <anchor path offset> in <selection>')

Type guard

const isCompleteAnchor = (a?: AnchorToken): a is AnchorToken =>
  !!a && a.path != null && a.offset != null

Try / catch

null

Prevention

When it happens

Trigger: Writing <selection><focus path={[0,0]} offset={0} /></selection> without an <anchor/>, or an <anchor/> missing either the path or offset attribute.

Common situations: Building test fixtures with hyperscript and forgetting the anchor half of the selection, or omitting offset assuming it defaults to 0 (it does not).

Related errors


AI-assisted analysis of ianstormtaylor/slate@72a37c701e (2026-08-27). Data as JSON: /api/errors/b8809e4c339d7887. Report an issue: GitHub.