Skip to content

Default to HTML parsing? #11

Open
Open

Description

@simevidas

Leaving out type="html" causes XML parsing, which breaks when the markup contains empty HTML elements that aren’t self-closed, e.g. <img>, <br>, <input>. This is especially problematic if the markup was generated by a Markdown processor (e.g. Marked does not self-close empty HTML elements). Due to this limitation, I think it makes sense to default to HTML parsing.

Related discussion in preact-markdown: laggingreflex/preact-markdown#1 (comment)

Activity

  1. changed the title [-]Default to type="html"?[/-] [+]Default to HTML parsing?[/+] on Mar 30, 2017
  2. developit commented on Mar 30, 2017

    @developit
    Owner

    I think this makes sense for preact-markdown for sure, but the reason it's not the default is because not all browsers support HTML parsing properly. I'd be interested in finding stats on which browsers support it though, if its IE9+ then maybe it'd be a good default.

  3. simevidas commented on Apr 1, 2017

    @simevidas
    Author

    I did some testing with preact-boilerplate and preact-markup in IE (code is here), and indeed there is an issue in IE9 (emulated) when using type="html". I get two instances of this in the console:

    preact-markup: Error: Unspecified error.
    

    No line number or anything is provided by the browser, so I’m unable to debug this. The page renders correctly in IE11 and IE10.

  4. developit commented on Apr 3, 2017

    @developit
    Owner
  5. TroyAlford commented on Apr 3, 2017

    @TroyAlford

    I wrote react-jsx-parser based on the code in this lib, and there are definitely a few frustrating things that I haven't been able to overcome via this approach, as well.

    If DOMParser uses application/xml for parsing, it will blow up, treating the content as invalid XML, (which I believe preact-markup will handle as-described above) whenever it encounters self-closing tags (such as <MyComponent /> or <img />). On the other hand, if you use text/html, (at least on Chrome), the browser will attempt to "correct" your JSX by doing things like converting:

    <MyComponent />
    <div>Content Here</div>
    

    into:
    <MyComponent><div>Content Here</div></MyComponent>

    I haven't found an elegant solution to this in my repo yet, either - but I'd love to collaborate on one. There's been some discussion related to it at TroyAlford/react-jsx-parser#4

  6. developit commented on Apr 5, 2017

    @developit
    Owner

    @TroyAlford if you ping me via preact-slack.now.sh I'd be happy to chat - seems like we need to find a decent wrapper around DOMParser that normalizes those differences.

    FWIW the self-closing thing could be avoided by setting the content type argument to application/xhtml+xml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions