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

Parsing html tag without body tag failed #818

Open
harryzcy opened this issue Jan 31, 2023 · 4 comments
Open

Parsing html tag without body tag failed #818

harryzcy opened this issue Jan 31, 2023 · 4 comments
Assignees
Labels
wontfix This will not be worked on

Comments

@harryzcy
Copy link

harryzcy commented Jan 31, 2023

Expected Behavior

parse('<html><p>Hello, World!</p></html>') should return a html tag, with a p child tag

Actual Behavior

It returns empty react element. console.log gives the following:

{
  ...
  props: {children: null}
  ...
}

Steps to Reproduce

Call parse('<html><p>Hello, World!</p></html>')

Reproducible Demo

Environment

  • Version: v3.0.8
  • Platform: node v18.12.1, TypeScript 4.9.5
  • Browser: Chrome v108
  • OS: macOS

Keywords

@harryzcy harryzcy added the bug Something isn't working label Jan 31, 2023
@remarkablemark
Copy link
Owner

remarkablemark commented Feb 11, 2023

@harryzcy This is expected behavior since if you're trying to render an <html> tag inside of a root element in a <body>, then you'll get the error:

Warning: validateDOMNesting(...): <html> cannot appear as a child of <div>.

See reproduced demo.

This leads me to the question: are you unable to strip out the <html> from your string before parsing?

@remarkablemark remarkablemark added wontfix This will not be worked on and removed bug Something isn't working labels Feb 11, 2023
@remarkablemark remarkablemark self-assigned this Feb 11, 2023
@harryzcy
Copy link
Author

harryzcy commented Feb 15, 2023

That's not the point I'm making. The warning that <html> cannot appear as a child of <div> is for when it's actually rendered in browser. But the bug I'm pointing out here is when it's parsed by html-react-parser, before it's even rendered by the browser.

I'm using the replace function in HTMLReactParserOptions to replace out the html tag, but it's not working for this specific case.

For reference, parse('<html><body><p>Hello, World!</p></body></html>') does work but parse('<html><p>Hello, World!</p></html>') doesn't. See this demo

So I still think it's a bug within the library.

@remarkablemark
Copy link
Owner

Hmm perhaps I am misunderstanding something @harryzcy, but your demo seems to be working correctly? I inspected the console output and it is rendering html > body > p and text

@harryzcy
Copy link
Author

harryzcy commented Mar 4, 2023

My demo is the working example that has parse('<html><body><p>Hello, World!</p></body></html>'). Your demo exactly shows when it's not working, when it's parse('<html><p>Hello, World!</p></html>') without the body tag. @remarkablemark

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

No branches or pull requests

2 participants