Add HTML encode/decode methods

This commit is contained in:
pyoor 2018-08-29 17:51:20 -04:00
parent ac7f4b3580
commit 2f87374476
2 changed files with 20 additions and 0 deletions

View file

@ -1,6 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public /* 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 * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const htmlentities = require('htmlentities')
const jsesc = require('jsesc') const jsesc = require('jsesc')
const utils = require('../utils') 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 * Remove duplicate items from a list
* @param {Array} list * @param {Array} list

View file

@ -79,6 +79,7 @@
"webpack-cli": "^3.1.0" "webpack-cli": "^3.1.0"
}, },
"dependencies": { "dependencies": {
"htmlentities": "^1.0.0",
"jsesc": "^2.5.1" "jsesc": "^2.5.1"
} }
} }