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

Support .ownerMathElement (or something akin to it) #241

Open
rschristian opened this issue Apr 30, 2024 · 5 comments
Open

Support .ownerMathElement (or something akin to it) #241

rschristian opened this issue Apr 30, 2024 · 5 comments
Labels

Comments

@rschristian
Copy link

rschristian commented Apr 30, 2024

Hopefully this is the correct place for such a request, let me know if not!

I'm looking to add support for rendering MathML elements w/ the correct namespace in Preact, but an issue I'm running up against is that it's quite challenging for us to figure out the namespace an element currently lives in. For a (rough & a bit silly) example:

<math>
    {condition ? <mi>1</mi> : <mo>2</mo>}
</math>

During the first rendering pass, we're able to keep track of the namespace as we delve into the children of <math>, however, when condition changes at some point in the future, we need to figure out the namespace before creating whichever element we're swapping to. This poses a bit of a challenge with the existing API.

With SVGs, we use .ownerSVGElement !== undefined which is pretty effective. Unfortunately, modern MathML doesn't seem to have an equivalent we could use, though it looks like MathML 2 supported this.

Is this by chance something worth supporting? Not quite sure yet how we can work around this as we have quite tight perf & size limitations.

Edit: We can use .namespaceURI to determine the existing namespace, but this is a bit less than ideal.

@dginev
Copy link

dginev commented Apr 30, 2024

[Workaround comment]

Side-stepping the main question (which is interesting), would other kinds of workarounds be acceptable? One that avoids namespaces is testing for the MathMLElement interface

parentDom instanceof MathMLElement

@rschristian
Copy link
Author

Indeed, that could be viable too.

However, I believe instanceof will perform considerably worse in most situations and it also relies on having MathMLElement actually defined, which might not be the case in non-browser environments. Things can be shimmed of course, but that's perhaps less ideal than shimming a method.

@lukewarlow
Copy link
Member

lukewarlow commented Apr 30, 2024

[Workaround]

Your best bet in that case is probably to make use of the element's namespaceURI property, this would work for HTML, SVG, and MathML all together so might allow cleaner code than using ownerSVGElement along side instanceof MathMLElement.

Edit: Which I see you've added to OP missed that.

@rschristian
Copy link
Author

Personally, I'd say checking .namespaceURI is far, far less clean, not to mention lacking in functionality.

Getting the owner element is useful, and consistent in the platform, even if not 100% required here. Was more using that as an example situation in which it could be used.

@davidcarlisle
Copy link
Collaborator

Thanks this seems not unreasonable, but we have labeled this to be considered for mathml-core level 2. as we are limited in what we can add to mathml-core at this stage in the process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants