Repository navigation
fix(blaze): run event maps for views rendered into a shadow root - #539
Open
dupontbertrand wants to merge 1 commit into
Open
dupontbertrand wants to merge 1 commit into
dupontbertrand wants to merge 1 commit into
Conversation
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 was referenced Nov 12, 2026
Open
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
How we found it
Meteor 3.6 shipped
meteor create --pwa, a Blaze app that installs like a native app, and theofflinepackage 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 anawait,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 theirisattribute, 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:In the DOM, every node carries a
nodeTypenumber that says what kind of node it is:1is an element (<div>),3a text node,9the document,11a document fragment. AShadowRootis aDocumentFragment, so itsnodeTypeis11, not1. 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.renderrecords the shadow root as the view'sparentElement. Before running a handler,Blaze._addEventMap(view.js) asksDOMRange#containsElementwhether the event's target belongs to the view, and that method starts withBlaze._elementContains(this.parentElement, target)(domrange.js). This helper, unchanged since 2014, begins withif (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#$(behindTemplate.instance().$(),find()andfindAll()) threw "Can't use $ on an offscreen range" for anynodeType11 parent, with a comment saying jQuery cannot run selectors on a fragment. That has not been true since jQuery 3, and the native backend usesquerySelectorAll, which works on a shadow root too.:scope(so that'div p'only matches a<p>inside a<div>of this view). When the query runs on a document fragment,:scopematches 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._elementContainsaccepts a document fragment as parent (nodeType11):compareDocumentPositionandcontains, 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.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.cardthat 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.nodeTypeis not 1 or 9 (itsacceptDatacheck), so$(shadowRoot).on(...)binds nothing, silently. When the parent is a fragment,delegateEventsandundelegateEventsnow use the native delegation of the same file instead. The capturing path (non-bubbling events likemouseenter) already usedaddEventListenerdirectly 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(nooriginalEvent), while a non-bubbling one still reaches it as ajQuery.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 bubblingclickthrough a descendant selector (.card .title) and through a child selector (> .card), a non-bubblingmouseenter(capture path), andfindAllfrom the template instance. It runs on both backends in CI and is skipped whereattachShadowis missing.