Refactor selector handling (first step towards #11685)

Support for @extends is limited in this version (see
Selector.replace()).

The parser accepts !optional but currently it has no effect.

Change-Id: Ie6c8076d5bf715c7f5d14abe75ff2f3f4665b983