Try to reduce the number of promises we need to create for flow state management.
Escape namespace qualifiers when computing CSS selectors.

Fixes issue 5217.