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

WebIDL: Support the HTMLConstructor attribute #621

Open
alexcrichton opened this issue Aug 3, 2018 · 12 comments
Open

WebIDL: Support the HTMLConstructor attribute #621

alexcrichton opened this issue Aug 3, 2018 · 12 comments
Labels
frontend:webidl Issues related to the WebIDL frontend to wasm-bindgen help wanted We could use some help fixing this issue! web-sys Issues related to the `web-sys` crate

Comments

@alexcrichton
Copy link
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.

@alexcrichton alexcrichton added help wanted We could use some help fixing this issue! frontend:webidl Issues related to the WebIDL frontend to wasm-bindgen web-sys Issues related to the `web-sys` crate Blocking Rust 2018 labels Aug 3, 2018
@fitzgen fitzgen mentioned this issue Aug 3, 2018
19 tasks
@ohanar
Copy link
Member

ohanar commented Aug 3, 2018

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

@alexcrichton
Copy link
Contributor Author

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
Copy link
Member

fitzgen commented Aug 3, 2018

I am confused

html-constructor

@fitzgen
Copy link
Member

fitzgen commented Aug 3, 2018

@alexcrichton
Copy link
Contributor Author

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

@fitzgen
Copy link
Member

fitzgen commented 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
Copy link
Contributor Author

Ok cool!

@Pauan
Copy link
Contributor

Pauan commented Aug 4, 2018

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
Copy link
Member

fitzgen commented Aug 4, 2018 via email

@Pauan
Copy link
Contributor

Pauan commented Aug 5, 2018

@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
Copy link
Contributor

Pauan commented Aug 5, 2018

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
Copy link
Member

fitzgen commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend:webidl Issues related to the WebIDL frontend to wasm-bindgen help wanted We could use some help fixing this issue! web-sys Issues related to the `web-sys` crate
Projects
None yet
Development

No branches or pull requests

4 participants