1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-13 18:53:59 +02:00

Allow zero as offset on <anchor> and <focus> inside <selection> (#3491)

* Allow zero as offset on <anchor> and <focus> inside <selection>

When creating a selection using the <selection> hyperscript tag, you couldn't
set zero as offset on <anchor> and <focus>, 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 <anchor> and <focus>

Throw exception when offset is strictly null or undefined, on <anchor> and
<focus> when creating a <selection>.
This commit is contained in:
Andreas Geffen Lundahl
2020-02-20 22:00:59 +01:00
committed by GitHub
parent 30bc30df4b
commit eb181f3279
2 changed files with 37 additions and 2 deletions

View File

@@ -152,13 +152,13 @@ export function createSelection(
const anchor: AnchorToken = children.find(c => c instanceof AnchorToken) const anchor: AnchorToken = children.find(c => c instanceof AnchorToken)
const focus: FocusToken = children.find(c => c instanceof FocusToken) 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( throw new Error(
`The <selection> hyperscript tag must have an <anchor> tag as a child with \`path\` and \`offset\` attributes defined.` `The <selection> hyperscript tag must have an <anchor> 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( throw new Error(
`The <selection> hyperscript tag must have a <focus> tag as a child with \`path\` and \`offset\` attributes defined.` `The <selection> hyperscript tag must have a <focus> tag as a child with \`path\` and \`offset\` attributes defined.`
) )

View File

@@ -0,0 +1,35 @@
/** @jsx jsx */
import { jsx } from 'slate-hyperscript'
export const input = (
<editor>
<element>word</element>
<selection>
<anchor path={[0, 0]} offset={0} />
<focus path={[0, 0]} offset={0} />
</selection>
</editor>
)
export const output = {
children: [
{
children: [
{
text: 'word',
},
],
},
],
selection: {
anchor: {
path: [0, 0],
offset: 0,
},
focus: {
path: [0, 0],
offset: 0,
},
},
}