Krombik / html2react

Transmute HTML markup into React components
MIT License
1 stars 1 forks source link

Improve detection of HTML to avoid false positives #1

Closed quantizor closed 6 months ago

quantizor commented 6 months ago

For an input string like:

"Here is some text with an < angle bracket"

Currently the < is being considered as HTML and the HTML2React component fails with error "invalid html".

Krombik commented 6 months ago

Hi, < is not valid symbol, it should be as &lt;, if you know that provided html may contains some invalid symbols you can fix it with this method:

const fixHtml = (html: string) =>
  new DOMParser().parseFromString(html, 'text/html').body.innerHTML;

for "Here is some text with an < angle bracket" it returns "Here is some text with an &lt; angle bracket"

quantizor commented 6 months ago

Hmm I tried this originally and it just printed &lt; directly into the DOM as plain text without converting back to the original character entity. I'll try playing with it some more... part of the trick is the backing template is used in places other than just the DOM (browser title for example.)

Krombik commented 6 months ago

You can try to use processTextSegment prop with this method:

(text: string) => text.replace('&lt;', '<')
Krombik commented 6 months ago

I figured out how to handle it, will be in the next update

Krombik commented 6 months ago

Sorry, the solution of this problem makes logic too complicated, so it will not be implemented

Krombik commented 5 months ago

@quantizor this problem was fixed in 1.2.0

quantizor commented 5 months ago

Glad to hear it, thanks!