Redo of #886: Page charset fix

I finished what @ptone started on #886, added a couple of tests, and resolved some merge conflicts.

If this get merged, pull request #483 is unblocked and free to go.