Slate: Invalid marks in schema are not removed

Created on 24 Apr 2018  Â·  13Comments  Â·  Source: ianstormtaylor/slate

Bug report

With this schema it shouldn't be possible to have any marks in the heading-one block:

const schema = {
  blocks: {
    'heading-one': {
      nodes: [{ objects: ['text'] }],
      marks: []
    }
  }
};

But as you can see I'm able to insert bold text in it:
image

What's the current behavior?

You can see this behaviour in action here:
https://youtu.be/mfI3U3KX9ug

To reproduce I'm just toggling a mark without actually making a selection and then simply writing.

bug ♥ help

Most helpful comment

@SmilinBrian you might want to use something like https://github.com/ianstormtaylor/slate-plugins/tree/master/packages/slate-auto-replace rather than validateNode for that

All 13 comments

Try marks: [''], as a work around. I think the [] should probably work though.

Thanks @CameronAckermanSEL but I'm getting the same result with either [] or [''].

Sorry, misunderstood. Able to reproduce this bug, specifically when inserting text. Appears to be because the insertion of text doesn't cause normalization so it doesn't pick up schema violations like this anymore.

Is this a good first task? I'd like to contribute to Slate.

@PierBover Yes, you can fix it here: packages/slate/src/changes/by-key in insertTextAtRange

  if (normalize !== undefined) {
    normalize = range.isExpanded
  }

to something like

  if (normalize !== undefined) {
    normalize = range.isExpanded && marks.size !== 0
  }

Ok I'll take a look tonight.

A discussion with "bupkis" on Slack on May 29th shows that this issue also applies to cases where the editor is intended to automatically convert certain sequences of characters typed into macros or something.

I made a JSFiddle showing this issue so that this other aspect of this problem can be fixed/tested as well: https://jsfiddle.net/SmilinBrian/d5qrhtgx/2/

[Edit: I note that the fix suggested above would not solve the aspect of the issue in my example. But given the PERF: Unless specified, don't normalize if only inserting text comment on those lines in changes/at-range.js, maybe automatic conversion based on characters typed should not be accomplished primarily by validateNode()? ]

@SmilinBrian you might want to use something like https://github.com/ianstormtaylor/slate-plugins/tree/master/packages/slate-auto-replace rather than validateNode for that

@isubasti slate-auto-replace is only suitable for when the user actually types. It doesn't work for example if you also need to process the initial state.

@samuelwagen yeah the schema normalization is still needed for some circumstances but for the use cases that @SmilinBrian pointed out, which is for macros then it will work. I believe it should work as it is anyway for the schema normalization on initial state

@samuelwagen this issue is in regards to insertTextAtRange not normalizing, so I think slate-auto-replace would still be a more appropriate approach to achieving the use case that @SmilinBrian described, as both insertTextAtRange and slate-auto-replace are tied closely to onKeyDown events.

Cases where you would want schema normalization to be applied to the text on load are out of scope for this issue and for slate-auto-replace

I agree that normalization on load is out of scope for this issue.

I still feel ambivalent about whether or not validateNode() should be called for all text insertion regardless of whether prior selection is collapsed. Seems to me it should be called, assuming validation is properly limited to "only the affected node(s)." If limited, any performance issues are due to the validations being too complex. That is, I'm assuming the built-in validations are fairly performant, and it would only be custom validations that might cause performance problems.

There are a couple of open PRs related to insertTextAtRange that address this.

The first of these triggers normalization when there's a mark, which I think is one case where normalization needs to be evaluated on insertion of text.

https://github.com/ianstormtaylor/slate/pull/1875
https://github.com/ianstormtaylor/slate/pull/1823

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ianstormtaylor picture ianstormtaylor  Â·  3Comments

ianstormtaylor picture ianstormtaylor  Â·  3Comments

markolofsen picture markolofsen  Â·  3Comments

bengotow picture bengotow  Â·  3Comments

bunterWolf picture bunterWolf  Â·  3Comments