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

DOMParser and MathML #8866

Closed
pygy opened this issue Feb 8, 2023 · 4 comments
Closed

DOMParser and MathML #8866

pygy opened this issue Feb 8, 2023 · 4 comments

Comments

@pygy
Copy link

pygy commented Feb 8, 2023

Now that all major engines support MathML once again, it would be nice to have the ability to parse it from JS without .innerHTML hacks.

Could the list of supported mime types for DOMParser.prototype.parseFromString be updated to accept MathML?

I suppose that the go-to type would be application/mathml xml which is what the W3C uses in the generic case.

The Wikidata page about MathML lists text/mathml which is mentioned in older standards. It is terser and more memorable, but I suppose it won't fly...

Using text/xml is not enough to get MathML to render

@annevk annevk transferred this issue from whatwg/dom Feb 8, 2023
@annevk
Copy link
Member

annevk commented Feb 8, 2023

  1. Why does text/xml not work? That demo isn't particularly clear about the inputs. I would not expect a different result between that and application/mathml xml.
  2. We don't really want to extend DOMParser. See Add DOMParser option to parse declarative shadow DOM #8759.

@pygy
Copy link
Author

pygy commented Feb 8, 2023

Sorry for the lack of clarity. markup.js exports a string containing a MathML document. Depending on the toggle state, it is rendered as .innerHTML or after being parsed as text/xml.

The latter doesn't work, it renders plain text.

🎉 for setHTML() and friends. new DOMParser().parseFromString(src, type) is needlessy convoluted.

The only reason I'd see to pursue adding a MathML mime type to DOMParser would be a matter of velocity re. shipping the feature. If I get it correctly, it is as complex as adding an entry in the .idl/.webidl files and a few tests, whereas setHTML is still being spec'ed out.

@andreubotella
Copy link
Member

andreubotella commented Feb 8, 2023

In order to use MathML with XML syntax, you need to use the MathML namespace. That is <math xmlns="http://www.w3.org/1998/Math/MathML"><!-- content here --></math>. Otherwise, it will parse as an element with the name math in no namespace, which will render with the default CSS properties. This is probably what you're seeing when you say it renders as plain text. So parsing with text/xml works as it should.

If you want to use the HTML syntax for MathML, you could just use something like new DOMParser("<math><!-- content here --></math>", "text/html").querySelector("math").

@pygy
Copy link
Author

pygy commented Feb 8, 2023

Thanks @andreubotella, my bad, indeed, I picked a MathML sample from the Web and didn't check the XML name space. So DOMParser with text/xml works for the XML syntax.

My use case is the m.trust() API in Mithril.js, which creates a vnode that can render arbitrary markup (think React's dangerouslySetHTML, but these nodes can have siblings). The engine knows if these nodes are rendered in HTML, SVG or MathML context, and parses the markup accordingly (with custom logic to make sure nodes that require a specific parent like <td> aren't stripped by the parser).

We use some logic that looks like your suggestion for SVG, but is built on top of .innerHTML because we had to support IE back in the day, and it didn't support DOMParser (at least some versions didn't).

The optimal solution in any case would be to rely on something like setHTML once it ships.

Thanks for your input!

@pygy pygy closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants