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

Error: img is a void element tag and must neither have children nor use dangerouslySetInnerHTML #14

Closed
poacher2k opened this issue Aug 26, 2016 · 14 comments
Assignees
Labels
bug Something isn't working

Comments

@poacher2k
Copy link
Contributor

poacher2k commented Aug 26, 2016

Hi!

Great plugin, really think this is going to be helpful for what I'm working on.

I noticed that parsing any img-element causes the above error. This may also apply to any element that never should have child elements.

A temporary workaround is to use something like this:

const parserConfig = {
    replace: domNode => {
        if(domNode.type === DOM_TYPE_TAG && domNode.name === 'img') {
            return <img src={domNode.attribs.src} alt={domNode.attribs.alt} className={domNode.attribs.class} />;
        }
    }
};
@remarkablemark
Copy link
Owner

Thanks for letting me know. Do you have an example so I can reproduce this error?

@poacher2k
Copy link
Contributor Author

Hi!
You can reproduce this with the following:
ReactDOM.render(Parser('<div><img src="" alt=""/></div>'), document.getElementById('root'));

@remarkablemark remarkablemark added the bug Something isn't working label Aug 29, 2016
@remarkablemark remarkablemark self-assigned this Aug 29, 2016
remarkablemark added a commit that referenced this issue Aug 29, 2016
- Fix void element bug (#14)
  - Void elements like <img> should not have any children
    (even an empty array `[]` counts as children)
- Pass `key` to the `replace` method as the second parameter
  - Prevent React "key" warning
- Update tests, mocks, and helpers
- Update README with update on `replace` method
@poacher2k
Copy link
Contributor Author

Awesome, thank you!

@remarkablemark
Copy link
Owner

@poacher2k Thanks for the example. I've looked into the bug and made a fix in #16.

Try npm update html-react-parser or npm install [email protected] and let me know if this removes the error for you.

@poacher2k
Copy link
Contributor Author

That fixed it, thanks again! Glad to see you also added checks for other void elements!

@remarkablemark
Copy link
Owner

Great to hear and you're very welcome! 👍

@vidit1
Copy link

vidit1 commented Jan 5, 2018

@remarkablemark I am still facing the issue and npm install [email protected] code is not updated in this version.

@remarkablemark
Copy link
Owner

@vidit1 Can you confirm that you installed the latest version?

npm i html-react-parser@latest

# or
npm i [email protected]

Then verify by doing:

npm ls html-react-parser

@blueskysmart
Copy link

blueskysmart commented Jan 12, 2018

@remarkablemark I updated the latest version html-react-parser and still face this issue. My element is inside a material-ui component.
<Drawer> <div className="alert" role="alert"> <img src="https://getbootstrap.com/assets/brand/bootstrap-solid.svg" width="30" height="30" className="d-inline-block align-top" alt=""> Bootstrap </img> </div> </Drawer>

@poacher2k
Copy link
Contributor Author

poacher2k commented Jan 12, 2018

@blueskysmart : You have the text "Bootstrap" inside your img-tag. As the error says, img is a void element, and cannot have any children.

See @remarkablemark 's comment below for guidance on how to fix it.

@remarkablemark
Copy link
Owner

@blueskysmart The comment made by @poacher2k is correct. You can see the reproduction of this error in this fiddle.

@blueskysmart
Copy link

Okie, I see. Thank you.

@cipiripper
Copy link

This error comes up also if you only try to parse this:
<img src="img.jpg".>

To make it work, wrap it in a div:

<div>
<img src="img.jpg".>
</div>

@Sshetty2
Copy link

I was running into this issue because I was trying to add children to an img tag...

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

6 participants