Skip to content

Commit 231d957

Browse files
chancancodebtecu
authored andcommitted
[BIGFIX release] Fix <LinkTo> with nested children
During bubbling, `event.target` may point to a child element whereas `event.currentTarget` always points to the element where the handler was attached, which is what we want here. Reported in a comment on emberjs#19546, though this may be a distinct issue from the original report as it was reported as a default-cancelling parent element interfering with the nested `<LinkTo>`, and this is the other way around.
1 parent 81f7fcc commit 231d957

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

packages/@ember/-internals/glimmer/lib/components/link-to.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class LinkTo extends InternalComponent implements DeprecatingInternalComponent {
138138
return;
139139
}
140140

141-
let element = event.target;
141+
let element = event.currentTarget;
142142
assert('[BUG] must be an <a> element', element instanceof HTMLAnchorElement);
143143

144144
let isSelf = element.target === '' || element.target === '_self';

packages/@ember/-internals/glimmer/tests/integration/components/link-to/routing-angle-test.js

+41
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,47 @@ moduleFor(
9292
);
9393
}
9494

95+
async ['@test [GH#19546] it navigates into the named route when containing other elements'](
96+
assert
97+
) {
98+
this.addTemplate(
99+
'about',
100+
`
101+
<h3 class="about">About</h3>
102+
<LinkTo @route='index' id='home-link'><span id='inside'>Home</span></LinkTo>
103+
<LinkTo @route='about' id='self-link'>Self</LinkTo>
104+
`
105+
);
106+
107+
await this.visit('/about');
108+
109+
assert.equal(this.$('h3.about').length, 1, 'The about template was rendered');
110+
assert.equal(
111+
this.$('#self-link.active').length,
112+
1,
113+
'The self-link was rendered with active class'
114+
);
115+
assert.equal(
116+
this.$('#home-link:not(.active)').length,
117+
1,
118+
'The other link was rendered without active class'
119+
);
120+
121+
await this.click('#inside');
122+
123+
assert.equal(this.$('h3.home').length, 1, 'The home template was rendered');
124+
assert.equal(
125+
this.$('#self-link.active').length,
126+
1,
127+
'The self-link was rendered with active class'
128+
);
129+
assert.equal(
130+
this.$('#about-link:not(.active)').length,
131+
1,
132+
'The other link was rendered without active class'
133+
);
134+
}
135+
95136
async [`@test [DEPRECATED] it doesn't add an href when the tagName isn't 'a'`](assert) {
96137
this.addTemplate(
97138
'index',

packages/@ember/-internals/glimmer/tests/integration/components/link-to/routing-curly-test.js

+41
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,47 @@ moduleFor(
9292
);
9393
}
9494

95+
async ['@test [GH#19546] it navigates into the named route when containing other elements'](
96+
assert
97+
) {
98+
this.addTemplate(
99+
'about',
100+
`
101+
<h3 class="about">About</h3>
102+
<div id="home-link">{{#link-to route='index'}}<span id='inside'>Home</span>{{/link-to}}</div>
103+
<div id="self-link">{{#link-to route='about'}}Self{{/link-to}}</div>
104+
`
105+
);
106+
107+
await this.visit('/about');
108+
109+
assert.equal(this.$('h3.about').length, 1, 'The about template was rendered');
110+
assert.equal(
111+
this.$('#self-link > a.active').length,
112+
1,
113+
'The self-link was rendered with active class'
114+
);
115+
assert.equal(
116+
this.$('#home-link > a:not(.active)').length,
117+
1,
118+
'The other link was rendered without active class'
119+
);
120+
121+
await this.click('#inside');
122+
123+
assert.equal(this.$('h3.home').length, 1, 'The home template was rendered');
124+
assert.equal(
125+
this.$('#self-link > a.active').length,
126+
1,
127+
'The self-link was rendered with active class'
128+
);
129+
assert.equal(
130+
this.$('#about-link > a:not(.active)').length,
131+
1,
132+
'The other link was rendered without active class'
133+
);
134+
}
135+
95136
async [`@test [DEPRECATED] it doesn't add an href when the tagName isn't 'a'`](assert) {
96137
this.addTemplate(
97138
'index',

0 commit comments

Comments
 (0)