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
The main module's default export is a class which you can construct with a few options:
110
110
111
-
-`implSuffix`: a suffix used, if any, to find files within the source directory based on the IDL file name.
112
-
-`suppressErrors`: set to true to suppress errors during generation.
111
+
-`implSuffix`: a suffix used, if any, to find files within the source directory based on the IDL file name
112
+
-`suppressErrors`: set to true to suppress errors during generation
113
+
-`processCEReactions` and `processHTMLConstructor`: see below
113
114
114
115
The `addSource()` method can then be called multiple times to add directories containing `.webidl` IDL files and `.js` implementation class files.
115
116
@@ -121,6 +122,66 @@ The transformer will also generate a file named `utils.js` inside the wrapper cl
121
122
122
123
Note that webidl2js works best when there is a single transformer instance that knows about as many files as possible. This allows it to resolve type references, e.g. when one operation has an argument type referring to some other interface.
123
124
125
+
### `[CEReactions]` and `[HTMLConstructor]`
126
+
127
+
By default webidl2js ignores HTML Standard-defined extended attributes [`[CEReactions]`](https://html.spec.whatwg.org/multipage/custom-elements.html#cereactions) and [`[HTMLConstructor]`](https://html.spec.whatwg.org/multipage/dom.html#htmlconstructor), since they require detailed knowledge of the host environment to implement correctly. The `processCEReactions` and `processHTMLConstructor` hooks provide a way to customize the generation of the wrapper class files when these extended attributes are present.
128
+
129
+
Both hooks have the signature `(code) => replacementCode`, where:
130
+
131
+
-`code` is the code generated by webidl2js normally, for calling into the impl class.
132
+
133
+
-`replacementCode` is the new code that will be output in place of `code` in the wrapper class.
134
+
135
+
If either hook is omitted, then the code will not be replaced, i.e. the default is equivalent to `(code) => code`.
136
+
137
+
Both hooks also have some utility methods that are accessible via `this`:
138
+
139
+
-`addImport(relPath, [importedIdentifier])` utility to require external modules from the generated interface. This method accepts 2 parameters: `relPath` the relative path from the generated interface file, and an optional `importedIdentifier` the identifier to import. This method returns the local identifier from the imported path.
140
+
141
+
The following variables are available in the scope of the replacement code:
142
+
143
+
-`globalObject` (object) is the global object associated with the interface
144
+
145
+
-`interfaceName` (string) is the name of the interface
146
+
147
+
An example of code that uses these hooks is as follows:
148
+
149
+
```js
150
+
"use strict";
151
+
constWebIDL2JS=require("webidl2js");
152
+
153
+
consttransformer=newWebIDL2JS({
154
+
implSuffix:"-impl",
155
+
processCEReactions(code) {
156
+
// Add `require("../ce-reactions")` to generated file.
The example above showed a simplified generated wrapper file with only three exports: `create`, `is`, and `interface`. In reality the generated wrapper file will contain more functionality, documented here. This functionality is different between generated wrapper files for interfaces and for dictionaries.
@@ -338,6 +399,11 @@ webidl2js is implementing an ever-growing subset of the Web IDL specification. S
338
399
-`[Unforgeable]`
339
400
-`[Unscopable]`
340
401
402
+
Supported Web IDL extensions defined in HTML:
403
+
404
+
-`[CEReactions]` - behavior can be defined via the `processCEReactions` hook
405
+
-`[HTMLConstructor]` - behavior can be defined via the `processHTMLConstructor` hook
406
+
341
407
Notable missing features include:
342
408
343
409
- Namespaces
@@ -366,7 +432,7 @@ By default the attribute passed to `this.getAttribute` and `this.setAttribute` w
366
432
367
433
Note that only the basics of the reflect algorithm are implemented so far: `boolean`, `DOMString`, `long`, and `unsigned long`, with no parametrizations.
368
434
369
-
In the future we may move this extended attribute out into some sort of plugin, since it is more related to HTML than to Web IDL.
435
+
In the future we may change this extended attribute to be handled by the caller, similar to `[CEReactions]` and `[HTMLConstructor]`, since it is more related to HTML than to Web IDL.
0 commit comments