Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug in the readme example (entityToHTML) #179

Open
Nasicus opened this issue Oct 6, 2020 · 4 comments
Open

Possible bug in the readme example (entityToHTML) #179

Nasicus opened this issue Oct 6, 2020 · 4 comments

Comments

@Nasicus
Copy link

Nasicus commented Oct 6, 2020

The readme has the following example:

  entityToHTML: (entity, originalText) => {
    if (entity.type === 'LINK') {
      return <a href={entity.data.url}>{originalText}</a>;
    }
    return originalText;
  }

This works fine, however the originalText appears to be already HTML encoded and therefore if it contains special characters like an ' it will be double encoded.

E.g. originalText = my&#x27;url will result in the HTML <a href="https://google.ch">my&amp;#x27;url</a>

As a workaround I changed the function to that:

    if (entity.type === 'LINK') {
      return <a href={entity.data.url} />;
    }

    return originalText;

Now the resulting HTML will be <a href="https://google.ch">my&#x27;url</a> (because of the way the getElementHTML function works).

Not sure if this workaround is correct and can be used like this but it appears to work. I guess if the getElementHTML will never change I can leave my workaround...

Also not sure if I'm doing something wrong or if this is an actual bug.

@karldanninger
Copy link

karldanninger commented May 21, 2021

Experienced this recently.

@Nasicus Your workaround works, thank you! Yet, this makes me a little uncomfortable since I'm unsure why this works the way it does.

Is there any way to not encode originalText supplied to entityToHtml? Perhaps that would defeat the purpose of the function since it's goal is to convert an entity to encoded valid HTML... 🤔

@hgezim
Copy link

hgezim commented Jun 1, 2021

@karldanninger

I looked into why this works and let me see if I understand it:

In entityToHTML, even when you return only <a href='...' />,

When that is converted to HTML, the above is only used to generate the tags (line 35

const entityHTML = getEntityHTML(entity, originalText);
).

In line 36 (

const elementHTML = getElementHTML(entityHTML, originalText);
), getElementHTML is called with originalText which actually ends up being used as the child node of a tag:

Xnip2021-05-31_21-28-15

@Nasicus
Copy link
Author

Nasicus commented Jun 1, 2021

@karldanninger @hgezim
thanks. We're using my "workaround" in production since bascially I wrote this post. No issues.

The only thing I'm a little afraid is that the suddenly change this in the future ;)

@karldanninger
Copy link

@Nasicus Haha! I have that exact same fear - but we've made a note of this issue in our repo. 👍

Thank you for the explanation @hgezim !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants