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

Proposal: Allow custom elements to be in any namespace #634

Open
hfmanson opened this issue Apr 3, 2017 · 15 comments
Open

Proposal: Allow custom elements to be in any namespace #634

hfmanson opened this issue Apr 3, 2017 · 15 comments

Comments

@hfmanson
Copy link

@hfmanson hfmanson commented Apr 3, 2017

In the current spec custom elements can only be in the namespace http://www.w3.org/1999/xhtml
and without using is="" derive from HTMLElement. It is desirable to have custom elements in other namespaces

  • Create custom SVG elements in the SVG namespace which derive from SVGElement
  • Create elements in your own namespace using XHTML which derive from Element

My proposal is to add a ‘namespace’ property to ElementDefinitionOptions to specify in which namespace the custom element should be placed. I created an issue before for Custom Elements v0.

Advantages

  • Custom element name clashes will no longer occur
  • If the namespace isn't used by the browser for rendering (e.g. http://www.w3.org/1999/xhtml or http://www.w3.org/2000/svg) the dash requirement for new custom element tag names can be removed.
  • Possible to implement polyfills for XML dialects such as XForms, MathML or X3D

References

"Distributed Extensibility: Finally Done Right?" in XML Prague 2014 proceedings
Are web components seriously not namespaced? on the w3c mailing list

Implementation

https://github.com/hfmanson/custom-elements/tree/namespace-aware is a fork that implements this feature. A live demo is at http://mansoft.nl/custom-elements/

This polyfill needs to be run on a browser that does not support Custom Elements natively, e.g. Microsoft Edge or Firefox 58.

@trusktr
Copy link

@trusktr trusktr commented Feb 10, 2018

This is an interesting topic. It's completely worthy to note that someone can opt into making a UI entirely out of SVG, but then they're unfortunately out of luck if they want to use Web Components. It would be great to use Web Components with any type of element (just like we can use React/Angular/Vue with any type of elements).

Worth noting: Scoped Custom Element Registries can also solve some of the namespacing problems.

Maybe, with scoped Custom Element registries, there could be a feature to allow for Custom Elements to define which class that elements distributed into a shadow root are upgraded to. No idea how it'd work, but seems like that topic may lend to that area of discussion. ( @justinfagnani )

@jarek-foksa
Copy link

@jarek-foksa jarek-foksa commented Feb 10, 2018

I second that. I'm making extensive use of v0 custom SVG elements in Boxy SVG to implement on-canvas editing controls. Encapsulated SVG components could be also useful when doing complex charts and diagrams.

@annevk
Copy link
Member

@annevk annevk commented Mar 5, 2018

We discussed this in Tokyo, there isn't much interest from implementers. And SVG generally isn't getting much attention, despite having loads of technical debt. And to really make use of this SVG would have to be able to use shadow roots too (and its layout model have to work with that too).

@bzbarsky
Copy link

@bzbarsky bzbarsky commented Mar 5, 2018

Note that the original shadow DOM proposals did try to support this. Would be useful context to know why it was dropped. @dglazkov

@jarek-foksa
Copy link

@jarek-foksa jarek-foksa commented Mar 5, 2018

@annevk As discussed here, the needed changes to the SVG layout model seem rather trivial. SVG 2 spec is already defining <use> elements using Shacow DOM model: https://svgwg.org/svg2-draft/struct.html#UseShadowTree.

@bzbarsky WebKit team did not want SVG support in v1 specs for the sake of simplicity and ease of implementation: #110 (comment). I don't recall them dismissing the idea altogether.

@bzbarsky
Copy link

@bzbarsky bzbarsky commented Mar 5, 2018

@jarek-foksa Right, but what were the specific complications and difficulties associated with it? "simplicity" and "ease of implementation" suggests those existed, unless they're code for "don't even want to think about it".

@bzbarsky
Copy link

@bzbarsky bzbarsky commented Mar 5, 2018

In particular, I think I kow what the issues were with supporting this in the initial shadow DOM setup (all sorts of complications around ensuring that the proto chain matched the element type created etc), but most if not all of those should be gone in the [HTMLConstructor] setup. I haven't had a chance to think through what issues, if any, remain, hence asking whether someone else already has.

@jarek-foksa
Copy link

@jarek-foksa jarek-foksa commented Mar 5, 2018

@bzbarsky I'm not sure about the specifics, but I recall that initial implementation of custom SVG elements in Chromium was ridden with bugs and it took them like 2 years to make it fully usable. For example, the styles and IDs in the shadow DOM were not properly scoped, thus leaking to the global scope and causing naming clashes.

@bzbarsky
Copy link

@bzbarsky bzbarsky commented Mar 5, 2018

Just to be clear, custom elements and shadow DOM are orthogonal in terms of both specification and implementation.

The problem you describe has to do with shadow DOM, and is going to be a problem for shadow DOM even if you only allow HTML elements to be shadow hosts, as long as you put SVG elements in the shadow tree. It seems completely unrelated to custom elements.

@jarek-foksa
Copy link

@jarek-foksa jarek-foksa commented Mar 5, 2018

@bzbarsky Yeah, I was assuming that custom SVG elements would make sense only with shadow DOM support as mentioned by Anne. Anyway, in Chromium most issues with ID and style scoping were specific to SVG shadow trees, for example: https://bugs.chromium.org/p/chromium/issues/detail?id=424598

@hayatoito
Copy link
Member

@hayatoito hayatoito commented Mar 6, 2018

SVG 2 spec is already defining elements using Shacow DOM model: https://svgwg.org/svg2-draft/struct.html#UseShadowTree.

That would be just a sort of the implementation detail, and we should be careful not to make a decision based on that.

We should ask and clarify the intention why SVG is trying to use Shadow DOM as an implementation of <use> there.
I guess that is just the result of spec-ing how Blink is implementing SVG's <use>, where there is no other intention.

@gertcuykens
Copy link

@gertcuykens gertcuykens commented Apr 13, 2019

Hmm is this the proposal that would also make is work for class SvgIcon extends SVGElement ?

      import {html, render} from 'https://unpkg.com/lit-html?module'
      import SvgIcon from './SvgIcon.js'
      customElements.define('svg-icon', SvgIcon, {extends: 'svg'})
      render(html`
        <svg is="svg-icon" viewBox="0 0 75 75" icon="add"></svg>
        `, document.body)

Uncaught DOMException: Failed to execute 'define' on 'CustomElementRegistry': "svg" is an HTMLUnknownElement

@ExE-Boss
Copy link

@ExE-Boss ExE-Boss commented Apr 13, 2019

@gertcuykens It should.

@hfmanson

If the namespace isn't used by the browser for rendering (e.g. http://www.w3.org/1999/xhtml or http://www.w3.org/2000/svg) the dash requirement for new custom element tag names can be removed.

Maybe keep the dash restriction for official w3c namespaces.

This is also because MathML elements are all implemented using just the Element interface (there’s no MathMLElement WebIDL interface and rendering is handled via custom appearance and display values).

@bkardell
Copy link

@bkardell bkardell commented May 22, 2019

@ExE-Boss note we are interested in improving the idl situation here mathml-refresh/mathml#83

@justinfagnani
Copy link

@justinfagnani justinfagnani commented Apr 6, 2020

I recently noticed that SVG2 specs the behavior of unknown tags to act like <g>: https://www.w3.org/TR/SVG2/struct.html#UnknownElement

The problem is no browser implements this. If they did, we would be able to "polyfill" custom SVG elements much like we do with HTML custom elements.

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

Successfully merging a pull request may close this issue.

None yet
10 participants
You can’t perform that action at this time.