mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 07:47:49 +00:00
Consolidate escape and unescape modules.
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
import basePropertyOf from './.internal/basePropertyOf.js';
|
||||
|
||||
/** Used to map characters to HTML entities. */
|
||||
const htmlEscapes = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": '''
|
||||
};
|
||||
|
||||
/**
|
||||
* Used by `escape` to convert characters to HTML entities.
|
||||
*
|
||||
* @private
|
||||
* @param {string} chr The matched character to escape.
|
||||
* @returns {string} Returns the escaped character.
|
||||
*/
|
||||
const escapeHtmlChar = basePropertyOf(htmlEscapes);
|
||||
|
||||
export default escapeHtmlChar;
|
||||
@@ -1,21 +0,0 @@
|
||||
import basePropertyOf from './.internal/basePropertyOf.js';
|
||||
|
||||
/** Used to map HTML entities to characters. */
|
||||
const htmlUnescapes = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
''': "'"
|
||||
};
|
||||
|
||||
/**
|
||||
* Used by `unescape` to convert HTML entities to characters.
|
||||
*
|
||||
* @private
|
||||
* @param {string} chr The matched character to unescape.
|
||||
* @returns {string} Returns the unescaped character.
|
||||
*/
|
||||
const unescapeHtmlChar = basePropertyOf(htmlUnescapes);
|
||||
|
||||
export default unescapeHtmlChar;
|
||||
12
escape.js
12
escape.js
@@ -1,6 +1,14 @@
|
||||
import escapeHtmlChar from './.internal/escapeHtmlChar.js';
|
||||
import toString from './toString.js';
|
||||
|
||||
/** Used to map characters to HTML entities. */
|
||||
const htmlEscapes = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": '''
|
||||
};
|
||||
|
||||
/** Used to match HTML entities and HTML characters. */
|
||||
const reUnescapedHtml = /[&<>"']/g;
|
||||
const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
|
||||
@@ -34,7 +42,7 @@ const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
|
||||
function escape(string) {
|
||||
string = toString(string);
|
||||
return (string && reHasUnescapedHtml.test(string))
|
||||
? string.replace(reUnescapedHtml, escapeHtmlChar)
|
||||
? string.replace(reUnescapedHtml, chr => htmlEscapes[chr])
|
||||
: string;
|
||||
}
|
||||
|
||||
|
||||
12
unescape.js
12
unescape.js
@@ -1,5 +1,13 @@
|
||||
import toString from './toString.js';
|
||||
import unescapeHtmlChar from './.internal/unescapeHtmlChar.js';
|
||||
|
||||
/** Used to map HTML entities to characters. */
|
||||
const htmlUnescapes = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
''': "'"
|
||||
};
|
||||
|
||||
/** Used to match HTML entities and HTML characters. */
|
||||
const reEscapedHtml = /&(?:amp|lt|gt|quot|#39);/g;
|
||||
@@ -25,7 +33,7 @@ const reHasEscapedHtml = RegExp(reEscapedHtml.source);
|
||||
function unescape(string) {
|
||||
string = toString(string);
|
||||
return (string && reHasEscapedHtml.test(string))
|
||||
? string.replace(reEscapedHtml, unescapeHtmlChar)
|
||||
? string.replace(reEscapedHtml, entity => htmlUnescapes[entity])
|
||||
: string;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user