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

custom name of a customized built-in element is unreachable #4043

Open
gullerya opened this issue Sep 20, 2018 · 5 comments
Open

custom name of a customized built-in element is unreachable #4043

gullerya opened this issue Sep 20, 2018 · 5 comments
Labels
addition/proposal New features or enhancements topic: custom elements Relates to custom elements (as defined in DOM and HTML)

Comments

@gullerya
Copy link

gullerya commented Sep 20, 2018

Use-case:
Framework working on DOM elements needs to process undefined custom elements only when defined.
For the regular custom element it is simple:

if (!element.matches(':defined') {
    customElements.whenDefined(element.localName).then(() => { ... do something ... });
}

But for customized built-in element this is not possible, since element.localName is just a standard DOM element's name and there is no way right now to reach the extended name provided in is property:

let element  = document.createElement('input', { is: 'custom-input' });
//   value of 'is' goes into internals and 'custom-input' becomes not reachable anymore

BTW, when the same element is being added as below, is handled as an attribute and is reachable:

someElement.innerHTML += '<input is="custom-input"/>';
someElement.firstChild.attributes.length === 1;                     // true
someElement.firstChild.getAttribute('is') === 'custom-input';       // true

Proposal:

  • when customized built-in element is created programmatively, preserve is value as an attribute in a symmetric to HTMLish syntax. As proposed by someone (see refs below) it should probably be readonly IDL attribute
  • unify behavior of is to be the same either provided from createElement JS API or coming from HTML's attribute

Appendix:

@annevk annevk added addition/proposal New features or enhancements topic: custom elements Relates to custom elements (as defined in DOM and HTML) labels Sep 20, 2018
@annevk
Copy link
Member

annevk commented Sep 20, 2018

I proposed this in #3402 (comment) but we didn't have a concrete use case then.

cc @whatwg/components

@gullerya
Copy link
Author

Read through the thread, having is getter would give an answer, from my POV,
Thanks.

@mfreed7
Copy link
Contributor

mfreed7 commented Nov 5, 2020

I just found this issue and asymmetry with the "is" content attribute. It causes issues when trying to use selectors. See this simple example (taken from actual production code) - the desire is to be able to style (from light DOM) elements of the customized type only:

<style>
p[is="my-p"] { background-color: green; }
p:not([is="my-p"]) { background-color: red; }
</style>

<p is="my-p">This retains the "is" attribute</p>

<script>
customElements.define('my-p',class extends HTMLParagraphElement {}, {extends: 'p'});
const foo = document.createElement('p', {is:'my-p'});
foo.appendChild(document.createTextNode('This does NOT retain the "is" attribute'));
document.body.appendChild(foo);
</script>

Is there a recommended better way to do this? It can obviously be overcome pretty easily like this:

const foo = document.createElement('p', {is:'my-p'});
foo.setAttribute('is', 'my-p');

but that seems very hacky, given the declarative version doesn't need this, and the imperative version serializes as <p is="my-p">.

@annevk
Copy link
Member

annevk commented Nov 6, 2020

There's not I think, but with Safari not willing to add is I'm not sure how comfortable I am with further entrenching it.

@gullerya
Copy link
Author

gullerya commented Nov 6, 2020

I must say that since that time as till now I've completely abandoned support for a customized built-ins due to both, a seemingly not-yet-well-baked spec as well as quite a strong objection of WebKit guys to provide that part.
So in essence - same as @annevk I'd refrain from any investment around this as of now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements topic: custom elements Relates to custom elements (as defined in DOM and HTML)
Development

No branches or pull requests

3 participants