Skip to content

Commit dead69c

Browse files
committed
feat(core utils): Add escape_html and unescape_html function to replace/unreplace html entity characters.
1 parent 71391db commit dead69c

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/core/utils.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,49 @@ const localized_isodate = (date) => {
570570
return `${year}-${month}-${day}`;
571571
};
572572

573+
/**
574+
* Replace HTML reserved characters with html entities to add HTML for user
575+
* editing to e.g. a textarea or a contenteditable.
576+
*
577+
* See: https://developer.mozilla.org/en-US/docs/Glossary/Entity#reserved_characters
578+
*
579+
* @param {string} html - The HTML string to encode.
580+
*
581+
* @returns {string} - Returns the escaped html string:
582+
* ``&`` will be replaced with ``&``.
583+
* ``<`` will be repalced with ``&lt;``,
584+
* ``>`` will be replaced with ``&gt;``,
585+
* ``"`` will be replaced with ``&quot;``.
586+
*/
587+
const escape_html = (html) => {
588+
return (html || "")
589+
.replace(/&/g, "&amp;") // needs to be first!
590+
.replace(/</g, "&lt;")
591+
.replace(/>/g, "&gt;")
592+
.replace(/"/g, "&quot;");
593+
};
594+
595+
/**
596+
* Return unescaped, raw HTML from an escaped HTML string.
597+
*
598+
* See: https://developer.mozilla.org/en-US/docs/Glossary/Entity#reserved_characters
599+
*
600+
* @param {string} escaped_html - The HTML string to decode.
601+
*
602+
* @returns {string} - Returns the escaped html string:
603+
* ``&amp;`` will be replaced with ``&``,
604+
* ``&lt;`` will be repalced with ``<``,
605+
* ``&gt;`` will be replaced with ``>``,
606+
* ``&quot;`` will be replaced with ``"``.
607+
*/
608+
const unescape_html = (escaped_html) => {
609+
return (escaped_html || "")
610+
.replace(/&amp;/g, "&")
611+
.replace(/&lt;/g, "<")
612+
.replace(/&gt;/g, ">")
613+
.replace(/&quot;/g, '"');
614+
};
615+
573616
var utils = {
574617
// pattern pimping - own module?
575618
jqueryPlugin: jqueryPlugin,
@@ -599,6 +642,8 @@ var utils = {
599642
jqToNode: jqToNode,
600643
ensureArray: ensureArray,
601644
localized_isodate: localized_isodate,
645+
escape_html: escape_html,
646+
unescape_html: unescape_html,
602647
};
603648

604649
export default utils;

src/core/utils.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,21 @@ describe("animation_frame ...", function () {
716716
expect(mock).toHaveBeenCalled();
717717
});
718718
});
719+
720+
describe("escape/unescape html ...", function () {
721+
it("escapes and unescapes html", () => {
722+
const unescaped = `<hello attribute="value">this & that</hello>`;
723+
const escaped = `&lt;hello attribute=&quot;value&quot;&gt;this &amp; that&lt;/hello&gt;`;
724+
725+
expect(utils.escape_html(unescaped)).toBe(escaped);
726+
expect(utils.unescape_html(escaped)).toBe(unescaped);
727+
});
728+
729+
it("does not break with null-ish input", () => {
730+
expect(utils.escape_html(null)).toBe("");
731+
expect(utils.escape_html(undefined)).toBe("");
732+
733+
expect(utils.unescape_html(null)).toBe("");
734+
expect(utils.unescape_html(undefined)).toBe("");
735+
});
736+
});

0 commit comments

Comments
 (0)