freeCodeCamp icon indicating copy to clipboard operation
freeCodeCamp copied to clipboard

Escaping HTML entities

Open naomi-lgbt opened this issue 1 year ago • 29 comments

Describe the Issue

We have a challenge that asks campers to replace symbols like & with the relevant HTML entity &. It's a neat little challenge, BUUUUUUT...

The in-app console we render sanitises the entities back to HTML. So with correct code, campers still see & instead of &.

https://imgur.com/a/xxjSSW2

Affected Page

https://www.freecodecamp.org/learn/full-stack-developer/lab-html-entitiy-converter/implement-an-html-entity-converter

Steps to Reproduce

  1. Go to https://www.freecodecamp.org/learn/full-stack-developer/lab-html-entitiy-converter/implement-an-html-entity-converter
  2. Enter the following code.
  3. Open browser console.
  4. See different results.
function convertHTML(str) {
  let newStr = "";
  for (let char of str) {
    if (char == "&") {
      newStr += "&";
    } else if (char == "<") {
      newStr += "&lt;";
    } else if (char == ">") {
      newStr += "&gt;";
    } else if (char == '"') {
      newStr += "&quot;";
    } else if (char == "'") {
      newStr += "&apos;";
    } else {
      newStr += char;
    }
  }
  return newStr;
}

console.log(convertHTML("Dolce & Gabbana"));

Expected behavior

