Repository navigation
feat: declare DOM event data and filters as JS snippets - #26845
Conversation
String event data and filters keep the expression as their key again, as before the snippets were added. So the browser sends their values under the expression, and the server does not translate the keys back. A declared snippet is keyed by "@" and a short hash of its function and captures, computed once when the snippet is created. Its settings are an object with the function id, the debounce settings, the side effect and the captures. A snippet whose captures are all plain values is shared through the constant pool. A snippet that captures, for example, an element is sent for that element only. Related to #25658
Flow sends three event expressions of its own: the always-true filter "1", which it adds for a debounce without a filter and when filtered and unfiltered listeners share an event type, and "event.preventDefault()" and "event.stopPropagation()". The browser compiled them from strings, so under a Content Security Policy without unsafe-eval the event was lost, and for "1" the server did not even log a warning. The client now runs the three directly, and the server no longer reports preventDefault() and stopPropagation() as string JavaScript. Related to #25658
Memory footprintCompared with the base commit 30455f2.
Scenarios
Measured by |
Type of change
How to test
To check the CSP side, run the
What changed in detailThe first commit is #25661 at
API changesNew:
Internal, but public:
Test coverage
|
|
Compared this with #25661, and I'd go ahead with this one: the public API is the same, string expressions keep their format from
|
vaadin-review-bot
left a comment
There was a problem hiding this comment.
Reviewed the changes — left 2 comments.
| Finding | |
|---|---|
The new setFilter overload makes setFilter(null) ambiguous, so existing callers stop compiling |
|
| 📐 | The existing setFilter/addEventData overloads are not updated to point at the new declared-JS variants |
Both setFilter versions now take null to clear the filter, so a nullable DomEventFilter variable can be passed. A plain setFilter(null) is ambiguous between the two, and the Javadoc of setFilter(String) says to cast it. The setFilter(String) Javadoc also points to the declared version for a Content Security Policy without unsafe-eval. Related to #25658
Declared event data and filters with plain captures are pooled, so each distinct captured value adds a constant pool entry that is never removed. The Javadoc and the wire protocol guide now say so. The string versions of addEventData now point to addEventData(DomEventData). Related to #25658
|
A right click fires no click event, so the listener filtered on the secondary button could never run. The listeners now use mousedown, and the test checks a left and a right click. Related to #25658
A second setFilter overload made setFilter(null) ambiguous, which broke existing code that clears a filter. Related to #25658
Explain that event data from the same method with different arguments sends separate values, and that reading the value with another instance gives null unless all arguments are plain values. Related to #25658
A snippet key with plain captures is computed once, when the snippet is created, but the captures are encoded each time they are sent. A captured AtomicInteger or AtomicLong could change in between, so two snippets could share a key while sending different values. Only the boxed primitive types, BigInteger and BigDecimal now count as plain. Related to #25658
Use the same wording for the shared function and the plain values in both classes, and explain that filters from the same method still check their own arguments. Related to #25658
getEventData(DomEventData) now says when another instance can read the value and when it gets null. The method Javadoc of addEventData and setFilterExpression no longer describes how the browser shares one function between snippets; the class Javadoc covers that. Related to #25658
|
|
…pets-simplified # Conflicts: # flow-server/src/test/java/com/vaadin/flow/js/JsDefinitionProxyTest.java
heruan
left a comment
There was a problem hiding this comment.
Three notes from a closer read, nothing blocking the API. One defect on the server side of declared filters, one question on captured elements, one small suggestion on the client.
The browser sends the value that a filter returned, which does not have to be a boolean, and also sends the values of filters that did not pass when another filter of the event passed. JsonNode.booleanValue() throws for anything but a boolean in Jackson 3, so such a value failed the event for every listener of its type on the element.
The missing function threw inside the DOM event handler, so the event was lost for every listener of its type on the element.
|
Document DomEventData and DomEventFilter as a Content Security Policy friendly alternative to the string-based addEventData() and setFilter() expressions, on the Element Events page. Documents vaadin/flow#26845 (`15a6f65954b851b1d3e1d81dfb6507a6b2c17185`).
|
Pull request created: #6606
|
|
Documentation Bot: Draft documentation pull request for this change: vaadin/docs#6606 Files updated:
It was written from the state of this pull request as you see it now. Please review it and mark it ready for review.
|
#2393) ## Summary With the current Flow `main`, `fireShortcut()` in UI unit tests triggers every shortcut listener, whatever key and modifiers are passed. Six `ShortcutsTest` cases fail ("not triggered on different key press / modifiers"). ## What changed - vaadin/flow#26845 changed how the server decides whether a DOM event filter passed. Flow used to call `booleanValue()` on the value sent for the filter. It now checks whether that value is truthy (`JavaScriptSemantics.isTrueish`). - The mock event data built by `fireShortcut()` returned itself for a filter key. That object node is always truthy, so every shortcut filter passed. - It now returns the match result as a `BooleanNode`. This works with both the old check and the new one, so it is safe with the currently published Flow snapshot too. ## Test summary - Covered by the existing shortcut tests: they fail against Flow `main` without this change and pass with it. - All unit tests of `vaadin-testbench-unit`, `-junit5` and `-junit6` pass against Flow `main` built locally. Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
|
This ticket/PR has been released with Vaadin 25.4.0-alpha6. |



Related to #25658
Related to #25661
new feature · flow-server, flow-client · apps that use a Content Security Policy without
unsafe-evaland need event data or event filtersBackground — event data and filters. A DOM listener can send extra values with the event and can ignore events that do not match a filter. Both are JavaScript expressions written as strings, which the browser compiles with
new Function. A Content Security Policy withoutunsafe-evalblocks this, so the event is lost.Event data and filters can now be declared in a
@JsDefinitioninterface, like other JS snippets. The build adds them to the bundle, so the browser compiles nothing. Flow's own always-true filter,preventDefault()andstopPropagation()also stop compiling strings.Risks:
DomEventData<T>andDomEventFilter,DomListenerRegistration.addEventData(DomEventData)andsetFilterExpression(DomEventFilter),DomEvent.getEventData(DomEventData).preventDefault()andstopPropagation()are no longer reported as string JavaScript in CSP mode, because the browser no longer compiles them.setFilterExpression(DomEventFilter), not asetFilteroverload as the "overload, not a new name" rule inCONVENTIONS.mdasks. An overload madesetFilter(null)ambiguous, which broke existing code that clears a filter.ElementListenerMap. One of the two should be closed.Context. The first commit is #25661 squashed. The second keeps the string expressions as keys, as on
main, so the server does not translate event data keys back.DomEventDataandDomEventFilter, declared with@JsExpressionand called witheventandelement.@and a hash of their function and captures.1,event.preventDefault()andevent.stopPropagation()without compiling them.EventDataCapturesIT, and a wire protocol section in the guidelines.