Skip to content
21 changes: 0 additions & 21 deletions inputfiles/overridingTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,6 @@
}
}
},
"GlobalEventHandlers": {
"properties": {
"property": {
"onerror": {
"overrideType": "OnErrorEventHandler"
},
"ontouchcancel": {
"optional": true
},
"ontouchend": {
"optional": true
},
"ontouchmove": {
"optional": true
},
"ontouchstart": {
"optional": true
}
}
}
},
"HTMLOrSVGElement": {
"properties": {
"property": {
Expand Down
7 changes: 7 additions & 0 deletions inputfiles/patches/eventhandlers.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface-mixin GlobalEventHandlers {
property ontouchcancel optional=#true
property ontouchend optional=#true
property ontouchmove optional=#true
property ontouchstart optional=#true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some context comment would be nice for touch events, which would be nicer as touch-events.kdl:

Touch event handlers are intentionally hidden in non-mobile web browsers.
See https://w3c.github.io/touch-events/#dfn-expose-legacy-touch-event-apis.

property onerror overrideType=OnErrorEventHandler
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can go events.kdl, as this should ultimately also be autogenerated rather than overridden.

}
13 changes: 11 additions & 2 deletions src/build/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,19 @@ function handleEvent(child: Node): Event {
* @param child The child node to handle.
*/
function handleProperty(child: Node): Partial<Property> {
return {
const result: Partial<Property> = {
name: child.values[0] as string,
exposed: child.properties?.exposed as string,
};

const props: (keyof Property)[] = ["exposed", "optional", "overrideType"];

props.forEach((prop) => {
const value = child.properties[prop];
if (value !== undefined) {
result[prop] = value as any;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have some type check, any should not be used.

Perhaps could use some helper:

import { parse, type Node, type Value } from "kdljs";

function optionalMember<const T>(prop: string, type: T, value?: Value) {
  if (value === undefined) {
    return {};
  }
  if (typeof value !== type) {
    throw new Error(`Expected type ${value} for ${prop}`);
  }
  return { [prop]: value as T extends "string" ? string : T extends "number" ? number : T extends "boolean" ? boolean : never };
}

const result = {
  name: child.values[0] as string,
  ...optionalMember("exposed", "string", child.properties?.exposed),
  ...optionalMember("optional", "boolean", child.properties?.optional),
  ...optionalMember("overrideType", "string", child.properties?.overrideType),
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated it for property and mixins

}
});
return result;
}

/**
Expand Down
Loading