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

SVG opacity conflicts with attributify opacity="" #2104

Closed
YunYouJun opened this issue Jan 22, 2023 · 3 comments · Fixed by #2110
Closed

SVG opacity conflicts with attributify opacity="" #2104

YunYouJun opened this issue Jan 22, 2023 · 3 comments · Fixed by #2110

Comments

@YunYouJun
Copy link
Contributor

SVG can use opacity="0.7" means opacity: 70%.

It also will parsed by unocss as opacity: 0.007 means opacity: 0.7%.

<svg class="waves" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto">
  <defs>
    <path id="gentle-wave" d="M-160 44c30 0 58-18 88-18s 58 18 88 18 58-18 88-18 58 18 88 18 v44h-352z" fill="var(--yun-c-cloud)" />
  </defs>
  <g class="parallax">
    <use xlink:href="#gentle-wave" x="48" y="0" opacity="0.7" />
    <use xlink:href="#gentle-wave" x="48" y="7" />
  </g>
</svg>
@sibbng
Copy link
Member

sibbng commented Jan 22, 2023

I also encountered some false positives in SVGs

Playground

In the playground above, it extracts some margins from path's d attribute and fill attributes.

fill and opacity issues can be fixed temporarily by adding those attributes to ignoreAttributes:

presetAttributify({ignoreAttributes: ['placeholder', 'fill', 'opacity']})

But this will ignore these attributes in other places too, not just SVGs. And d attribute issue is still there.

In my opinion, we should have some special handling for SVGs.

fill and opacity issues can be fixed here:

d issue can be fixed here:

export const splitCode = (code: string) => {

My suggested solution is to remove these attributes from the code using regex.

@UltraCakeBakery
Copy link
Contributor

Not sure if this comment (still) applies here: #205 (comment)

@YunYouJun
Copy link
Contributor Author

Not sure if this comment (still) applies here: #205 (comment)

Thanks. It's a good idea to add namespaces to avoid confusion between unocss and svg attributes.
Unfortunately, this does not prevent svg's inherent properties from being parsed by unocss.

As @sibbng says, we should remove unocss parsing of svg attributes.
But regular expression is really hard for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants