Improve JS extraction from HTML. Fixes gh-1942

When there were no lines before a <script> fragment and the previous fragment,
or if there was no previous fragment before a <script> fragment, the parser
would throw. This is corrected.

Thanks to @alex-macaulay for proposing this patch.

Closes #1942