If you care about edge cases, section anchor generation is rather complicated: anchors can be postfixed with an index when there are multiple identical titles, and HTML, templates and parser tags are handled differently for display and for anchor generation. (Yes, these can and do appear in titles. E.g. people sometimes put <math> tags in there, or italicize a word.)