Only use bracket notation if tag is invalid.

Also centralize validity logic in xjs.js.