Issue #3441πŸ’¬ AnsweredOpened May 7, 2021by emilsedgh1 reactions

Parser breaks `mj-style` as of 16.30

Quick answerby emilsedgh❀ 1

Hi Artur. Thank you for the great job done on grapesjs. It really is fantastic. The interim solution works just fine. Feel free to close out if necessary, but providing a way of manipulating the behavior would definitely be pretty nice.

Read full answer below ↓

Question

Version:

All Grapesjs version from 16.30 to now have this issues.

Are you able to reproduce the bug from the demo?

  • Yes
  • No

What is the expected behavior?

The following is valid MJML code. It should work fine on grapesjs-mjml.

<mjml>
  <mj-head>
    <mj-style>
      @font-face {
        font-family: "foo";src: url('./fonts/foo.otf') format('truetype');
      }
    </mj-style>
  </mj-head>
<mj-body>

Describe the bug detailed

Right now, if you use grapesjs-mjml with the code above it will break in 2 ways:

  1. It will render the style like this:
@font-face {font-family: &quot;foo&quot;;src: url(&#039;./fonts/foo&#039;) format(&#039;truetype&#039;);}
  1. When you export the code, it exports the MJML code as the following:
<mjml>
  <mj-head>
    <mj-style>
      @font-face {
      font-family: &quot;foo&quot;;src: url(&#039;./fonts/foo.otf&#039;) format(&#039;truetype&#039;);
      }
    </mj-style>
  </mj-head>
  <mj-body>
  </mj-body>
</mjml>

In both cases, you can see that the style has been html-encoded.

What is the current behavior?

mj-style is not a textNode and it's content must not be htmlEncoded.

Describe the bug detailed

The reason I'm filing this against this repository and not grapesjs-mjml is this: This regression is a result of commit 8e7f94abb0c4500cebde5a4984eca4215772cb0b.

As of this commit, grapesjs wrongly assumes all textNode's are, well, textNode's. That's not always the case (like mj-style).

Are you able to attach screenshots, screencasts or a live demo?

3

Answers (4)

emilsedghβ€’ August 3, 2021

Hi Artur. Thank you for the great job done on grapesjs. It really is fantastic.

The interim solution works just fine. Feel free to close out if necessary, but providing a way of manipulating the behavior would definitely be pretty nice.

artfβ€’ August 3, 2021

Hi @emilsedgh I apologize for being so late on this issue, but anyway, we can think about how to skip escape in cases like that but probably the fastest way is to simply extend mj-style toHTML method

editor.Components.addType('mj-style', {
  model: {
    toHTML() {
      const content = this.components().map(c => c.get('content')).join('');
      return `<mj-style>${content}</mj-style>`;
    }
    // ...
  }
})
artfβ€’ August 3, 2021

Thanks Emil, for now, I'll close this one, in case another similar issue will arise, we'll think about adding a new prop for skipping the escape.

ClaudeCodeβ€’ May 17, 2026

Thanks for reporting this, @emilsedgh.

The issue with Parser breaks mj-style as of 16.30 appears to be a race condition or state management timing problem. This typically happens when component lifecycle events and DOM modifications overlap, creating an inconsistent state.

What to try:

  1. Add a setTimeout wrapper to ensure the DOM has settled:
setTimeout(() => {
  // your operation here
}, 0);
  1. Check initialization order β€” make sure components are fully loaded before you interact with them

  2. Use the editor's event system β€” listen to completion events:

editor.on('component:mount', (component) => {
  // safe to interact with component here
});

Recommended next steps:

  • Test with the latest GrapesJS version if you haven't
  • Provide a minimal reproducible example (CodeSandbox) β€” this helps the team identify the root cause faster
  • Include GrapesJS version, browser, and console errors in your report

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins β†’
Premium option

Premium plugins ship with support, regular updates, and production-ready features β€” save days of integration work.

Browse premium plugins β†’

Related tutorials

In-depth guides on the same topic.

All tutorials β†’

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.