html-serializer doesn't work with nested blocks
See original GitHub issueDo you want to request a feature or report a bug?
A bug
What’s the current behavior?
const BLOCK_TAGS = {
blockquote: 'quote',
p: 'paragraph',
//div: 'div'
}
const rules = [
{
deserialize(el, next) {
const type = BLOCK_TAGS[el.tagName.toLowerCase()]
if (!type) return
return {
kind: 'block',
type: type,
nodes: next(el.childNodes)
}
}
}
]
const pureHtml = '<blockquote><div>a text<blockquote>inner quote</blockquote></div></blockquote>'
const initialValue = new HtmlSerializer({ rules: rules }).deserialize(pureHtml);
It only renders a text
element, and I couldn’t see inner quote
.
See https://jsfiddle.net/oj53q1n2/26/
What’s the expected behavior?
We should see both text and quote.
Issue Analytics
- State:
- Created 6 years ago
- Comments:15 (11 by maintainers)
Top Results From Across the Web
Nested Blocks: Using InnerBlocks | Block Editor Handbook
You can create a single block that nests other blocks using the InnerBlocks component. This is used in the Columns block, Social Links...
Read more >Django REST Framework: Nested Serializers not appearing
When setting serializer_class to "UserSerializer", the nested data from the "NameSerializer" class isn't appearing.
Read more >Store data from nested block of gutenberg
I have two gutenberg blocks to make up an accordion. The parent accordion uses InnerBlocks with allowedBlocks set to just the panel block....
Read more >Schema API design - Discuss - ProseMirror
Hi all. I've been doing some work on delivering the promised feature of custom document schemas, along with a nice API for defining...
Read more >Jinja Documentation (3.0.x) » Template Designer ...
A Jinja template doesn't need to have a specific extension: .html , .xml , or ... It's the job of “child” templates to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’d be curious to hear more about why that validation rule exists at all. While I agree that there’s a conceptual correctness to it, there’s no such restriction in HTML. In addition to the example provided by @nghuuphuoc, the following is valid HTML that is “unrepresentable” in Slate.
The implicit behavior of silently destroying content feels like it needs a strong justification and prominent documentation.
After a bit more digging, the problem appears to be this constraint in the core Slate schema (which is enforced by Value.fromJSON inside the HTML deserializer):
If the input HTML contains
<div>
tags and the Serializerrules
convert thosediv
to blocks rather than ignoring them, it’s easy to create a structure that will be ripped apart by the schema validation after parsing, because Slate does not allow blocks to have both block children and text / inline children and this is a very common<div>
case.My solution is here: https://gist.github.com/bengotow/f5408e9cb543f22409d033df58e34579. Before running the HTML deserializer, I traverse the DOM tree and ensure that divs, blockquotes, and other nodes converted to Slate
block
s contain either text + inline children OR block children, wrapping children into blocks as necessary. Curious whether this would be welcomed as default behavior in some way (cc @ianstormtaylor).