question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

html-serializer doesn't work with nested blocks

See original GitHub issue

Do 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:closed
  • Created 6 years ago
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

11reactions
pvandecommented, Apr 11, 2018

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.

<ul>
  <li>
    Text Content
    <ul>
      <li>Nested Text Content</li>
    </ul>
  </li>
</ul>

The implicit behavior of silently destroying content feels like it needs a strong justification and prominent documentation.

10reactions
bengotowcommented, Jan 8, 2018

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):

/**
 * Only allow block nodes or inline and text nodes in blocks.
 *
 * @type {Object}
 */

{
  validateNode: function validateNode(node) {
    if (node.object != 'block') return;
    var first = node.nodes.first();
    if (!first) return;
    var objects = first.object == 'block' ? ['block'] : ['inline', 'text'];
    var invalids = node.nodes.filter(function (n) {
      return !objects.includes(n.object);
    });
    if (!invalids.size) return;

    return function (change) {
      invalids.forEach(function (child) {
        change.removeNodeByKey(child.key, { normalize: false });
      });
    };
  }
},

If the input HTML contains <div> tags and the Serializer rules convert those div 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 blocks 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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found