From a0ddffd409610ce1bb823fb8ac112754459f42cf Mon Sep 17 00:00:00 2001 From: Francois Daoust Date: Fri, 19 Apr 2024 18:23:06 +0200 Subject: [PATCH] Events post-processor: consolidate events that "babble" It seems possible to end up with events that bubble for some target interfaces but don't for other target interfaces, see discussion in: https://github.com/w3c/webref/issues/1212 The post-processor already knew how to assign a bubbling flag per target interface because we make a distinction between interfaces that are in a bubbling tree and interfaces that are not. However, the post-processor did not know how to consolidate an entry that appears duplicated in the events extract because the underlying event bubbles in one case but not in the other. This update makes it able to consolidate these events (which I call "babbling" events because, hey, it's Friday evening). We still don't have post-processing tests but that seems to work fine on Webref data. --- src/postprocessing/events.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/postprocessing/events.js b/src/postprocessing/events.js index ab41f3e9..b69363ef 100644 --- a/src/postprocessing/events.js +++ b/src/postprocessing/events.js @@ -65,8 +65,14 @@ module.exports = { }) .filter(event => !!event); + // Before we clean and sort the result, we'll consolidate events that + // don't always bubble. We'll call them... "babbling" events. Such events + // should remain exceptions to the rule, and will likely be artificially + // created through some patching mechanism (in Webref typically) because + // the events extraction logic does not (yet?) support this scenario. return events .filter(event => !eventsToDrop.includes(event)) + .filter(event => consolidateBabblingEvent(event, events)) .map(event => { cleanTargetInterfaces(event, parsedInterfaces); delete event.spec; @@ -223,3 +229,31 @@ function extendEvent(event, events) { { spec: event.spec.series.shortname }, event.src?.href ? { href: event.src?.href } : {})); } + + +/** + * Consolidate events that got duplicated in the extract because they bubble + * or don't bubble depending on the target interface. + * + * We'll say that these events "babble" because they don't seem to know whether + * they bubble or not. + */ +function consolidateBabblingEvent(event, events) { + if (event.mergedIntoAnotherEvent) { + return null; + } + const newTargets = events + .filter(e => + e !== event && !e.isExtension && !e.mergedIntoAnotherEvent && + e.href && e.href === event.href && e.cancelable === event.cancelable) + .map(e => { + // Flag the event as merged so that we can filter it out afterwards + e.mergedIntoAnotherEvent = true; + return e.targets; + }) + .flat(); + if (newTargets.length > 0) { + event.targets = (event.targets || []).concat(newTargets); + } + return event; +}