mjmlio / vscode-mjml

MJML preview, lint, compile for Visual Studio Code.
https://marketplace.visualstudio.com/items?itemName=attilabuti.vscode-mjml
MIT License
178 stars 25 forks source link

<mj-style> formatting goes crazy when the css contains '<' or '>' #33

Open tennox opened 2 years ago

tennox commented 2 years ago

Expected behavior

CSS inside <mj-style> can use < and > characters as expected, e.g.

<mj-style>
    .foo > a {
        color: black;
        content: "something with < or >";
    }
    /* smiley face :-< */

Actual behavior

Steps to reproduce the problem

  1. Copy the below code sample into VSCode
  2. Run MJML: Beautify

Code sample

<mjml>
    <mj-head>
        <mj-style>
            p > a {
              color: #002435;
            }
        </mj-style>
        <mj-style>
            /* < */
        </mj-style>
    </mj-head>
</mjml>

Specifications

Other information

Managed to work around the issue using:

<!-- beautify ignore:start -->
<mj-style>
  ...
</mj-style><!-- beautify ignore:end -->
tennox commented 2 years ago

I think the issue (and #25) lies in this function's logic: https://github.com/mjmlio/vscode-mjml/blob/6417f0be61399da38750f2b007633975a6aeaf2f/src/helper.ts#L83

Issues I see:

Possible workaround is using lazy / non-hungry matchers and ditch the [<>]: <.*?style[^>]*?>(?:.+?)<.*?\/.*?style> See demo

Parsing HTML via RegEx is known to break hell loose