Skip to content

Commit

Permalink
fix: Decode HTML entities in the test output (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
tchaffee authored and raisedadead committed Sep 27, 2018
1 parent caf85cc commit 8de3a4c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/learn/src/templates/Challenges/classic/Show.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {

import './classic.css';

import decodeHTMLEntities from '../../../../utils/decodeHTMLEntities';

const mapStateToProps = createSelector(
challengeFilesSelector,
challengeTestsSelector,
Expand Down Expand Up @@ -200,7 +202,7 @@ class ShowClassic extends PureComponent {
* Your test output will go here.
*/
`}
output={output}
output={decodeHTMLEntities(output)}
/>
</ReflexElement>
) : null}
Expand Down
27 changes: 27 additions & 0 deletions packages/learn/utils/decodeHTMLEntities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Converts HTML entity codes in a string to the characters they represent.
*
* Example:
* `decodeHTMLEntities('Beets &amp; carrots');`
* will return "Beets & carrots".
*
* The regex makes sure we only replace the HTML entities in the string.
* For example, the regex would match "&lt;" as well as "&#58;".
* The decoding works by setting the innerHTML of a dummy element and then
* retrieving the innerText. Per the spec, innerText is a property that
* represents the "rendered" text content of an element.
*
* See:
* https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText
* https://developer.mozilla.org/en-US/docs/Glossary/Entity
*
*/
const decodeHTMLEntities = str => {
const el = document.createElement('div');
return str.replace(/\&[#0-9a-z]+;/gi, enc => {
el.innerHTML = enc;
return el.innerText;
});
};

export default decodeHTMLEntities;

0 comments on commit 8de3a4c

Please sign in to comment.