MDL-46688 weblib: improve the highlight function.

Thanks to Joseph Baxter for suggesting some of the test cases.