Skip to content

WebIDL: Support the HTMLConstructor attribute #621

Open
@alexcrichton

Description

@alexcrichton
Contributor

Right now we're not processing it which means a lot of types don't have constructors!

There may be some assorted constructor attributes that also need special handling as well, will require some investigation.

Activity

ohanar

ohanar commented on Aug 3, 2018

@ohanar
Member

So the WebIDL spec doesn't specify the HTMLConstructor anywhere. Is there anything special about these constructors?

alexcrichton

alexcrichton commented on Aug 3, 2018

@alexcrichton
ContributorAuthor

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

fitzgen commented on Aug 3, 2018

@fitzgen
Member

I am confused

html-constructor

alexcrichton

alexcrichton commented on Aug 3, 2018

@alexcrichton
ContributorAuthor

Interesting! Does that mean that there's nothing actually to do here?

fitzgen

fitzgen commented on Aug 3, 2018

@fitzgen
Member

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

alexcrichton commented on Aug 3, 2018

@alexcrichton
ContributorAuthor

Ok cool!

Pauan

Pauan commented on Aug 4, 2018

@Pauan
Contributor

I believe they just exist so that custom elements can work:

class Foo extends HTMLDivElement {
    ...
}

My understanding of the spec is that new HTMLDivElement() and HTMLDivElement() should both throw errors.

So indeed they cannot be created directly (only indirectly by creating a new class that extends from them).

fitzgen

fitzgen commented on Aug 4, 2018

@fitzgen
Member
Pauan

Pauan commented on Aug 5, 2018

@Pauan
Contributor

@fitzgen

class Foo extends HTMLDivElement {
    // Returns which attributes should be observed.
    // (This isn't needed if you don't care about attribute changes)
    static get observedAttributes() {
        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>
const my_foo = document.createElement("div", { is: "my-foo" });

document.body.appendChild(my_foo);
document.body.appendChild(my_foo);

my_foo.setAttribute("bar", "qux");

This is only relevant after wasm-bindgen gets the ability to create JS classes in Rust.

Pauan

Pauan commented on Aug 5, 2018

@Pauan
Contributor

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.

fitzgen

fitzgen commented on Aug 6, 2018

@fitzgen
Member

Woops, yeah I was only trying in Firefox.

I think we can reopen this, but that it should not block Rust 2018 / Release Candidate.

reopened this on Aug 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    frontend:webidlIssues related to the WebIDL frontend to wasm-bindgenhelp wantedWe could use some help fixing this issue!web-sysIssues related to the `web-sys` crate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @alexcrichton@fitzgen@Pauan@ohanar

        Issue actions

          WebIDL: Support the HTMLConstructor attribute · Issue #621 · rustwasm/wasm-bindgen