Skip to content
This repository has been archived by the owner on Aug 14, 2021. It is now read-only.

Use HTML-escaping when displaying member names. #58

Closed

Conversation

glacambre
Copy link

This fixes a bug where objects members with names that look like HTML
tags wouldn't properly be rendered.

The corresponding issue is
TypeStrong/typedoc#780.

I expect tests in https://github.com/TypeStrong/typedoc to need to be updated. Let me know if the changes in this PR seem reasonable to you, if they are I'll open a separate PR in the typedoc repository in order to update the tests there.

If you would rather not change the template, what other approach should I use? I briefly considered HTML-escaping names in src/lib/converter/nodes/variable.ts but this didn't seem like a good idea to me.

This fixes a bug where objects members with names that look like HTML
tags wouldn't properly be rendered.

The corresponding issue is
TypeStrong/typedoc#780.
@cmcaine
Copy link

cmcaine commented Aug 2, 2018

How about just getting the wbr function to escape its input and tell handlebars not to escape its output?

This goes in the main typedoc repo:

/**
 * Insert word break tags ``<wbr>`` into the given string.
 *
 * Breaks the given string at ``_``, ``-`` and captial letters.
 *
 * @param str  The string that should be split.
 * @return     The original string containing ``<wbr>`` tags where possible.
 */
function wbr(options) {
    let str = typeof options === 'string' ? options : options.fn(this);
    str = Handlebars.escapeExpression(str)

    str = str.replace(/([^_\-][_\-])([^_\-])/g, (m, a, b) => a + '<wbr>' + b);
    str = str.replace(/([^A-Z])([A-Z][^A-Z])/g, (m, a, b) => a + '<wbr>' + b);

    return new Handlebars.SafeString(str);
}

Example manual run:

Handlebars.registerHelper('wbr', wbr)
template = Handlebars.compile(`<code>{{wbr name}}</code>`)
template({name: "<C-a>foobar"}) // correctly escaped

And you can then use wbr with double rather than triple braces, though it should do exactly the same regardless of which you use.

@glacambre
Copy link
Author

Could be a good idea too. I'm not sure having <wbr> is useful for member names (in my opinion you should try to have it all on the same line) but it probably was put there for a reason.

@aciccarello, @sebastian-lenz, do you have a preference ?

Gerrit0 added a commit to TypeStrong/typedoc that referenced this pull request Oct 27, 2019
@Gerrit0
Copy link
Contributor

Gerrit0 commented Oct 27, 2019

cmcaine's suggestion is a better solution. The changes here catch some of the places we output member names, but it is also used in several other places that weren't updated.

The wbr tag doesn't make browsers split names across lines if they will fit on the same line, but it does make it possible for us to tell the browser where to split the name if it is too long for a single line.

I've pulled cmcaine's solution into the main repo with TypeStrong/typedoc@5355abc. It will be available in the next release.

@Gerrit0 Gerrit0 closed this Oct 27, 2019
@glacambre glacambre deleted the fix_wierd_member_names branch October 27, 2019 20:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants