You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried with custom elements that extend HTMLElement and HTMLDivElement and
couldn't get it to work. I could be doing it wrong, but in this scenario it
also seems like rust code wouldn't be calling the html element constructors
explicitly anyways.
If you can find a snippet that shows what is supposed to be going on, and
works, then we can definitely reopen!
classFooextendsHTMLDivElement{// Returns which attributes should be observed.// (This isn't needed if you don't care about attribute changes)staticgetobservedAttributes(){return["bar"];}// Called when the element is constructed (either via HTML or document.createElement).constructor(){super();console.log("Created!");}// Called when inserting into the DOM.// (This also includes when the DOM node is moved within the same Document)connectedCallback(){console.log("Connected!");}// Called when removing from the DOM.// (This also includes when the DOM node is moved within the same Document)disconnectedCallback(){console.log("Disconnected!");}// Called when moving to a different Document.adoptedCallback(){console.log("Adopted!");}// Called when one of the observedAttributes changes.attributeChangedCallback(name,oldValue,newValue){console.log("Attribute changed!",name,oldValue,newValue);}}// This assigns our class to an HTML name (in this case `my-foo`).// It is mandatory for the HTML name to include a dash (it cannot be called just `foo`).//// The "extends" is necessary because we are extending from HTMLDivElement.// If we weren't extending from a built-in HTML element then we wouldn't need "extends".customElements.define("my-foo",Foo,{extends: "div"});// Rather than using document.createElement, you can instead use <div is="my-foo"></div>// in HTML, which does the same thing.//// This special "is" stuff is needed because we're extending a built-in HTML element.// If we weren't extending a built-in HTML element then we would use one of these instead://// document.createElement("my-foo")// <my-foo></my-foo>constmy_foo=document.createElement("div",{is: "my-foo"});document.body.appendChild(my_foo);document.body.appendChild(my_foo);my_foo.setAttribute("bar","qux");
If it's still not working for you, try Chrome. MDN has this big disclaimer:
Note: Custom elements are supported by default in Chrome and Opera. Firefox is very close; they are currently available if you set the preferences dom.webcomponents.shadowdom.enabled and dom.webcomponents.customelements.enabled to true. Firefox's implementation is planned to be enabled by default in version 63/64. Safari so far supports only autonomous custom elements, and Edge is working on an implementation as well.
Activity
ohanar commentedon Aug 3, 2018
So the WebIDL spec doesn't specify the HTMLConstructor anywhere. Is there anything special about these constructors?
alexcrichton commentedon Aug 3, 2018
Perhaps not! It sounds like we may be able to treat it as a normal constructor perhaps? (I'm not too knowledgeable of what it is either!)
fitzgen commentedon Aug 3, 2018
I am confused
fitzgen commentedon Aug 3, 2018
https://html.spec.whatwg.org/multipage/dom.html#htmlconstructor
alexcrichton commentedon Aug 3, 2018
Interesting! Does that mean that there's nothing actually to do here?
fitzgen commentedon Aug 3, 2018
I think so. I find that series of steps somewhat impenetrable. I tried a bunch of things, and I couldn't get anything that wouldn't throw an error.
alexcrichton commentedon Aug 3, 2018
Ok cool!
Pauan commentedon Aug 4, 2018
I believe they just exist so that custom elements can work:
My understanding of the spec is that
new HTMLDivElement()
andHTMLDivElement()
should both throw errors.So indeed they cannot be created directly (only indirectly by creating a new
class
that extends from them).fitzgen commentedon Aug 4, 2018
Pauan commentedon Aug 5, 2018
@fitzgen
This is only relevant after wasm-bindgen gets the ability to create JS classes in Rust.
Pauan commentedon Aug 5, 2018
If it's still not working for you, try Chrome. MDN has this big disclaimer:
fitzgen commentedon Aug 6, 2018
Woops, yeah I was only trying in Firefox.
I think we can reopen this, but that it should not block Rust 2018 / Release Candidate.