Skip to content

important style is not parsed #1302

Open

Description

@piszczu4

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?

Activity

  1. remarkablemark commented on Feb 9, 2024

    @remarkablemark
    Owner

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

  2. added
    questionFurther information is requested
    and removed
    bugSomething isn't working
    on Feb 9, 2024
  3. piszczu4 commented on Feb 9, 2024

    @piszczu4
    Author

    @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

  4. remarkablemark commented on Feb 9, 2024

    @remarkablemark
    Owner

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

  5. piszczu4 commented on Feb 9, 2024

    @piszczu4
    Author
  6. remarkablemark commented on Feb 9, 2024

    @remarkablemark
    Owner

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

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

  7. piszczu4 commented on Feb 9, 2024

    @piszczu4
    Author

    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>;
    }
    
    
  8. piszczu4 commented on Feb 12, 2024

    @piszczu4
    Author

    Any feeedbaack?

  9. piszczu4 commented on Feb 29, 2024

    @piszczu4
    Author

    Any progress on that one?

  10. ArtsiomReutovich commented on Apr 4, 2024

    @ArtsiomReutovich

    Hello Guys, are you going to fix the issue?

  11. remarkablemark commented on Apr 4, 2024

    @remarkablemark
    Owner

    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

  12. ArtsiomReutovich commented on Apr 4, 2024

    @ArtsiomReutovich

    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>

  13. remarkablemark commented on Apr 4, 2024

    @remarkablemark
    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.

  14. ArtsiomReutovich commented on Apr 5, 2024

    @ArtsiomReutovich

    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

Metadata

Metadata

Labels

questionFurther information is requestedwontfixThis will not be worked on

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions