Skip to content

Commit

Permalink
feat(wasm-api-dom): add boolean attrib support
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 29, 2022
1 parent 36d1857 commit 1d9c543
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
20 changes: 20 additions & 0 deletions packages/wasm-api-dom/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ export interface DOMImports extends WebAssembly.ModuleImports {
*/
_setNumericAttrib(elementID: number, nameAddr: number, val: number): void;

/**
* Sets (or removes) boolean attribute for given element. `nameAddr` is a
* zero-terminated char pointer (or standard Zig `[]u8` / `[]const u8`
* slices). If `state` is non-zero, the attrib will be created/ensured, if
* zero it will be removed.
*
* @param elementID
* @param nameAddr
* @param state
*/
_setBooleanAttrib(elementID: number, nameAddr: number, state: number): void;

/**
* Reads a numeric attribute value from DOM element and returns it as f64.
*
Expand All @@ -124,6 +136,14 @@ export interface DOMImports extends WebAssembly.ModuleImports {
*/
_getNumericAttrib(elementID: number, nameAddr: number): number;

/**
* Check if the DOM element has given attribute and returns 1 if so, else 0.
*
* @param elementID
* @param nameAddr
*/
_getBooleanAttrib(elementID: number, nameAddr: number): number;

/**
* Attaches a new DOM event listener for given event name to element (or
* window) given by `ctxID`. The `listenerID` is managed by the Zig
Expand Down
32 changes: 23 additions & 9 deletions packages/wasm-api-dom/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,14 @@ export class WasmDom implements IWasmAPI<DOMExports> {
opts: CreateElementOpts,
nestedParent?: number
) => {
let el: Element;
const tagName = opts.tag.deref();
const ns = opts.ns.deref();
if (ns) {
el = document.createElementNS(
NS_PREFIXES[ns] || ns,
tagName
);
} else {
el = document.createElement(tagName);
}
const el = ns
? document.createElementNS(
NS_PREFIXES[ns] || ns,
tagName
)
: document.createElement(tagName);
this.initElement(el, opts, nestedParent);
const id = this.elements.add(el);
if (opts.children.length > 0) {
Expand Down Expand Up @@ -161,6 +158,16 @@ export class WasmDom implements IWasmAPI<DOMExports> {
.get(elementID)
.setAttribute(this.parent.getString(name), String(val)),

_setBooleanAttrib: (
elementID: number,
name: number,
val: number
) => {
const el = this.elements.get(elementID);
const attr = this.parent.getString(name);
val ? el.setAttribute(attr, "") : el.removeAttribute(attr);
},

_getStringAttrib: (
elementID: number,
name: number,
Expand All @@ -185,6 +192,13 @@ export class WasmDom implements IWasmAPI<DOMExports> {
.getAttribute(this.parent.getString(name))
),

_getBooleanAttrib: (elementID: number, name: number) =>
this.elements
.get(elementID)
.getAttribute(this.parent.getString(name)) != null
? 1
: 0,

_addListener: (ctxID: number, name: number, listenerID: number) => {
const ctx = ctxID < 0 ? window : this.elements.get(ctxID);
const eventName = this.parent.getString(name);
Expand Down
14 changes: 14 additions & 0 deletions packages/wasm-api-dom/zig/dom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ pub fn getNumericAttrib(elementID: i32, name: []const u8) f64 {
return _getNumericAttrib(elementID, name.ptr);
}

pub extern "dom" fn _setBooleanAttrib(elementID: i32, name: [*]const u8, state: u8) void;

/// Returns numeric value of attrib for given name
pub fn setBooleanAttrib(elementID: i32, name: []const u8, state: bool) void {
return _setBooleanAttrib(elementID, name.ptr, if (state) 1 else 0);
}

pub extern "dom" fn _getBooleanAttrib(elementID: i32, name: [*]const u8) u8;

/// Returns boolean value of attrib for given name
pub fn getBooleanAttrib(elementID: i32, name: []const u8) bool {
return if (_getBooleanAttrib(elementID, name.ptr) != 0) true else false;
}

pub extern "dom" fn _setInnerHtml(elementID: i32, ptr: [*]const u8) void;

/// Sets the `.innerHTML` property of a DOM element to given string
Expand Down

0 comments on commit 1d9c543

Please sign in to comment.