Support @while control directive (#13808)

This change also refactors/fixes tracking of where to append
new nodes to keep the correct order.

Change-Id: Ic0c5ec7972e1651c093e2ea8301f459734cfbbdf