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.

This patch re-organizes some of the CLI tests, and corrects behaviour for some
of the `extract` tests.

Closes #1942