  This paper investigates the gravitational trapping of HII regions predicted
by steady-state analysis using radiation hydrodynamical simulations. We present
idealised spherically symmetric radiation hydrodynamical simulations of the
early evolution of HII regions including the gravity of the central source. As
with analytic steady state solutions of spherically symmetric ionised Bondi
accretion flows, we find gravitationally trapped HII regions with accretion
through the ionisation front onto the source. We found that, for a constant
ionising luminosity, fluctuations in the ionisation front are unstable. This
instability only occurs in this spherically symmetric accretion geometry. In
the context of massive star formation, the ionising luminosity increases with
time as the source accretes mass. The maximum radius of the recurring HII
region increases on the accretion timescale until it reaches the sonic radius,
where the infall velocity equals the sound speed of the ionised gas, after
which it enters a pressure-driven expansion phase. This expansion prevents
accretion of gas through the ionisation front, the accretion rate onto the star
decreases to zero, and it stops growing from accretion. Because of the time
required for any significant change in stellar mass and luminosity through
accretion our simulations keep both mass and luminosity constant and follow the
evolution from trapped to expanding in a piecewise manner. Implications of this
evolution of HII regions include a continuation of accretion of material onto
forming stars for a period after the star starts to emit ionising radiation,
and an extension of the lifetime of ultracompact HII regions.
