use strict, setAttribute refactor

1. Add 'use strict'.
2. Refactor setAttribute to be more explicit, less repetitive, and
avoid unnecessary checks.