Escaping HTML entities
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
- Go to https://www.freecodecamp.org/learn/full-stack-developer/lab-html-entitiy-converter/implement-an-html-entity-converter
- Enter the following code.
- Open browser console.
- See different results.
function convertHTML(str) {
let newStr = "";
for (let char of str) {
if (char == "&") {
newStr += "&";
} else if (char == "<") {
newStr += "<";
} else if (char == ">") {
newStr += ">";
} else if (char == '"') {
newStr += """;
} else if (char == "'") {
newStr += "'";
} 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/
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;";
} 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")); // Dolce & Gabbana
Behind the scenes the string appears as Dolce &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>&</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 & 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.
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.
The in-app console should render
Dolce & Gabanna
I think you mean the in-app console should render Dolce & Gabanna.
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
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. 👍🏾
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
Is it possible to use the console from the legacy JavaScript cert?
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.
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 👍🏾
Going to open this up again for contributors. There does not seem to be a PR in works.
Should we preserve only the 5 HTML entities relevant to this lab (&, <, >, ", '), or should we handle all HTML entities?
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.
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: @.***>
Hi, I would like to work on this issue.
Hi, I'm a first-time contributor and would like to work on this issue. Can you please assign it to me?
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.
"Hi, I'm reading through this issue to understand it better. Thank you for the detailed description!"
when i click on link i get
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
can i work on this issue