Fix issue with invalid document when script tag is whitelisted

closes #299