The PHP function Sanitizer::escapeId is used to generate URL fragments and HTML IDs. It has a special encoding which is MediaWiki specific. This part of the function is needed in JavaScript to generate URLs with fragments and HTML IDs from API responses.
Example:
The API request https://de.wikipedia.org/w/api.php?action=query&format=json&titles=Burgh%C3%B6hle+Dietfurt&redirects=true with the result
{ "batchcomplete": "", "query": { "redirects": [ { "from": "Burgh\u00f6hle Dietfurt", "to": "Ruine Dietfurt", "tofragment": "Burgh\u00f6hle Dietfurt" } ], "pages": { "922564": { "pageid": 922564, "ns": 0, "title": "Ruine Dietfurt" } } } }
has to converted to the URL https://de.wikipedia.org/wiki/Ruine_Dietfurt#Burgh.C3.B6hle_Dietfurt
The JavaScript function should also support wgExperimentalHtmlIds because the encoding in PHP depends on it.
The JavaScript function should not decode HTML entities like ü to ü because entities from wikitext are already decoded in the API responses.
The JavaScript function may optionally support adding a letter as first character.
This function can also used for fragment support in T103553 and T103715 and for ID generating in T87557.
Suggested name for the function: mw.util.escapeId. Alternative: mw.util.fragmentEncode or mw.util.fragmentencode.