The in-app console should render `Dolce & Gabanna

Screenshots

No response

System

Irrelevant

Additional context

https://www.reddit.com/r/FreeCodeCamp/comments/1ovgdgm/current_javascript_version_autoconvert_makes/

naomi-lgbt avatar Nov 12 '25 21:11 naomi-lgbt

I modified the supplied code to factor in the missing characters.

function convertHTML(str) {
  let newStr = "";
  for (let char of str) {
    if (char == "&") {
      newStr += "&amp;amp;";
    } else if (char == "<") {
      newStr += "&lt;lt;";
    } else if (char == ">") {
      newStr += "&gt;gt;";
    } else if (char == '"') {
      newStr += "&quot;quot;";
    } else if (char == "'") {
      newStr += "&apos;apos;";
    } else {
      newStr += char;
    }
  }
  return newStr;
}

console.log(convertHTML("Dolce & Gabbana")); // Dolce &amp; Gabbana

Behind the scenes the string appears as Dolce &amp;amp; Gabbana

Maybe mention in the user story to check the preview, and add a hint about how to refactor.

The test will need to change to check for the behind the scenes string.

NOTE: This is a console thing, it works in a JavaScript only environment.

The following does not work: 😦

      newStr += `<code>&amp;</code>`;

Here is another solution using the code supplied for the issue:

function convertHTML(str) {
  let newStr = "";
  for (let char of str) {
    if (char == "&") {
      newStr += "&\0amp;";
    } else if (char == "<") {
      newStr += "&\0lt;";
    } else if (char == ">") {
      newStr += "&\0gt;";
    } else if (char == '"') {
      newStr += "&\0quot;";
    } else if (char == "'") {
      newStr += "&\0apos;";
    } else {
      newStr += char;
    }
  }
  return newStr;
}

console.log(convertHTML("Dolce & Gabbana")); // Dolce &amp; Gabbana

NB: The Full Stack Developer Curriculum was recently split into separate certificates. Use the link supplied by @a2937 below to access the lab.

The correct characters render in the legacy JavaScript console. Perhaps use the older console for this lab.

Supravisor avatar Nov 13 '25 02:11 Supravisor

That direct link didn't work for me and I had to navigate to https://www.freecodecamp.org/learn/javascript-v9/lab-html-entitiy-converter/implement-an-html-entity-converter to access the lab.

a2937 avatar Nov 13 '25 03:11 a2937

The in-app console should render Dolce & Gabanna

I think you mean the in-app console should render Dolce &amp; Gabanna.

a2937 avatar Nov 13 '25 03:11 a2937

I don't think it's the sanitizer library that we're using that is causing the issue. I'm fairly certain that it escapes all text content. If anything it should be causing the opposite?

But could it be something like say the dangerouslySetInnerHTML attribute doing this?

Edit: I think I found the proof I was looking for: https://github.com/apostrophecms/sanitize-html/blob/86efc067a63515e08ecfc47f94d8bca0e3715030/test/test.js#L143-L145

a2937 avatar Nov 13 '25 03:11 a2937

@a2937

Are you planning to work on this? I see a draft PR linked to the issue. If so, then you can remove the triage label and assign the issue to yourself. 👍🏾

jdwilkin4 avatar Nov 17 '25 21:11 jdwilkin4

I was going to but I don't actually know the right way to solve this. My attempt at solving this only revealed the other half of the problem I was aware of.

@jdwilkin4

a2937 avatar Nov 18 '25 01:11 a2937

Is it possible to use the console from the legacy JavaScript cert?

Supravisor avatar Nov 18 '25 01:11 Supravisor

No but it might give me an idea on how to fix it.

@jdwilkin4 Jessica do you know where the code for the old console lives?

Edit: Or maybe you know other challenges where output.tsx is used and doesn't output plain text. That might be helpful.

a2937 avatar Nov 18 '25 17:11 a2937

Unfortunately, I don't know where the old console is. But you can try posting in the contributors chat and see if one of the other maintainers knows 👍🏾

jdwilkin4 avatar Nov 18 '25 21:11 jdwilkin4

Going to open this up again for contributors. There does not seem to be a PR in works.

Sembauke avatar Mar 13 '26 13:03 Sembauke

Should we preserve only the 5 HTML entities relevant to this lab (&amp;, &lt;, &gt;, &quot;, &apos;), or should we handle all HTML entities?

AdityaSingh-18 avatar Mar 16 '26 11:03 AdityaSingh-18

We discussed this a little on Anna's PR https://github.com/freeCodeCamp/freeCodeCamp/pull/63817#issuecomment-3576349740 and I'd like to surface that. Specifically:

Alternatively we could stop sending raw HTML to the client and, instead, send something that it can process without needing to resort to dangerouslySetInnerHTML. e.g. an array of { type: "code"|"text, value: "some text" }, so we could get react to conditionally render code elements.

This both addresses this issue and removes a use of dangerouslySetInnerHTML. Even if it's not particularly dangerous in this instance, it's best avoided.

ojeytonwilliams avatar Mar 16 '26 12:03 ojeytonwilliams

Bump

On Mon, Mar 16, 2026, 12:27 PM Oliver Eyton-Williams < @.***> wrote:

ojeytonwilliams left a comment (freeCodeCamp/freeCodeCamp#63788) https://github.com/freeCodeCamp/freeCodeCamp/issues/63788#issuecomment-4067298193

We discussed this a little on Anna's PR #63817 (comment) https://github.com/freeCodeCamp/freeCodeCamp/pull/63817#issuecomment-3576349740 and I'd like to surface that. Specifically:

Alternatively we could stop sending raw HTML to the client and, instead, send something that it can process without needing to resort to dangerouslySetInnerHTML. e.g. an array of { type: "code"|"text, value: "some text" }, so we could get react to conditionally render code elements.

This both addresses this issue and removes a use of dangerouslySetInnerHTML. Even if it's not particularly dangerous in this instance, it's best avoided.

— Reply to this email directly, view it on GitHub https://github.com/freeCodeCamp/freeCodeCamp/issues/63788#issuecomment-4067298193, or unsubscribe https://github.com/notifications/unsubscribe-auth/B7FYT7KNEK5XBGYV3ZPM3M34Q7XJXAVCNFSM6AAAAACL5ZETPSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DANRXGI4TQMJZGM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

jameswhitaker816-design avatar Mar 18 '26 10:03 jameswhitaker816-design

Hi, I would like to work on this issue.

AdithRavi avatar Mar 19 '26 11:03 AdithRavi

Hi, I'm a first-time contributor and would like to work on this issue. Can you please assign it to me?

harshitha-809 avatar Apr 03 '26 06:04 harshitha-809

Hi, I'm a first-time contributor and would like to work on this issue. Can you please assign it to me?

We typically do not assign issues. Instead, we accept the first pull request that comprehensively solves the issue.

Issues labeled with help wanted or first timers only are open for contributions.

Please make sure you read our guidelines for contributing. We prioritize contributors following the instructions in our guide. Join us in our chat room or the forum if you need help contributing - our community will be happy to assist you.

Jeevankumar-s avatar Apr 03 '26 06:04 Jeevankumar-s

"Hi, I'm reading through this issue to understand it better. Thank you for the detailed description!"

ishitGsigma avatar Apr 10 '26 10:04 ishitGsigma

when i click on link i get

Image

AyushK5ingh avatar Apr 24 '26 09:04 AyushK5ingh

The Full Stack certificate was split into separate certificates.

Try this link: https://www.freecodecamp.org/learn/javascript-v9/lab-html-entitiy-converter/implement-an-html-entity-converter

Supravisor avatar Apr 28 '26 00:04 Supravisor

can i work on this issue

rustom-yadav avatar May 12 '26 13:05 rustom-yadav