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

Lit template vars are incorrectly escaped when using a svelte component #1529

Open
2 of 13 tasks
Jfoltz-adobe opened this issue Aug 8, 2024 · 3 comments
Open
2 of 13 tasks
Labels
bug Something isn't working

Comments

@Jfoltz-adobe
Copy link

Jfoltz-adobe commented Aug 8, 2024

I am interested in helping provide a fix!

Yes

Which generators are impacted?

  • All
  • Angular
  • HTML
  • Preact
  • Qwik
  • React
  • React-Native
  • Solid
  • Stencil
  • Lit
  • Svelte
  • Vue
  • Web components

Reproduction case

https://mitosis.builder.io/playground/?code=DwZwxgTglgDgLgPgFAAIVgPYDsR3QGwEMQQA5QgWwFMUBeFAcgBNCIBrBgbiWAHpxo8ZDyZQAbgWIhaAbzBES5agF9kaNKHwY8vZH1FjkQA%3D

Expected Behaviour

Should compile correctly

  render() {
    return html`

          <div class="{${state.className}}"><my-slot></my-slot></div>

        `
  }

Actual Behaviour

  render() {
    return html`

          <div class="{`${state.className}`}"><my-slot></my-slot></div>

        `
  }

Additional Information

"@builder.io/mitosis": "^0.3.20",
"@builder.io/mitosis-cli": "^0.3.20",

In the playground the svelte templates don't appear to be working

@Jfoltz-adobe Jfoltz-adobe added the bug Something isn't working label Aug 8, 2024
@Jfoltz-adobe
Copy link
Author

Looks like the issue is in this file: https://github.com/BuilderIO/mitosis/blob/main/packages/core/src/generators/lit/collect-class-string.ts

It looks like some of the dynamic class strings include string template markers. I was able to get this working locally by updating: https://github.com/BuilderIO/mitosis/blob/main/packages/core/src/generators/lit/generate.ts

All I had to do was strip string template characters like this:

collectClassString(json)?.replace(/`/g,'');

So I'm guessing something changed in the class string data that started including template tags. The simplest fix was just to strip them but it might be a sign of a bigger regression.

@samijaber
Copy link
Contributor

This is specific to class for some reason:

} else {
// otherwise just assign
code = '`' + code + '${' + binding + '}`';
mitosisNode.bindings.class = createSingleBinding({ code });
}

if (attribute.name === 'class') {
code = mitosisNode.bindings.class?.code?.length
? insertAt(
mitosisNode.bindings.class.code,
' ${' + code + '}',
mitosisNode.bindings.class.code.length - 1,
)
: '`${' + code + '}`';
}

@raymondmuller is the one who wrote the Sveltosis parser, not sure what the reason was for this special behaviour 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants