From eb181f32792283a62352a04c79ad8b995b74f562 Mon Sep 17 00:00:00 2001 From: Andreas Geffen Lundahl Date: Thu, 20 Feb 2020 22:00:59 +0100 Subject: [PATCH] Allow zero as offset on and inside (#3491) * Allow zero as offset on and inside When creating a selection using the hyperscript tag, you couldn't set zero as offset on and , because of a faulty falsiness check. This is fixed by checking if the offset is falsy and explicitly not zero. * Make sure offset can't be null or undefined on and Throw exception when offset is strictly null or undefined, on and when creating a . --- packages/slate-hyperscript/src/creators.ts | 4 +-- .../test/fixtures/selection-offset-start.js | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 packages/slate-hyperscript/test/fixtures/selection-offset-start.js diff --git a/packages/slate-hyperscript/src/creators.ts b/packages/slate-hyperscript/src/creators.ts index 0b569b8da..32d411244 100644 --- a/packages/slate-hyperscript/src/creators.ts +++ b/packages/slate-hyperscript/src/creators.ts @@ -152,13 +152,13 @@ export function createSelection( const anchor: AnchorToken = children.find(c => c instanceof AnchorToken) const focus: FocusToken = children.find(c => c instanceof FocusToken) - if (!anchor || !anchor.offset || !anchor.path) { + if (!anchor || anchor.offset == null || anchor.path == null) { throw new Error( `The hyperscript tag must have an tag as a child with \`path\` and \`offset\` attributes defined.` ) } - if (!focus || !focus.offset || !focus.path) { + if (!focus || focus.offset == null || focus.path == null) { throw new Error( `The hyperscript tag must have a tag as a child with \`path\` and \`offset\` attributes defined.` ) diff --git a/packages/slate-hyperscript/test/fixtures/selection-offset-start.js b/packages/slate-hyperscript/test/fixtures/selection-offset-start.js new file mode 100644 index 000000000..d00277152 --- /dev/null +++ b/packages/slate-hyperscript/test/fixtures/selection-offset-start.js @@ -0,0 +1,35 @@ +/** @jsx jsx */ + +import { jsx } from 'slate-hyperscript' + +export const input = ( + + word + + + + + +) + +export const output = { + children: [ + { + children: [ + { + text: 'word', + }, + ], + }, + ], + selection: { + anchor: { + path: [0, 0], + offset: 0, + }, + focus: { + path: [0, 0], + offset: 0, + }, + }, +}