Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusable block list having additional lines added #4205

Closed
karmatosed opened this issue Dec 29, 2017 · 2 comments · Fixed by #4304
Closed

Reusable block list having additional lines added #4205

karmatosed opened this issue Dec 29, 2017 · 2 comments · Fixed by #4304
Assignees
Labels
[Feature] Blocks Overall functionality of blocks [Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) [Type] Bug An existing feature does not function as intended

Comments

@karmatosed
Copy link
Member

This is a weird bug, I am not even sure how replicable but I got it several times.

I was writing a blog post and had a reusable block. It was a list and on auto saving this happened:

issue-extraline-reusableblocks

That extra line I did not add :/ Most odd and unfortunately I couldn't work out the cause of this so posting in hope we can dig a bit. I was using Chrome at the time.

@karmatosed karmatosed added [Feature] Blocks Overall functionality of blocks [Type] Bug An existing feature does not function as intended labels Dec 29, 2017
@noisysocks noisysocks added the [Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) label Jan 3, 2018
@noisysocks noisysocks self-assigned this Jan 3, 2018
@noisysocks
Copy link
Member

Steps to reproduce:

  1. Convert a list to a reusable block
  2. Open the inserter

It looks like this is due to a bug in the list block.

When a list block is serialize()d, it generates markup that looks like this:

<!-- wp:list -->
<ul>
    <li>A</li>
    <li>B</li>
</ul>
<!-- /wp:list -->

Note that the markup is nicely indented. When we later parse() this markup, we get attributes that look like this:

values: {
    0: '    ',
    1: { type: 'li', props: { children: 'A' } ... },
    2: '    ',
    3: { type: 'li', props: { children: 'B' } ... },
    4: '    ',
}

Note the empty values: they're there because we've sourced from children which picks up both the text nodes and element nodes that exist as children of the <ul>.

This isn't ordinarily a problem because we pass values into Editable.defaultValue which is smart enough to consider only element nodes. But, when a reusable block is updated, we call editor.setContent( content, { format: 'raw' } ) (see #667). Since we're disabling TinyMCE's sanitisation, the text nodes aren't filtered out and so we end up with empty <li>s.

There's a few ways we can fix this:

  1. We could call editor.setContent( content ) instead of editor.setContent( content, { format: 'raw' } ).
  2. We could change the core/list selector to select child elements instead of child nodes
    1. This could be done by adding a flag to the children matcher, e.g.:
      attributes: {
          values: {
              type: 'array',
              source: 'children',
              selector: 'ol,ul',
              filterElements: true, // <-- selects only nodes with node.nodeType === NODE_ELEMENT
              default: [],
          },
      },
    2. Or, this could be done by using the query matcher in a way similar to how core/quote works, e.g.:
      attributes: {
          value: {
              type: 'array',
              source: 'query',
              selector: 'body > ol > *, body > ul > *',
              query: {
                  children: 'element',
              },
              default: [],
          },
      },

@aduth: why do we disable TinyMCE's sanitisation? Do you have a preference on which way we fix this?

@noisysocks noisysocks assigned noisysocks and unassigned noisysocks Jan 4, 2018
@aduth
Copy link
Member

aduth commented Jan 4, 2018

@aduth: why do we disable TinyMCE's sanitisation? Do you have a preference on which way we fix this?

If I recall correctly, it was just to ensure that we were working with as raw a markup as possible, assuming that what's generated from and into markup by React is what we want to use; obviously we hadn't accounted for prettified HTML. There's also a bit of overhead in using the non-raw content setters / getters. Lastly, I believe we're currently passing 'raw' as the argument to getContent (where performance could matter a bit more), so likely this way just for consistency's sake.

Off the top of my head, I don't see any issues that would discourage us from setting content without { format: 'raw' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks [Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants