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

Ignore attribute when attribute content is invalid #141

Closed
shiglet opened this issue Mar 3, 2020 · 6 comments
Closed

Ignore attribute when attribute content is invalid #141

shiglet opened this issue Mar 3, 2020 · 6 comments
Labels
question Further information is requested

Comments

@shiglet
Copy link

shiglet commented Mar 3, 2020

html-react-parser is a great tool ! But unfortunately, i'm having an exception when i'm trying to parse html string that content invalid attribute, so my app is crashing : undefined:1:2: property missing ':'. I can of course try that exception to avoid the app to crash but it won't really solve my issue.

I'm aware that's not to html-react-parser fault, but i would like to know if it's possible with html-react-parser to ignore some attribute when they are invalid, in my case i'm having troubles with an invalid style attribute. Its content isn't valid. If not, do you have any other idea to achieve that ?

Thank you !

@remarkablemark
Copy link
Owner

This sounds like invalid CSS in your HTML. See 'The parser throws an error'.

@remarkablemark
Copy link
Owner

This is related to #125

@shiglet
Copy link
Author

shiglet commented Mar 4, 2020

Hello,

Thank you for your answer. I had already seen this issues. Unfortunately, i can't use sanitize-html, the reason is that i need to keep valid styling, and remove them only when it's invalid.

Is there any way to remove that with sanitize-html or another library ? I don't really know how can i actually know in javascript if the given style is actually valid or not. I know it's not related to html-react-parser, but i could actually use the html-react-parser replace method to remove that style attribute as long as i know how to be sure that the style is valid or not.

EDIT: Maybe should i need to edit (in local) the inline-style-parser to return null instead of throwing that "missing ':'" error when the attribute content isn't valid ?

Thank you.

@remarkablemark
Copy link
Owner

Since the library uses style-to-object under the hood (to parse inline style attribute into an object), you can wrap the style parsing in a try-catch:

const parse = require('html-react-parser');
const { domToReact } = parse;
const style = require('style-to-object');

parse('<hr style="/*">', {
  replace: domNode => {
    if (domNode.attribs && domNode.attribs.style) {
      try {
        style(domNode.attribs.style);
      } catch (error) {
        // delete the attribute that's causing the error
        // then convert the dom node to react
        delete domNode.attribs.style;
        return domToReact(domNode);
      }
    }
  },
});

See Repl.it example.

@remarkablemark remarkablemark added the question Further information is requested label Mar 10, 2020
@shiglet shiglet closed this as completed Mar 10, 2020
@ZigZagT
Copy link

ZigZagT commented Mar 21, 2020

Thank you @remarkablemark . I also struggle with this issue for quite a long time. Very appreciate for the solution!

@remarkablemark
Copy link
Owner

remarkablemark commented Mar 21, 2020

You're very welcome

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

No branches or pull requests

3 participants