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

important style is not parsed #1302

Open
piszczu4 opened this issue Feb 9, 2024 · 13 comments
Open

important style is not parsed #1302

piszczu4 opened this issue Feb 9, 2024 · 13 comments
Assignees
Labels
question Further information is requested wontfix This will not be worked on

Comments

@piszczu4
Copy link

piszczu4 commented Feb 9, 2024

The following is not parsed when using attributesToProps(domNode.attribs):

style="margin-left: 24px!important"

while this is:

style="margin-left: 24px"

Why is that a case?

@piszczu4 piszczu4 added the bug Something isn't working label Feb 9, 2024
@remarkablemark
Copy link
Owner

@piszczu4 this is expected behavior because this library parses inline styles and inline styles do not support !important

@remarkablemark remarkablemark added question Further information is requested and removed bug Something isn't working labels Feb 9, 2024
@piszczu4
Copy link
Author

piszczu4 commented Feb 9, 2024

@piszczu4 this is expected behavior because this library parses inline styles and inline styles do not support !important

Why does not support? Look here: https://codepen.io/degel123/pen/poYOLzE

@remarkablemark
Copy link
Owner

@piszczu4 can you provide a reproducible example of this not working for you?

@piszczu4
Copy link
Author

piszczu4 commented Feb 9, 2024

Here it is: https://codesandbox.io/p/sandbox/parser-hf4g76?file=%2Fsrc%2FApp.tsx%3A30%2C44-30%2C53

If you remove !important, it starts to work.

@remarkablemark
Copy link
Owner

@piszczu4 I can't seem to open the CodeSandbox?

Screen Shot 2024-02-09 at 10 41 02 AM

@piszczu4
Copy link
Author

piszczu4 commented Feb 9, 2024

Try this: https://codesandbox.io/p/sandbox/parser-hf4g76?file=%2Fsrc%2FApp.tsx

Here is the code:

import * as Popover from "@radix-ui/react-popover";
import * as Tooltip from "@radix-ui/react-tooltip";
import * as React from "react";
import { forwardRef, ReactNode } from "react";
import "./styles.css";

import parse, {
  DOMNode,
  Element,
  HTMLReactParserOptions,
  attributesToProps,
  domToReact,
} from "html-react-parser";

export default function App() {
  const options = {
    replace(domNode: DOMNode) {
      if (!(domNode instanceof Element)) return domNode;

      if (domNode.tagName === "p") {
        return (
          <p {...attributesToProps(domNode.attribs)}>
            {domToReact(domNode.children as DOMNode[], options)}
          </p>
        );
      }
    },
  };

  const html = `<p style="margin-left:24px!important">Example</p>`;
  return <div className="App">{parse(html, options)}</div>;
}

@piszczu4
Copy link
Author

Any feeedbaack?

@piszczu4
Copy link
Author

Any progress on that one?

@ArtsiomReutovich
Copy link

Hello Guys, are you going to fix the issue?

@remarkablemark
Copy link
Owner

remarkablemark commented Apr 4, 2024

I've been really busy these days so I haven't had a chance to look into this. If anyone has a solution, feel free to open a PR and I can review it.

I did a quick search and it looks like React does not support important inline styles: https://stackoverflow.com/questions/23074748/important-inline-styles-in-react

@remarkablemark remarkablemark added the wontfix This will not be worked on label Apr 4, 2024
@ArtsiomReutovich
Copy link

ArtsiomReutovich commented Apr 4, 2024

Hello @remarkablemark,

In our case, the problem is mostly related to some styles being missed after it was processed by the plugin.

For instance, we have node:
<span style="color:red!important;font-size:14px">Text</span>

After the conversion becomes:
<span style="font-size:14px">Text</span>

As you can see "color" style is missing.
Can we have the possibility of "saving" style even if it was passed with "!important"?
like:
<span style="color:red;font-size:14px">Text</span>

@remarkablemark
Copy link
Owner

@ArtsiomReutovich can replace work as a workaround for you? If it doesn't, a quick hack would be to do a replaceAll('!important', '') for your HTML string.

@ArtsiomReutovich
Copy link

ArtsiomReutovich commented Apr 5, 2024

Hello @remarkablemark,
Yes, I'm doing it this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants