WICG / webcomponents

Web Components specifications
Other
4.37k stars 371 forks source link

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

Open hfmanson opened 7 years ago

hfmanson commented 7 years ago

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

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

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

trusktr commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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: https://github.com/w3c/webcomponents/issues/110#issuecomment-180642629. I don't recall them dismissing the idea altogether.

bzbarsky commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

@ExE-Boss note we are interested in improving the idl situation here https://github.com/mathml-refresh/mathml/issues/83

justinfagnani commented 4 years ago

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.

trusktr commented 4 years ago

Another question is, if we have custom SVG elements (as awesome as it would be for developers making things with better-organized SVG code), then we export the DOM as HTML and throw it at applications like Adobe Illustrator or Inkscape, what is supposed to happen?

akauppi commented 3 years ago

@trusktr I don't think that would be a problem. Either renderers ignore them altogether (as happens today, with unknown SVG elements in browsers), or they treat them as g if SVG2 is followed (see @justinfagnani 's message above).

So to me, this seems a non-concern. Any HTML/XML is intended to be expandable, so nothing new there.

justinfagnani commented 2 years ago

Is there anything we could do to get movement on allowing SVG custom elements? There are some very large unaddressed use cases here for charting, dynamic illustrations, drawing apps, very graphical UIs like node/edge editors, etc. Any chance SVG is in a better maintained state since 2018?

lordanubi commented 2 years ago

Definitely needing this, use tag is great but has its limits (no custom attributes for example)

andreafuturi commented 1 year ago

I think this a really important feature for improving UI

nileshtrivedi commented 7 months ago

+1 for allowing custom elements to be in the SVG namespace. Will make the development of diagramming apps much easier as framework-agnostic libraries can emerge.

bennyschudel commented 1 month ago

Was somehow surprised that this is not implemented. For dynamic UI this would be a great boost.

Delnee commented 3 days ago

I am using the Lit library to build web components, and its a shame that large SVG elements cannot be decomposed into custom components, because this is isn't supported.

cscheffauer commented 1 day ago

+1