From 2f873744761677cfc8e898a1355e71c9b1be17b7 Mon Sep 17 00:00:00 2001 From: pyoor Date: Wed, 29 Aug 2018 17:51:20 -0400 Subject: [PATCH] Add HTML encode/decode methods --- lib/utils/common.js | 19 +++++++++++++++++++ package.json | 1 + 2 files changed, 20 insertions(+) diff --git a/lib/utils/common.js b/lib/utils/common.js index a04cbe7..2e7b0cb 100644 --- a/lib/utils/common.js +++ b/lib/utils/common.js @@ -1,6 +1,7 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +const htmlentities = require('htmlentities') const jsesc = require('jsesc') const utils = require('../utils') @@ -72,6 +73,24 @@ class common extends utils { } } + /** + * Escape special characters using HTML entities + * @param {string} str + * @returns {string} + */ + static htmlEncode (str) { + return htmlentities.encode(str) + } + + /** + * Remove HTML entities from string + * @param {string} str + * @returns {string} + */ + static htmlDencode (str) { + return htmlentities.decode(str) + } + /** * Remove duplicate items from a list * @param {Array} list diff --git a/package.json b/package.json index c35849d..1258fbd 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "webpack-cli": "^3.1.0" }, "dependencies": { + "htmlentities": "^1.0.0", "jsesc": "^2.5.1" } }