If A fouls prior to the change of possession, regardless of the outcome of the play, B must decline the foul to keep the ball. Technically, B has the option of enforcing the foul and replaying the down with A in possession, but that's unlikely.
If A fouls after the change of possession, and B scores a TD on the play, B has the option of enforcing the foul on the try or subsequent kickoff (8-2-3).
If B does not score, I believe the basic spot is the end of the (related) run and A's foul will be enforced from there. However, 10-4-6b only covers cases where the foul occurs beyond the end of the run. If A grabs B's facemask, but then B runs for 10 more yards before being tackled, I can no longer point to a specific item in rule 10 that covers this scenario.