npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webidl-class-generator

v1.6.2

Published

Generates classes from WebIDL plus implementation code

Downloads

5

Readme

Generate JavaScript Classes from WebIDL Interfaces

The goal of this project is to take as input

// foo.idl
interface Foo : Bar {
    [Reflect] attribute unsigned long x;
    readonly attribute long y;
    attribute DOMString z;
    boolean method(DOMString arg);

    constant unsigned short A_CONSTANT = 42;
}

plus

// foo-impl.js
export default class FooImpl {
    get y() { return Math.random() * 1000; }
    get z() { return this._z; }
    set z(v) { this._z = v; }
    method(arg) { return arg.toLowerCase(); }
}

and produce something like

// foo.js
import reflector from "webidl-html-reflector";
import conversions from "webidl-conversions";
import Impl from "./foo-impl";

export default class Foo extends Bar {
    get x() {
        return reflector["unsigned long"].get(this, "x");
    }
    set x(v) {
        v = conversions["unsigned long"](v);
        reflector["unsigned long"].set(this, "x", v);
    }

    get y() {
        const implGetter = Object.getOwnPropertyDescriptor(Impl.prototype, "y").get;
        const implResult = implGetter.call(this);
        return conversions["long"](implResult);
    }

    get z() {
        const implGetter = Object.getOwnPropertyDescriptor(Impl.prototype, "z").get;
        const implResult = implGetter.call(this);
        return conversions["DOMString"](implResult);
    }
    set z(v) {
        v = conversions["DOMString"](v);
        const implSetter = Object.getOwnPropertyDescriptor(Impl.prototype, "z").set;
        implSetter.call(this, v);
    }

    method(arg) {
        arg = conversions["DOMString"](arg);
        const implMethod = Impl.prototype.method;
        const implResult = implMethod.call(this, arg);
        return conversions["boolean"](implResult);
    }
}

Object.defineProperty(Foo, "A_CONSTANT", { value: 42, enumerable: true });
Object.defineProperty(Foo.prototype, "A_CONSTANT", { value: 42, enumerable: true });

window.Foo = Foo;

API

This package's main module's default export is a function that takes as input a WebIDL string and the implementation module name, and returns a string of JavaScript. It will parse the WebIDL, making sure that it contains a single (non-partial) interface, and then build up the resulting JavaScript. Any unsupported WebIDL features—which is most of them, right now—will generally be ignored. An example:

const fs = require("fs");
import generate from "webidl-class-generator";

const idl = fs.readFileSync("html-hr-element.idl", { encoding: "utf-8" });
const js = generate(idl, "./html-hr-element-impl.js");

fs.writeFileSync("html-hr-element.js", js);

Nonstandard Extended Attributes

A couple of non-standard extended attributes are allowed which are not part of the WebIDL specification.

[Reflect]

The [Reflect] extended attribute is implemented to call this.getAttribute or this.setAttribute and process the input our output using webidl-html-reflector, on both setting and getting. If [Reflect] is specified, the implementation class will not be consulted for the getter or setter logic.

By default the attribute passed to this.getAttribute and this.setAttribute will be the same as the name of the property being reflected. You can use the form [Reflect=custom] or [Reflect=custom_with_dashes] to change that to be "custom" or "custom-with-dashes", respectively.

[NoConversion]

The [NoConversion] extended attribute will cause any type conversions to be omitted from a getter or setter. This is mostly useful when the implementation class already does the type conversion, e.g. if implementing URLUtils it is possible that the techniques using by the implementation class will already produce USVStrings, and thus it would be undesirable to convert them all over again.

[CustomElementCallbacks]

The [CustomElementCallbacks] extended attribute can be applied to an interface to denote that it should copy over any of the custom element callbacks from the implementation class to the generated class.

Status

We only support a subset of WebIDL features; they are being added on an as-needed basis for HTML as Custom Elements. Check out the test cases for a sampling of what's supported, and the issues for upcoming ones that need work.