The behaviour of alt text in image links is confusing

Writing a CommonMark parser, and I am struggling to figure out why the first line’s alt text is not also “foo”.

![[[foo](uri1)](uri2)](uri3)
![![[foo](uri1)](uri2)](uri3)

Is this a bug, or some kind of subtle interaction I’m missing? Especially since nested links and images behave the same if you remove one layer of nesting:

![[foo](uri1)](uri3)
![![foo](uri1)](uri3)
<p><img src="uri3" alt="foo" />
<img src="uri3" alt="foo" /></p>