Skip to content

fix(blaze): run event maps for views rendered into a shadow root - #539

Open
dupontbertrand wants to merge 1 commit into
meteor:release-3.1.0from
dupontbertrand:fix/event-maps-in-shadow-root
Open

dupontbertrand wants to merge 1 commit into
meteor:release-3.1.0from
dupontbertrand:fix/event-maps-in-shadow-root

Conversation

@dupontbertrand

@dupontbertrand dupontbertrand commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

How we found it

Meteor 3.6 shipped meteor create --pwa, a Blaze app that installs like a native app, and the offline package in meteor/meteor#14956 is adding offline data on top of it for 3.7. With that in place, the next question for Blaze 3.2 was: once the app is installed, do the browser's native APIs fit naturally with Blaze templates? So I took the index of Web APIs that MDN maintains (149 API groups: drag and drop, popover, view transitions, clipboard, Web Components, and so on) and exercised each of them from a Blaze PWA, in Chrome on desktop and on Android, with the native DOM backend and then with jQuery.

Most of them just work, and the reserves are documented patterns (Template.instance() after an await, Tracker.flush() inside a view transition). The Web Components entry is where something real showed up: rendering a template into a shadow root gives a view that renders but never hears a click. This pull request is that finding, with the two smaller gaps the review of the fix uncovered next to it ($() and selectors rooted at the shadow root). The second Web Components gap, customized built-in elements and their is attribute, is #540.

Context

A shadow root is the private DOM subtree that Web Components attach to a host element with host.attachShadow({ mode: 'open' }): the markup, styles and events of a custom element live inside it, isolated from the page around it. An app that builds a custom element with Blaze renders a template into that subtree:

class UserCard extends HTMLElement {
  connectedCallback() {
    this.view = Blaze.renderWithData(Template.userCard, { user: this.user }, this.attachShadow({ mode: 'open' }));
  }
}

In the DOM, every node carries a nodeType number that says what kind of node it is: 1 is an element (<div>), 3 a text node, 9 the document, 11 a document fragment. A ShadowRoot is a DocumentFragment, so its nodeType is 11, not 1. That single number is where this bug comes from.

The bug

Rendering into a shadow root works: the nodes appear and react to data. But no handler of the template's event map ever runs, on both DOM backends (native and jQuery), and nothing is logged.

Blaze.render records the shadow root as the view's parentElement. Before running a handler, Blaze._addEventMap (view.js) asks DOMRange#containsElement whether the event's target belongs to the view, and that method starts with Blaze._elementContains(this.parentElement, target) (domrange.js). This helper, unchanged since 2014, begins with if (a.nodeType !== 1) return false;: a parent that is not an element is considered to contain nothing. With a shadow root as parent, every event is rejected right there, before the handler, with no error.

Two more spots assumed the parent is an element:

  • DOMRange#$ (behind Template.instance().$(), find() and findAll()) threw "Can't use $ on an offscreen range" for any nodeType 11 parent, with a comment saying jQuery cannot run selectors on a fragment. That has not been true since jQuery 3, and the native backend uses querySelectorAll, which works on a shadow root too.
  • The native backend roots every delegation selector at the parent with :scope (so that 'div p' only matches a <p> inside a <div> of this view). When the query runs on a document fragment, :scope matches nothing, so even with the first check fixed no selector would ever match.

The workaround people have been using since blaze#195 (2016, "Blaze + Polymer") is to render into a <div> placed inside the shadow root rather than into the shadow root itself.

Change

  • Blaze._elementContains accepts a document fragment as parent (nodeType 11): compareDocumentPosition and contains, which it relies on, work on fragments.
  • DOMRange#$ accepts a shadow root. A bare document fragment, not attached to any host (an offscreen range), is still refused as before.
  • Native backend, scopeSelector: for a shadow root the selector is rooted with :host, the selector that designates the host element when evaluated inside a shadow tree, so '> .card' keeps meaning "a .card that is a direct child of the root". For a bare document fragment the selector is used unchanged; a selector starting with a combinator is not supported there, and the code comment says so.
  • jQuery backend: jQuery refuses to attach data, and therefore events, to a node whose nodeType is not 1 or 9 (its acceptData check), so $(shadowRoot).on(...) binds nothing, silently. When the parent is a fragment, delegateEvents and undelegateEvents now use the native delegation of the same file instead. The capturing path (non-bubbling events like mouseenter) already used addEventListener directly and needed no change.

One consequence to know with jQuery installed: in a view rendered into a shadow root, a bubbling event reaches the handler as a native Event (no originalEvent), while a non-bubbling one still reaches it as a jQuery.Event. Nothing fired before, so nothing regresses; it is documented here rather than papered over because the jQuery backend goes away in 4.0.

Test

event map works for a view rendered into a shadow root (spacebars-tests) renders a template into a shadow root and checks: a bubbling click through a descendant selector (.card .title) and through a child selector (> .card), a non-bubbling mouseenter (capture path), and findAll from the template instance. It runs on both backends in CI and is skipped where attachShadow is missing.

Blaze._elementContains returned false for any parent whose nodeType is
not 1, so DOMRange#containsElement rejected every event of a view rendered
straight into a ShadowRoot (a DocumentFragment, nodeType 11) and no event
map handler ever ran, on both DOM backends. DOMRange#$ refused the shadow
root too, so Template.instance().find() threw there.

Accept document fragments in both places (a bare, offscreen fragment is
still refused by $). On the native backend, :scope matches nothing when
the query runs on a fragment: the delegation selector is rooted with :host
for a shadow root and left as is for a bare fragment. jQuery binds nothing
on a fragment (acceptData refuses nodeType 11), so delegation goes through
the native path when the parent is a fragment.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant